silverbullet/web/components/top_bar.tsx

145 lines
3.6 KiB
TypeScript
Raw Normal View History

// import { Fragment, h } from "../deps.ts";
2022-11-15 02:36:35 +08:00
import { FontAwesomeIcon, useRef } from "../deps.ts";
2022-11-15 02:36:35 +08:00
import {
ComponentChildren,
IconDefinition,
useEffect,
useState,
} from "../deps.ts";
import { Notification } from "../types.ts";
import { isMacLike } from "../../common/util.ts";
function prettyName(s: string | undefined): string {
if (!s) {
return "";
}
return s.replaceAll("/", " / ");
}
2022-11-15 02:36:35 +08:00
export type ActionButton = {
icon: IconDefinition;
description: string;
callback: () => void;
};
export function TopBar({
pageName,
unsavedChanges,
isLoading,
notifications,
2022-11-15 02:36:35 +08:00
onRename,
actionButtons,
2022-04-05 00:33:13 +08:00
lhs,
rhs,
}: {
pageName?: string;
unsavedChanges: boolean;
isLoading: boolean;
notifications: Notification[];
2022-11-15 02:36:35 +08:00
onRename: (newName: string) => void;
actionButtons: ActionButton[];
lhs?: ComponentChildren;
rhs?: ComponentChildren;
}) {
const [theme, setTheme] = useState<string>(localStorage.theme ?? "light");
2022-11-15 02:36:35 +08:00
const inputRef = useRef<HTMLInputElement>(null);
2022-11-15 16:26:25 +08:00
const [editMode, setEditMode] = useState<boolean>(false);
2022-09-25 01:01:16 +08:00
const isMac = isMacLike();
2022-11-15 02:36:35 +08:00
useEffect(() => {
if (editMode) {
setTimeout(() => {
if (inputRef.current) {
console.log("Going to focus");
inputRef.current!.focus();
}
}, 0);
}
}, [editMode]);
return (
2022-11-15 16:26:25 +08:00
<div id="sb-top">
2022-04-05 00:33:13 +08:00
{lhs}
2022-11-15 16:26:25 +08:00
<div
className="main"
onClick={(e) => {
if (!editMode) {
setEditMode(true);
setTimeout(() => {
if (inputRef.current) {
console.log("Going to dispatch click event again");
inputRef.current!.dispatchEvent(e);
}
}, 100);
}
}}
>
2022-04-05 00:33:13 +08:00
<div className="inner">
2022-04-10 17:04:07 +08:00
<span
2022-08-02 20:40:04 +08:00
className={`sb-current-page ${
isLoading
? "sb-loading"
: unsavedChanges
? "sb-unsaved"
: "sb-saved"
2022-08-02 20:40:04 +08:00
}`}
2022-04-10 17:04:07 +08:00
>
2022-11-15 02:36:35 +08:00
{editMode
? (
<input
type="text"
ref={inputRef}
value={pageName}
className="sb-edit-page-name"
2022-11-15 16:26:25 +08:00
onBlur={() => {
setEditMode(false);
}}
2022-11-15 02:36:35 +08:00
onKeyDown={(e) => {
console.log("Key press", e);
e.stopPropagation();
if (e.key === "Enter") {
e.preventDefault();
const newName = (e.target as any).value;
onRename(newName);
2022-11-15 16:26:25 +08:00
setEditMode(false);
2022-11-15 02:36:35 +08:00
}
}}
/>
)
2022-11-15 16:26:25 +08:00
: pageName}
2022-04-05 00:33:13 +08:00
</span>
{notifications.length > 0 && (
2022-08-02 20:40:04 +08:00
<div className="sb-notifications">
2022-04-05 00:33:13 +08:00
{notifications.map((notification) => (
<div
key={notification.id}
2022-08-02 20:40:04 +08:00
className={`sb-notification-${notification.type}`}
>
{notification.message}
</div>
2022-04-05 00:33:13 +08:00
))}
</div>
)}
2022-08-02 20:40:04 +08:00
<div className="sb-actions">
2022-11-15 02:36:35 +08:00
{actionButtons.map((actionButton) => (
<button
onClick={(e) => {
actionButton.callback();
e.stopPropagation();
}}
title={actionButton.description}
>
<FontAwesomeIcon icon={actionButton.icon} />
</button>
))}
2022-05-07 00:55:04 +08:00
</div>
2022-04-05 00:33:13 +08:00
</div>
</div>
2022-04-05 00:33:13 +08:00
{rhs}
</div>
);
}