Spaces:
Sleeping
Sleeping
| import { createQueryOptions } from '@/api-client/query-config'; | |
| import { rpcClient } from '@/api-client/utils/rpc-client'; | |
| import type { PoxRequest, PoxResponse } from '@/schema/gradio-proxy/pox'; | |
| import type { FetchQueryOptions } from '@tanstack/react-query'; | |
| import { useQueryClient } from '@tanstack/react-query'; | |
| import { useCallback } from 'react'; | |
| /** | |
| * pox APIを呼び出すカスタムフック | |
| * @returns キャッシュ対応のAPI呼び出し関数(タイムアウト20分、キャッシュ24時間) | |
| */ | |
| export function usePox() { | |
| const queryClient = useQueryClient(); | |
| return useCallback( | |
| async (request: PoxRequest): Promise<PoxResponse> => { | |
| console.log('[usePox] API呼び出し開始:', { | |
| hasCommonDict: !!request.commonDict, | |
| hasScoreDict: !!request.scoreDict, | |
| hasScoreTotal: !!request.score_total, | |
| dummyMode: request.dummyMode, | |
| }); | |
| return queryClient.fetchQuery( | |
| createQueryOptions<PoxResponse>({ | |
| queryKey: ['pox', request], | |
| queryFn: async (): Promise<PoxResponse> => { | |
| try { | |
| const response = await rpcClient['gradio-proxy']['pox'].$post({ | |
| json: request, | |
| }); | |
| if (!response.ok) { | |
| console.error('[usePox] Response status:', response.status); | |
| const errorData = (await response.json()) as { message?: string; status?: string }; | |
| console.error('[usePox] API エラー:', errorData); | |
| throw new Error(errorData.message || 'pox APIの呼び出しに失敗しました'); | |
| } | |
| const data = await response.json(); | |
| console.log('[usePox] API呼び出し成功'); | |
| return data as PoxResponse; | |
| } catch (error) { | |
| console.error('[usePox] API呼び出しエラー:', error); | |
| throw error; | |
| } | |
| }, | |
| }) as FetchQueryOptions<PoxResponse>, | |
| ); | |
| }, | |
| [queryClient], | |
| ); | |
| } | |