mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-27 01:36:30 +02:00
feat: add public chat frontend
This commit is contained in:
parent
9d7259aab9
commit
37adc54d6a
9 changed files with 415 additions and 1 deletions
53
surfsense_web/hooks/use-public-chat-runtime.ts
Normal file
53
surfsense_web/hooks/use-public-chat-runtime.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
"use client";
|
||||
|
||||
import {
|
||||
type AppendMessage,
|
||||
type ThreadMessageLike,
|
||||
useExternalStoreRuntime,
|
||||
} from "@assistant-ui/react";
|
||||
import { useCallback, useMemo } from "react";
|
||||
import type { GetPublicChatResponse, PublicChatMessage } from "@/contracts/types/public-chat.types";
|
||||
|
||||
interface UsePublicChatRuntimeOptions {
|
||||
data: GetPublicChatResponse | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a read-only runtime for public chat viewing.
|
||||
*/
|
||||
export function usePublicChatRuntime({ data }: UsePublicChatRuntimeOptions) {
|
||||
const messages = useMemo(() => data?.messages ?? [], [data?.messages]);
|
||||
|
||||
// No-op - public chat is read-only
|
||||
const onNew = useCallback(async (_message: AppendMessage) => {}, []);
|
||||
|
||||
// Convert PublicChatMessage to ThreadMessageLike
|
||||
const convertMessage = useCallback(
|
||||
(msg: PublicChatMessage, idx: number): ThreadMessageLike => ({
|
||||
id: `public-msg-${idx}`,
|
||||
role: msg.role as "user" | "assistant",
|
||||
content: msg.content as ThreadMessageLike["content"],
|
||||
createdAt: new Date(msg.created_at),
|
||||
metadata: msg.author
|
||||
? {
|
||||
custom: {
|
||||
author: {
|
||||
displayName: msg.author.display_name,
|
||||
avatarUrl: msg.author.avatar_url,
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
||||
const runtime = useExternalStoreRuntime({
|
||||
isRunning: false,
|
||||
messages,
|
||||
onNew,
|
||||
convertMessage,
|
||||
});
|
||||
|
||||
return runtime;
|
||||
}
|
||||
14
surfsense_web/hooks/use-public-chat.ts
Normal file
14
surfsense_web/hooks/use-public-chat.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { useQuery } from "@tanstack/react-query";
|
||||
import type { GetPublicChatResponse } from "@/contracts/types/public-chat.types";
|
||||
import { publicChatApiService } from "@/lib/apis/public-chat-api.service";
|
||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||
|
||||
export function usePublicChat(shareToken: string) {
|
||||
return useQuery<GetPublicChatResponse, Error>({
|
||||
queryKey: cacheKeys.publicChat.byToken(shareToken),
|
||||
queryFn: () => publicChatApiService.getPublicChat({ share_token: shareToken }),
|
||||
enabled: !!shareToken,
|
||||
staleTime: 30_000,
|
||||
retry: false,
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue