import { useCallback, useEffect, useMemo, useState } from 'react' import { lexicalNodeToPlainText } from '../components/chat-view/chat-input/utils/editor-state-to-plain-text' import { useApp } from '../contexts/AppContext' import { CommandManager } from '../database/json/command/CommandManager' import { TemplateContent } from '../database/schema' export interface QuickCommand { id: string name: string content: TemplateContent contentText: string createdAt: number updatedAt: number } type UseCommands = { createCommand: (name: string, content: TemplateContent) => Promise deleteCommand: (id: string) => Promise updateCommand: (id: string, name: string, content: TemplateContent) => Promise commandList: QuickCommand[] } export function useCommands(): UseCommands { const [commandList, setCommandList] = useState([]) const app = useApp() const templateManager = useMemo(() => new CommandManager(app), [app]) const fetchCommandList = useCallback(async () => { templateManager.ListCommands().then((rows) => { setCommandList(rows.map((row) => ({ id: row.id, name: row.name, content: row.content, contentText: row.content.nodes.map(lexicalNodeToPlainText).join(''), createdAt: row.createdAt, updatedAt: row.updatedAt, }))) }) }, [templateManager]) useEffect(() => { void fetchCommandList() }, [fetchCommandList]) const createCommand = useCallback( async (name: string, content: TemplateContent): Promise => { await templateManager.createCommand({ name, content, }) fetchCommandList() }, [templateManager, fetchCommandList], ) const deleteCommand = useCallback( async (id: string): Promise => { await templateManager.deleteCommand(id) fetchCommandList() }, [templateManager, fetchCommandList], ) const updateCommand = useCallback( async (id: string, name: string, content: TemplateContent): Promise => { await templateManager.updateCommand(id, { name, content, }) fetchCommandList() }, [templateManager, fetchCommandList], ) return { createCommand, deleteCommand, updateCommand, commandList, } }