|
| 1 | +import { useCallback } from "react"; |
| 2 | + |
| 3 | +import * as memory from "../tinybase/store/memory"; |
| 4 | +import * as persisted from "../tinybase/store/persisted"; |
| 5 | +import { id } from "../utils"; |
| 6 | + |
| 7 | +export function Chat() { |
| 8 | + const currentChatGroupId = memory.useCurrentChatGroupId(); |
| 9 | + const chatGroupIds = persisted.UI.useSortedRowIds("chat_groups", "created_at", false, 0, 5, persisted.STORE_ID); |
| 10 | + |
| 11 | + const setCurrentChatGroupId = memory.UI.useSetValueCallback( |
| 12 | + "current_chat_group_id", |
| 13 | + (e: string) => e, |
| 14 | + [], |
| 15 | + memory.STORE_ID, |
| 16 | + ); |
| 17 | + |
| 18 | + const handleAddMessage = persisted.UI.useSetRowCallback( |
| 19 | + "chat_messages", |
| 20 | + id(), |
| 21 | + (row: persisted.ChatMessage) => ({ |
| 22 | + ...row, |
| 23 | + metadata: JSON.stringify(row.metadata), |
| 24 | + parts: JSON.stringify(row.parts), |
| 25 | + } satisfies persisted.ChatMessage), |
| 26 | + [], |
| 27 | + persisted.STORE_ID, |
| 28 | + ); |
| 29 | + |
| 30 | + const handleSubmitMessage = useCallback((e: React.FormEvent<HTMLFormElement>) => { |
| 31 | + e.preventDefault(); |
| 32 | + |
| 33 | + const formData = new FormData(e.currentTarget); |
| 34 | + const message = formData.get("message"); |
| 35 | + if (message) { |
| 36 | + handleAddMessage({ |
| 37 | + user_id: "TODO", |
| 38 | + chat_group_id: currentChatGroupId, |
| 39 | + role: "user", |
| 40 | + content: "TODO", |
| 41 | + metadata: "TODO", |
| 42 | + parts: JSON.stringify([]), |
| 43 | + created_at: new Date().toISOString(), |
| 44 | + }); |
| 45 | + } |
| 46 | + }, [handleAddMessage, currentChatGroupId]); |
| 47 | + |
| 48 | + const messageIds = persisted.UI.useSliceRowIds( |
| 49 | + persisted.INDEXES.chatMessagesByGroup, |
| 50 | + currentChatGroupId, |
| 51 | + persisted.STORE_ID, |
| 52 | + ); |
| 53 | + |
| 54 | + if (!currentChatGroupId || !messageIds?.length) { |
| 55 | + return ( |
| 56 | + <div className="border border-gray-300 rounded p-2"> |
| 57 | + <div className="text-gray-500">Select or create a chat group</div> |
| 58 | + |
| 59 | + <div className="flex flex-col gap-2"> |
| 60 | + {chatGroupIds?.map((chatGroupId) => ( |
| 61 | + <ChatGroup |
| 62 | + key={chatGroupId} |
| 63 | + id={chatGroupId} |
| 64 | + handleClick={() => setCurrentChatGroupId(chatGroupId)} |
| 65 | + /> |
| 66 | + ))} |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + return ( |
| 73 | + <div className="border border-gray-300 rounded p-2"> |
| 74 | + <button onClick={() => setCurrentChatGroupId("")}> |
| 75 | + reset |
| 76 | + </button> |
| 77 | + |
| 78 | + <div className="space-y-2"> |
| 79 | + {messageIds?.map((messageId) => <ChatMessage key={messageId} messageId={messageId} />)} |
| 80 | + </div> |
| 81 | + |
| 82 | + <form onSubmit={handleSubmitMessage}> |
| 83 | + <input |
| 84 | + name="message" |
| 85 | + type="text" |
| 86 | + className="border border-gray-300 rounded p-2" |
| 87 | + /> |
| 88 | + <button className="border border-gray-300 rounded p-2">Send</button> |
| 89 | + </form> |
| 90 | + </div> |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +function ChatGroup({ id, handleClick }: { id: string; handleClick: () => void }) { |
| 95 | + const chatGroup = persisted.UI.useRow("chat_groups", id, persisted.STORE_ID); |
| 96 | + |
| 97 | + return ( |
| 98 | + <div className="p-2 rounded bg-gray-50" onClick={handleClick}> |
| 99 | + <div className="text-xs text-gray-500 mb-1">{chatGroup?.title}</div> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +function ChatMessage({ messageId }: { messageId: string }) { |
| 105 | + const message = persisted.UI.useRow("chat_messages", messageId, persisted.STORE_ID); |
| 106 | + |
| 107 | + return ( |
| 108 | + <div className="p-2 rounded bg-gray-50"> |
| 109 | + <div className="text-xs text-gray-500 mb-1">{message?.role}</div> |
| 110 | + <div>{message?.content}</div> |
| 111 | + </div> |
| 112 | + ); |
| 113 | +} |
0 commit comments