o2o-infinith-demo/src/hooks/useEnrichment.ts
Haewon Kam a7d8aeeddc feat: Facebook page data collection via Apify scraper
- enrich-channels: add Facebook Pages Scraper (apify~facebook-pages-scraper)
- Collects: pageName, followers, likes, categories, email, phone, website, intro, rating
- transformReport: merge Facebook data into facebookAudit.pages[] (auto-shows section)
- Frontend: pass facebookHandle through enrichment pipeline
- EnrichChannelsRequest: add facebookHandle parameter

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 16:16:37 +09:00

71 lines
2.1 KiB
TypeScript

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;
instagramHandles?: string[];
youtubeChannelId?: string;
facebookHandle?: 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;
// Always enrich if clinicName exists — Naver, 강남언니, Google Maps work with name alone
hasTriggered.current = true;
setStatus('loading');
enrichChannels({
reportId: params.reportId,
clinicName: params.clinicName,
instagramHandle: params.instagramHandle,
instagramHandles: params.instagramHandles,
youtubeChannelId: params.youtubeChannelId,
facebookHandle: params.facebookHandle,
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,
};
}