mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-06 20:15:17 +02:00
refactor: migrate community prompts from imperative hook to jotai atom
This commit is contained in:
parent
ccce47846b
commit
8727acb5ad
3 changed files with 10 additions and 49 deletions
|
|
@ -12,8 +12,9 @@ import { ScrollArea } from "@/components/ui/scroll-area";
|
|||
import { Switch } from "@/components/ui/switch";
|
||||
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { type CommunityPrompt, useCommunityPrompts } from "@/hooks/use-community-prompts";
|
||||
import { communityPromptsAtom } from "@/atoms/search-spaces/search-space-query.atoms";
|
||||
import { authenticatedFetch } from "@/lib/auth-utils";
|
||||
import { useAtomValue } from "jotai";
|
||||
|
||||
interface SetupPromptStepProps {
|
||||
searchSpaceId: number;
|
||||
|
|
@ -21,7 +22,9 @@ interface SetupPromptStepProps {
|
|||
}
|
||||
|
||||
export function SetupPromptStep({ searchSpaceId, onComplete }: SetupPromptStepProps) {
|
||||
const { prompts, loading: loadingPrompts } = useCommunityPrompts();
|
||||
const communityPromptsQuery = useAtomValue(communityPromptsAtom);
|
||||
const prompts = communityPromptsQuery.data || [];
|
||||
const loadingPrompts = communityPromptsQuery.isPending;
|
||||
const [enableCitations, setEnableCitations] = useState(true);
|
||||
const [customInstructions, setCustomInstructions] = useState("");
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
|
|
|||
|
|
@ -24,9 +24,10 @@ import { Switch } from "@/components/ui/switch";
|
|||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { type CommunityPrompt, useCommunityPrompts } from "@/hooks/use-community-prompts";
|
||||
import { communityPromptsAtom } from "@/atoms/search-spaces/search-space-query.atoms";
|
||||
import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service";
|
||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { authenticatedFetch } from "@/lib/auth-utils";
|
||||
|
||||
interface PromptConfigManagerProps {
|
||||
|
|
@ -39,7 +40,9 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
|
|||
queryFn: () => searchSpacesApiService.getSearchSpace({ id: searchSpaceId }),
|
||||
enabled: !!searchSpaceId,
|
||||
});
|
||||
const { prompts, loading: loadingPrompts } = useCommunityPrompts();
|
||||
const communityPromptsQuery = useAtomValue(communityPromptsAtom);
|
||||
const prompts = communityPromptsQuery.data || [];
|
||||
const loadingPrompts = communityPromptsQuery.isPending;
|
||||
|
||||
const [enableCitations, setEnableCitations] = useState(true);
|
||||
const [customInstructions, setCustomInstructions] = useState("");
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
export interface CommunityPrompt {
|
||||
key: string;
|
||||
value: string;
|
||||
author: string;
|
||||
link: string | null;
|
||||
category?: string;
|
||||
}
|
||||
|
||||
export function useCommunityPrompts() {
|
||||
const [prompts, setPrompts] = useState<CommunityPrompt[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchPrompts = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await fetch(
|
||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces/prompts/community`
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch community prompts: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
setPrompts(data);
|
||||
setError(null);
|
||||
} catch (err: any) {
|
||||
setError(err.message || "Failed to fetch community prompts");
|
||||
console.error("Error fetching community prompts:", err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchPrompts();
|
||||
}, [fetchPrompts]);
|
||||
|
||||
return { prompts, loading, error, refetch: fetchPrompts };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue