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