// 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, lhs, rhs, }: { pageName?: string; unsavedChanges: boolean; isLoading: boolean; notifications: Notification[]; onRename: (newName: string) => void; actionButtons: ActionButton[]; lhs?: ComponentChildren; rhs?: ComponentChildren; }) { const [theme, setTheme] = useState(localStorage.theme ?? "light"); const inputRef = useRef(null); const [editMode, setEditMode] = useState(false); const isMac = isMacLike(); useEffect(() => { if (editMode) { setTimeout(() => { if (inputRef.current) { console.log("Going to focus"); inputRef.current!.focus(); } }, 0); } }, [editMode]); return (
{lhs}
{ if (!editMode) { setEditMode(true); setTimeout(() => { if (inputRef.current) { console.log("Going to dispatch click event again"); inputRef.current!.dispatchEvent(e); } }, 100); } }} >
{editMode ? ( { setEditMode(false); }} onKeyDown={(e) => { console.log("Key press", e); e.stopPropagation(); if (e.key === "Enter") { e.preventDefault(); const newName = (e.target as any).value; onRename(newName); setEditMode(false); } }} /> ) : pageName} {notifications.length > 0 && (
{notifications.map((notification) => (
{notification.message}
))}
)}
{actionButtons.map((actionButton) => ( ))}
{rhs}
); }