import { useRef, useState } from "react";
import { BookOpen, ImagePlus } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
Popover,
PopoverTrigger,
PopoverContent,
PopoverHeader,
PopoverTitle,
} from "@/components/ui/popover";
import { useEditorStore } from "@/stores/editorStore";
import { useBacklinks } from "@/hooks/useBacklinks";
import BacklinkIndicator from "@/components/editor/BacklinkIndicator";
import { EXAMPLES } from "@/components/editor/examples";
interface Props {
pageName: string;
onNameChange?: (name: string) => void;
onSaveDraft: () => void;
onPublish: () => void;
onInsertExample: (source: string) => void;
saving: boolean;
isDirty: boolean;
}
export default function ToolBar({
pageName,
onNameChange,
onSaveDraft,
onPublish,
onInsertExample,
saving,
isDirty,
}: Props) {
const currentPage = useEditorStore((s) => s.currentPage);
const backlinks = useBacklinks(currentPage?.name);
const [examplesOpen, setExamplesOpen] = useState(false);
return (
{onNameChange ? (
onNameChange(e.target.value)}
placeholder="page-name"
className="font-mono w-48 h-8 text-sm"
/>
) : (
{pageName}
)}
Examples
}
/>
Insert Example
{EXAMPLES.map((ex) => (
))}
onInsertExample(
`image "${path}" braille 30\n align center`
)} />
{isDirty && (
Unsaved
)}
);
}
function UploadImageButton({ onUploaded }: { onUploaded: (path: string) => void }) {
const fileRef = useRef(null);
const [uploading, setUploading] = useState(false);
const handleUpload = async (e: React.ChangeEvent) => {
const file = e.target.files?.[0];
if (!file) return;
setUploading(true);
try {
const form = new FormData();
form.append("file", file);
const res = await fetch("/api/upload-image", { method: "POST", body: form });
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
toast.success(`Uploaded ${data.filename}`);
onUploaded(data.path);
} catch (err) {
toast.error(`Upload failed: ${err}`);
} finally {
setUploading(false);
if (fileRef.current) fileRef.current.value = "";
}
};
return (
<>
>
);
}