2022-10-10 12:50:21 +00:00
|
|
|
// import { Fragment, h } from "../deps.ts";
|
|
|
|
|
2022-11-18 15:04:37 +00:00
|
|
|
import { FontAwesomeIcon, useRef } from "../deps.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
2022-11-18 15:04:37 +00:00
|
|
|
import {
|
|
|
|
ComponentChildren,
|
|
|
|
IconDefinition,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
} from "../deps.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { Notification } from "../types.ts";
|
|
|
|
import { isMacLike } from "../../common/util.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
function prettyName(s: string | undefined): string {
|
|
|
|
if (!s) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return s.replaceAll("/", " / ");
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:04:37 +00:00
|
|
|
export type ActionButton = {
|
|
|
|
icon: IconDefinition;
|
|
|
|
description: string;
|
|
|
|
callback: () => void;
|
|
|
|
};
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
export function TopBar({
|
|
|
|
pageName,
|
2022-03-31 12:28:07 +00:00
|
|
|
unsavedChanges,
|
2022-09-06 14:21:33 +00:00
|
|
|
isLoading,
|
2022-03-20 08:56:28 +00:00
|
|
|
notifications,
|
2022-11-18 15:04:37 +00:00
|
|
|
onRename,
|
|
|
|
actionButtons,
|
2022-04-04 16:33:13 +00:00
|
|
|
lhs,
|
|
|
|
rhs,
|
2022-03-20 08:56:28 +00:00
|
|
|
}: {
|
|
|
|
pageName?: string;
|
2022-03-31 12:28:07 +00:00
|
|
|
unsavedChanges: boolean;
|
2022-09-06 14:21:33 +00:00
|
|
|
isLoading: boolean;
|
2022-03-20 08:56:28 +00:00
|
|
|
notifications: Notification[];
|
2022-11-24 16:09:05 +00:00
|
|
|
onRename: (newName?: string) => void;
|
2022-11-18 15:04:37 +00:00
|
|
|
actionButtons: ActionButton[];
|
2022-10-10 12:50:21 +00:00
|
|
|
lhs?: ComponentChildren;
|
|
|
|
rhs?: ComponentChildren;
|
2022-03-20 08:56:28 +00:00
|
|
|
}) {
|
2022-11-24 16:09:05 +00:00
|
|
|
// const [theme, setTheme] = useState<string>(localStorage.theme ?? "light");
|
2022-11-18 15:04:37 +00:00
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
2022-11-24 16:09:05 +00:00
|
|
|
// const isMac = isMacLike();
|
2022-09-24 17:01:16 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
return (
|
2022-11-18 15:04:37 +00:00
|
|
|
<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 ${
|
2022-09-06 14:21:33 +00:00
|
|
|
isLoading
|
|
|
|
? "sb-loading"
|
|
|
|
: unsavedChanges
|
|
|
|
? "sb-unsaved"
|
|
|
|
: "sb-saved"
|
2022-08-02 12:40:04 +00:00
|
|
|
}`}
|
2022-04-10 09:04:07 +00:00
|
|
|
>
|
2022-11-18 15:04:37 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
ref={inputRef}
|
|
|
|
value={pageName}
|
|
|
|
className="sb-edit-page-name"
|
2022-11-24 16:09:05 +00:00
|
|
|
onBlur={(e) => {
|
|
|
|
(e.target as any).value = pageName;
|
|
|
|
}}
|
2022-11-18 15:04:37 +00:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
e.preventDefault();
|
|
|
|
const newName = (e.target as any).value;
|
|
|
|
onRename(newName);
|
|
|
|
}
|
2022-11-24 16:09:05 +00:00
|
|
|
if (e.key === "Escape") {
|
|
|
|
onRename();
|
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
}}
|
|
|
|
/>
|
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) => (
|
2022-07-14 11:32:28 +00:00
|
|
|
<div
|
|
|
|
key={notification.id}
|
2022-08-02 12:40:04 +00:00
|
|
|
className={`sb-notification-${notification.type}`}
|
2022-07-14 11:32:28 +00:00
|
|
|
>
|
|
|
|
{notification.message}
|
|
|
|
</div>
|
2022-04-04 16:33:13 +00:00
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
2022-08-02 12:40:04 +00:00
|
|
|
<div className="sb-actions">
|
2022-11-18 15:04:37 +00:00
|
|
|
{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>
|
2022-03-20 08:56:28 +00:00
|
|
|
</div>
|
2022-04-04 16:33:13 +00:00
|
|
|
{rhs}
|
2022-03-20 08:56:28 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|