1
0
silverbullet/web/components/top_bar.tsx

110 lines
2.7 KiB
TypeScript
Raw Normal View History

// import { Fragment, h } from "../deps.ts";
import { FontAwesomeIcon, useRef } from "../deps.ts";
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("/", " / ");
}
export type ActionButton = {
icon: IconDefinition;
description: string;
callback: () => void;
};
export function TopBar({
pageName,
unsavedChanges,
isLoading,
notifications,
onRename,
actionButtons,
2022-04-04 16:33:13 +00:00
lhs,
rhs,
}: {
pageName?: string;
unsavedChanges: boolean;
isLoading: boolean;
notifications: Notification[];
onRename: (newName: string) => void;
actionButtons: ActionButton[];
lhs?: ComponentChildren;
rhs?: ComponentChildren;
}) {
const [theme, setTheme] = useState<string>(localStorage.theme ?? "light");
const inputRef = useRef<HTMLInputElement>(null);
2022-09-24 17:01:16 +00:00
const isMac = isMacLike();
return (
<div id="sb-top">
2022-04-04 16:33:13 +00:00
{lhs}
<div className="main">
<div className="inner">
2022-04-10 09:04:07 +00:00
<span
2022-08-02 12:40:04 +00:00
className={`sb-current-page ${
isLoading
? "sb-loading"
: unsavedChanges
? "sb-unsaved"
: "sb-saved"
2022-08-02 12:40:04 +00:00
}`}
2022-04-10 09:04:07 +00:00
>
<input
type="text"
ref={inputRef}
value={pageName}
className="sb-edit-page-name"
onKeyDown={(e) => {
e.stopPropagation();
if (e.key === "Enter") {
e.preventDefault();
const newName = (e.target as any).value;
onRename(newName);
}
}}
/>
2022-04-04 16:33:13 +00:00
</span>
{notifications.length > 0 && (
2022-08-02 12:40:04 +00:00
<div className="sb-notifications">
2022-04-04 16:33:13 +00:00
{notifications.map((notification) => (
<div
key={notification.id}
2022-08-02 12:40:04 +00:00
className={`sb-notification-${notification.type}`}
>
{notification.message}
</div>
2022-04-04 16:33:13 +00:00
))}
</div>
)}
2022-08-02 12:40:04 +00:00
<div className="sb-actions">
{actionButtons.map((actionButton) => (
<button
onClick={(e) => {
actionButton.callback();
e.stopPropagation();
}}
title={actionButton.description}
>
<FontAwesomeIcon icon={actionButton.icon} />
</button>
))}
2022-05-06 16:55:04 +00:00
</div>
2022-04-04 16:33:13 +00:00
</div>
</div>
2022-04-04 16:33:13 +00:00
{rhs}
</div>
);
}