"use client"; import { useEditor, EditorContent } from "@tiptap/react"; import StarterKit from "@tiptap/starter-kit"; import Highlight from "@tiptap/extension-highlight"; import TextAlign from "@tiptap/extension-text-align"; import { Markdown } from "@tiptap/markdown"; import { HomelabApp } from "./types"; import { invoke } from "@tauri-apps/api/core"; import { useEffect, useState } from "react"; import Menubar from "./menu-bar"; interface TextEditorProps { content: string; onSave: (content: string, linkedAppIds: string[]) => void; initialLinkedAppIds?: string[]; // Added prop } const getApps = async (): Promise => { return (await invoke("get_app")) as HomelabApp[]; }; export default function TextEditor({ content, onSave, initialLinkedAppIds = [], }: TextEditorProps) { const [availableApps, setAvailableApps] = useState([]); // Seed state instantly from the prop if it exists const [linkedAppIds, setLinkedAppIds] = useState(initialLinkedAppIds); useEffect(() => { const fetchApps = async () => { const apps = await getApps(); setAvailableApps(apps); }; fetchApps(); }, []); const editor = useEditor({ extensions: [ StarterKit.configure({ bulletList: { HTMLAttributes: { class: "list-disc ml-3" } }, orderedList: { HTMLAttributes: { class: "list-decimal ml-3" } }, }), Highlight.configure({ HTMLAttributes: { class: "bg-yellow-200" } }), TextAlign.configure({ types: ["heading", "paragraph"] }), Markdown, ], immediatelyRender: false, content: content || "

Start typing your documentation notes here...

", editorProps: { attributes: { class: "prose max-w-none focus:outline-none min-h-[300px]", }, }, }); const toggleAppLink = (appId: string) => { setLinkedAppIds((prev) => prev.includes(appId) ? prev.filter((id) => id !== appId) : [...prev, appId], ); }; const handleSave = () => { if (!editor) return; const htmlOutput = editor.getHTML(); onSave(htmlOutput, linkedAppIds); }; return (
Draft Session

Document Assets

Click apps to bind/unbind them to this entire document.

{availableApps.map((app) => { const isLinked = linkedAppIds.includes(app.id); return ( ); })}
); }