import { CompletionContext, CompletionResult, useEffect, useRef, } from "../deps.ts"; import type { ComponentChildren, FunctionalComponent } from "../deps.ts"; import { Notification } from "../types.ts"; import { FeatherProps } from "https://esm.sh/v99/preact-feather@4.2.1/dist/types"; import { MiniEditor } from "./mini_editor.tsx"; export type ActionButton = { icon: FunctionalComponent; description: string; callback: () => void; }; export function TopBar({ pageName, unsavedChanges, isLoading, notifications, onRename, actionButtons, darkMode, vimMode, completer, lhs, rhs, }: { pageName?: string; unsavedChanges: boolean; isLoading: boolean; notifications: Notification[]; darkMode: boolean; vimMode: boolean; onRename: (newName?: string) => Promise; completer: (context: CompletionContext) => Promise; actionButtons: ActionButton[]; lhs?: ComponentChildren; rhs?: ComponentChildren; }) { // const [theme, setTheme] = useState(localStorage.theme ?? "light"); const inputRef = useRef(null); // Another one of my less proud moments: // Somehow I cannot seem to proerply limit the width of the page name, so I'm doing // it this way. If you have a better way to do this, please let me know! useEffect(() => { function resizeHandler() { const currentPageElement = document.getElementById("sb-current-page"); if (currentPageElement) { // Temporarily make it very narrow to give the parent space currentPageElement.style.width = "10px"; const innerDiv = currentPageElement.parentElement!.parentElement!; // Then calculate a new width currentPageElement.style.width = `${ Math.min(650, innerDiv.clientWidth - 150) }px`; } } globalThis.addEventListener("resize", resizeHandler); // Stop listening on unmount return () => { globalThis.removeEventListener("resize", resizeHandler); }; }, []); return (
{lhs}
{ if (newName !== pageName) { return onRename(newName); } else { return onRename(); } }} completer={completer} onEnter={(newName) => { onRename(newName); }} /> {notifications.length > 0 && (
{notifications.map((notification) => (
{notification.message}
))}
)}
{actionButtons.map((actionButton) => ( ))}
{rhs}
); }