o2o-infinith-frontend/src/features/report/hooks/useReport.ts

54 lines
1.5 KiB
TypeScript

import { useState, useEffect } from 'react';
import type { MarketingReport } from '@/features/report/types/report';
import { getReport } from '@/shared/api/generated/reports/reports';
import { transformReportOutput } from '@/features/report/lib/transformReport';
interface UseReportResult {
data: MarketingReport | null;
isLoading: boolean;
error: string | null;
isEnriched: boolean;
socialHandles: Record<string, string | null> | null;
clinicId: string | null;
}
export function useReport(id: string | undefined): UseReportResult {
const [data, setData] = useState<MarketingReport | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (!id) {
setError('리포트 ID가 없습니다.');
setIsLoading(false);
return;
}
getReport(id)
.then((res) => {
if (res.status !== 200) {
throw new Error('리포트 조회에 실패했습니다.');
}
const output = res.data;
if (!output) {
throw new Error('리포트 데이터가 비어있습니다.');
}
const transformed = transformReportOutput(id, output, { url: '', generatedAt: '' });
setData(transformed);
})
.catch((err) => {
setError(err instanceof Error ? err.message : 'Failed to fetch report');
})
.finally(() => setIsLoading(false));
}, [id]);
return {
data,
isLoading,
error,
isEnriched: false,
socialHandles: null,
clinicId: null,
};
}