| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- /** @jsx h */
- import { h } from "preact";
- import { useEffect, useState } from "preact/hooks";
- import { EditorMode } from "./Editor.tsx";
- interface TopBarProps {
- allowMode: EditorMode;
- isLogined: boolean;
- }
- export default function TopBar(props: TopBarProps) {
- const [mode, setMode] = useState(props.allowMode);
- // Event listener
- const modeChangeListener = (e: CustomEvent) => {
- if (
- e.detail &&
- (props.allowMode === e.detail || props.allowMode === EditorMode.Both)
- ) {
- setMode(e.detail);
- }
- };
- // Event dispatcher
- const modeChangeDispatcher = (mode: EditorMode) => {
- dispatchEvent(new CustomEvent("ModeChange", { detail: mode }));
- };
- const doLogout = async () => {
- const resp = await fetch("/api/user/logout");
- const respJson = await resp.json();
- if (respJson.success) {
- location.href = "/login";
- return true;
- }
- return false;
- };
- const goHome = () => {
- location.href = "/";
- };
- // Init event listeners
- useEffect(() => {
- addEventListener("ModeChange", modeChangeListener);
- return () => {
- removeEventListener("ModeChange", modeChangeListener);
- };
- }, []);
- return (
- <div className="pd-top-bar">
- <div className="pd-top-bar-mode-switcher">
- <button
- className={`pd-top-bar-btn${
- mode === EditorMode.Edit ? " active" : ""
- }${props.allowMode === EditorMode.Read ? " disabled" : ""}`}
- id="edit"
- type="button"
- onClick={() => {
- modeChangeDispatcher(EditorMode.Edit);
- }}
- >
- Edit
- </button>
- <button
- className={`pd-top-bar-btn${
- mode === EditorMode.Read ? " active" : ""
- }${props.allowMode === EditorMode.Edit ? " disabled" : ""}`}
- id="read"
- type="button"
- onClick={() => {
- modeChangeDispatcher(EditorMode.Read);
- }}
- >
- Read
- </button>
- <button
- className={`pd-top-bar-btn${
- mode === EditorMode.Both ? " active" : ""
- }${props.allowMode !== EditorMode.Both ? " disabled" : ""}`}
- id="both"
- type="button"
- onClick={() => {
- modeChangeDispatcher(EditorMode.Both);
- }}
- >
- Both
- </button>
- </div>
- {props.isLogined ? (
- <div className="pd-top-bar-tool-icons">
- <i className="bi bi-box-arrow-left" onClick={doLogout} />
- <i className="bi bi-house-door" onClick={goHome} />
- <i className="bi bi-share" />
- <i className="bi bi-gear" />
- </div>
- ) : null}
- </div>
- );
- }
|