mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-12 22:42:13 +02:00
Merge pull request #745 from CREDO23/sur-106-feat-public-chats
[Feature] Public Chat Sharing and Cloning
This commit is contained in:
commit
752a51d3fd
43 changed files with 2253 additions and 474 deletions
|
|
@ -25,6 +25,10 @@ export const useGithubStars = () => {
|
|||
|
||||
setStars(data?.stargazers_count);
|
||||
} catch (err) {
|
||||
// Ignore abort errors (expected on unmount)
|
||||
if (err instanceof Error && err.name === "AbortError") {
|
||||
return;
|
||||
}
|
||||
if (err instanceof Error) {
|
||||
console.error("Error fetching stars:", err);
|
||||
setError(err.message);
|
||||
|
|
@ -37,7 +41,7 @@ export const useGithubStars = () => {
|
|||
getStars();
|
||||
|
||||
return () => {
|
||||
abortController.abort();
|
||||
abortController.abort("Component unmounted");
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
|
|
|||
51
surfsense_web/hooks/use-public-chat-runtime.ts
Normal file
51
surfsense_web/hooks/use-public-chat-runtime.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
"use client";
|
||||
|
||||
import { type AppendMessage, useExternalStoreRuntime } from "@assistant-ui/react";
|
||||
import { useCallback, useMemo } from "react";
|
||||
import type { GetPublicChatResponse, PublicChatMessage } from "@/contracts/types/public-chat.types";
|
||||
import { convertToThreadMessage } from "@/lib/chat/message-utils";
|
||||
import type { MessageRecord } from "@/lib/chat/thread-persistence";
|
||||
|
||||
interface UsePublicChatRuntimeOptions {
|
||||
data: GetPublicChatResponse | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map PublicChatMessage to MessageRecord shape for reuse of convertToThreadMessage
|
||||
*/
|
||||
function toMessageRecord(msg: PublicChatMessage, idx: number): MessageRecord {
|
||||
return {
|
||||
id: idx,
|
||||
thread_id: 0,
|
||||
role: msg.role as "user" | "assistant" | "system",
|
||||
content: msg.content,
|
||||
created_at: msg.created_at,
|
||||
author_id: msg.author ? "public" : null,
|
||||
author_display_name: msg.author?.display_name ?? null,
|
||||
author_avatar_url: msg.author?.avatar_url ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) => {}, []);
|
||||
|
||||
const convertMessage = useCallback(
|
||||
(msg: PublicChatMessage, idx: number) => convertToThreadMessage(toMessageRecord(msg, idx)),
|
||||
[]
|
||||
);
|
||||
|
||||
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