o2o-infinith-demo/src/hooks/useMarketingPlan.ts
Haewon Kam 4484ac788a feat: P0 fixes — date formatting, channel labels, dynamic marketing plan
- ReportHeader/PlanHeader: format ISO dates as Korean (2026년 4월 2일)
- ChannelOverview: map API keys to Korean labels (naverBlog → 네이버 블로그)
- useMarketingPlan: replace mockPlan with real DB-based plan generation
- transformPlan: build MarketingPlan from report data (channels, pillars, calendar)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 13:58:40 +09:00

67 lines
2.0 KiB
TypeScript

import { useState, useEffect } from 'react';
import { useLocation } from 'react-router';
import type { MarketingPlan } from '../types/plan';
import { fetchReportById } from '../lib/supabase';
import { transformReportToPlan } from '../lib/transformPlan';
interface UseMarketingPlanResult {
data: MarketingPlan | null;
isLoading: boolean;
error: string | null;
}
interface LocationState {
report?: Record<string, unknown>;
metadata?: Record<string, unknown>;
reportId?: string;
}
export function useMarketingPlan(id: string | undefined): UseMarketingPlanResult {
const [data, setData] = useState<MarketingPlan | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const location = useLocation();
useEffect(() => {
if (!id) {
setError('No plan ID provided');
setIsLoading(false);
return;
}
const state = location.state as LocationState | undefined;
// Source 1: Report data passed via navigation state
if (state?.report && state?.metadata) {
try {
const plan = transformReportToPlan({
id: (state.reportId || id),
url: (state.metadata.url as string) || '',
clinic_name: (state.metadata.clinicName as string) || '',
report: state.report,
created_at: (state.metadata.generatedAt as string) || new Date().toISOString(),
});
setData(plan);
setIsLoading(false);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to build marketing plan');
setIsLoading(false);
}
return;
}
// Source 2: Fetch report from Supabase and transform to plan
fetchReportById(id)
.then((row) => {
const plan = transformReportToPlan(row);
setData(plan);
})
.catch((err) => {
setError(err instanceof Error ? err.message : 'Failed to fetch marketing plan');
})
.finally(() => setIsLoading(false));
}, [id, location.state]);
return { data, isLoading, error };
}