feat: real API integration + YouTube Data API v3 + progressive loading
- Replace mock useReport() with real Supabase API data pipeline - Add transformReport.ts to map API responses to MarketingReport type - Add useEnrichment() hook for background channel data enrichment - Replace Apify YouTube scraper with YouTube Data API v3 - Add mergeEnrichment() for progressive data loading - Add EmptyState component for graceful empty data handling - Add socialHandles to generate-report metadata - Graceful empty data in ClinicSnapshot, YouTube, Instagram, Facebook - Add Supabase Edge Functions and DB migrations - Add developer handoff documentation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c6e18b6a67
commit
60cd055042
30 files changed
+2905
-142
No files matched your search
@@ -0,0 +1,67 @@
|
||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { enrichChannels, fetchReportById } from '../lib/supabase';
|
||||
import { mergeEnrichment, type EnrichmentData } from '../lib/transformReport';
|
||||
import type { MarketingReport } from '../types/report';
|
||||
|
||||
type EnrichmentStatus = 'idle' | 'loading' | 'success' | 'error';
|
||||
|
||||
interface UseEnrichmentResult {
|
||||
status: EnrichmentStatus;
|
||||
enrichedReport: MarketingReport | null;
|
||||
}
|
||||
|
||||
interface EnrichmentParams {
|
||||
reportId: string | null;
|
||||
clinicName: string;
|
||||
instagramHandle?: string;
|
||||
youtubeChannelId?: string;
|
||||
address?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers background channel enrichment after Phase 1 report renders.
|
||||
* Fires once, waits for the Edge Function to complete (~27s),
|
||||
* then returns the merged report.
|
||||
*/
|
||||
export function useEnrichment(
|
||||
baseReport: MarketingReport | null,
|
||||
params: EnrichmentParams | null,
|
||||
): UseEnrichmentResult {
|
||||
const [status, setStatus] = useState<EnrichmentStatus>('idle');
|
||||
const [enrichedReport, setEnrichedReport] = useState<MarketingReport | null>(null);
|
||||
const hasTriggered = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!baseReport || !params?.reportId || hasTriggered.current) return;
|
||||
// Don't enrich if no social handles are available
|
||||
if (!params.instagramHandle && !params.youtubeChannelId) return;
|
||||
|
||||
hasTriggered.current = true;
|
||||
setStatus('loading');
|
||||
|
||||
enrichChannels({
|
||||
reportId: params.reportId,
|
||||
clinicName: params.clinicName,
|
||||
instagramHandle: params.instagramHandle,
|
||||
youtubeChannelId: params.youtubeChannelId,
|
||||
address: params.address,
|
||||
})
|
||||
.then((result) => {
|
||||
if (result.success && result.data) {
|
||||
const merged = mergeEnrichment(baseReport, result.data as EnrichmentData);
|
||||
setEnrichedReport(merged);
|
||||
setStatus('success');
|
||||
} else {
|
||||
setStatus('error');
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
setStatus('error');
|
||||
});
|
||||
}, [baseReport, params]);
|
||||
|
||||
return {
|
||||
status,
|
||||
enrichedReport,
|
||||
};
|
||||
}
|
||||
+59
-12
@@ -1,6 +1,8 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useLocation } from 'react-router';
|
||||
import type { MarketingReport } from '../types/report';
|
||||
import { mockReport } from '../data/mockReport';
|
||||
import { fetchReportById } from '../lib/supabase';
|
||||
import { transformApiReport } from '../lib/transformReport';
|
||||
|
||||
interface UseReportResult {
|
||||
data: MarketingReport | null;
|
||||
@@ -8,27 +10,72 @@ interface UseReportResult {
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
interface LocationState {
|
||||
report?: Record<string, unknown>;
|
||||
metadata?: {
|
||||
url: string;
|
||||
clinicName: string;
|
||||
generatedAt: string;
|
||||
socialHandles?: Record<string, string | null>;
|
||||
address?: string;
|
||||
services?: string[];
|
||||
};
|
||||
reportId?: string;
|
||||
}
|
||||
|
||||
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);
|
||||
const location = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
if (!id) {
|
||||
setError('No report ID provided');
|
||||
setIsLoading(false);
|
||||
const state = location.state as LocationState | undefined;
|
||||
|
||||
// Source 1: Report data passed via navigation state (from AnalysisLoadingPage)
|
||||
if (state?.report && state?.metadata) {
|
||||
try {
|
||||
const reportId = state.reportId || id || 'live';
|
||||
const transformed = transformApiReport(
|
||||
reportId,
|
||||
state.report,
|
||||
state.metadata,
|
||||
);
|
||||
setData(transformed);
|
||||
setIsLoading(false);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to parse report data');
|
||||
setIsLoading(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Phase 1: Return mock data immediately
|
||||
// Phase 2+: Replace with real API call — fetch(`/api/reports/${id}`)
|
||||
const timer = setTimeout(() => {
|
||||
setData(mockReport);
|
||||
setIsLoading(false);
|
||||
}, 100);
|
||||
// Source 2: Fetch from Supabase by report ID (bookmarked/shared link)
|
||||
if (id && id !== 'view-clinic') {
|
||||
fetchReportById(id)
|
||||
.then((row) => {
|
||||
const transformed = transformApiReport(
|
||||
row.id,
|
||||
row.report,
|
||||
{
|
||||
url: row.url,
|
||||
clinicName: row.clinic_name || '',
|
||||
generatedAt: row.created_at,
|
||||
},
|
||||
);
|
||||
setData(transformed);
|
||||
})
|
||||
.catch((err) => {
|
||||
setError(err instanceof Error ? err.message : 'Failed to fetch report');
|
||||
})
|
||||
.finally(() => setIsLoading(false));
|
||||
return;
|
||||
}
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, [id]);
|
||||
// No data source available
|
||||
setError('리포트 데이터를 찾을 수 없습니다. 새 분석을 시작해주세요.');
|
||||
setIsLoading(false);
|
||||
}, [id, location.state]);
|
||||
|
||||
return { data, isLoading, error };
|
||||
}
|
||||
Reference in new issue
Block a user