Spaces:
Sleeping
Sleeping
| import { createQueryOptions } from '@/api-client/query-config'; | |
| import { rpcClient } from '@/api-client/utils/rpc-client'; | |
| import { useGlobalStore } from '@/store/global'; | |
| import { generateSelectionHash } from '@/store/proposal-trigger'; | |
| import { useQuery } from '@tanstack/react-query'; | |
| import { useMemo } from 'react'; | |
| // RefreshMoment用のパラメータ型定義 | |
| interface UseRefreshMomentParams { | |
| commonDict?: Record<string, unknown> | object | null; | |
| scoreDict?: Record<string, unknown> | object | null; | |
| swot?: Record<string, unknown> | object | null; | |
| userEmail?: string; | |
| } | |
| // 変換後のデータ型 | |
| export interface RefreshMomentResult { | |
| moments: Array<{ text: string; value: boolean }>; | |
| ownTheme: string; | |
| } | |
| /** | |
| * RefreshMoment(改善案の方向性)を取得するフック | |
| * AIが生成する追加の方向性選択肢とownThemeを取得 | |
| */ | |
| export function useRefreshMoment(params: UseRefreshMomentParams) { | |
| const { commonDict, scoreDict, swot, userEmail } = params; | |
| const { userIdentifier } = useGlobalStore(); | |
| // オブジェクトをハッシュ化して安定したキーを生成 | |
| const queryKeyHash = useMemo(() => { | |
| return generateSelectionHash({ | |
| commonDict, | |
| scoreDict, | |
| swot, | |
| userEmail, | |
| }); | |
| }, [commonDict, scoreDict, swot, userEmail]); | |
| return useQuery({ | |
| queryKey: ['refreshMoment', queryKeyHash], | |
| ...createQueryOptions<[string, string]>({ | |
| queryFn: async () => { | |
| if (!commonDict || !scoreDict || !swot) { | |
| throw new Error('Required parameters are missing'); | |
| } | |
| const response = await rpcClient['gradio-proxy']['refresh-moment'].$post({ | |
| json: { | |
| commonDict: commonDict as Record<string, unknown>, | |
| scoreDict: scoreDict as Record<string, unknown>, | |
| swot: swot as Record<string, unknown>, | |
| dummyMode: false, | |
| userEmail: userEmail ?? undefined, | |
| userIdentifier, | |
| }, | |
| }); | |
| if (!response.ok) { | |
| console.error('[useRefreshMoment] Response status:', response.status); | |
| const errorData = (await response.json()) as { message?: string; status?: string }; | |
| console.error('[useRefreshMoment] API エラー:', errorData); | |
| throw new Error(errorData.message || 'refresh_moment APIの呼び出しに失敗しました'); | |
| } | |
| const data = await response.json(); | |
| return data as [string, string]; | |
| }, | |
| enabled: !!commonDict && !!scoreDict && !!swot, | |
| }), | |
| select: (data): RefreshMomentResult => { | |
| // データを整形してから返す | |
| const responseArray = data as unknown as [string, string] | undefined; | |
| const moments = | |
| responseArray?.[0] | |
| ?.split('\n') | |
| .map((moment: string) => ({ | |
| text: moment.trim(), | |
| value: false, | |
| })) | |
| .filter((m: { text: string }) => m.text) || []; | |
| const ownTheme = responseArray?.[1] || ''; | |
| return { | |
| moments, | |
| ownTheme, | |
| }; | |
| }, | |
| }); | |
| } | |