Files
o2o-infinith-demo/src/hooks/useMarketingPlan.ts
T
Haewon KamandClaude Opus 5.5 0be730f0f9 fix: 데모 화면의 DB 조회·쓰기를 서버 함수로 옮긴다
브라우저가 공개 키로 테이블을 직접 읽고 쓰던 구조를 없앤다.
- api/db.js: 정해진 작업만 서비스 키로 실행 (임의 쿼리·열 지정 불가)
- api/_origin.js: 서버 함수 공용 출처 검사, 공개 배포에서는 404
- src/lib/db.ts: 화면용 호출 함수, 반환 형태 { data, error } 유지
- 빌드 큐에 바로 넣던 예전 진단 신청 모달 삭제

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 16:09:29 +09:00

187 lines
6.6 KiB
TypeScript

import { useState, useEffect } from 'react';
import { useLocation } from 'react-router';
import type { MarketingPlan, ChannelStrategyCard, CalendarData, ContentStrategyData } from '../types/plan';
import { fetchReportById, fetchActiveContentPlan } from '../lib/supabase';
import { db } from '../lib/db';
import { transformReportToPlan } from '../lib/transformPlan';
import { mockPlan } from '../data/mockPlan';
import { mockPlanBanobagi } from '../data/mockPlan_banobagi';
import { mockPlanGrand } from '../data/mockPlan_grand';
import { mockPlanWonjin } from '../data/mockPlan_wonjin';
import { mockPlanTs } from '../data/mockPlan_ts';
import { mockPlanIrum } from '../data/mockPlan_irum';
import { mockPlanO2O } from '../data/mockPlan_o2o';
const DEMO_PLANS: Record<string, MarketingPlan> = {
'view-clinic': mockPlan,
'banobagi': mockPlanBanobagi,
'grand': mockPlanGrand,
'wonjin': mockPlanWonjin,
'ts': mockPlanTs,
'irum': mockPlanIrum,
'o2o': mockPlanO2O,
};
interface UseMarketingPlanResult {
data: MarketingPlan | null;
isLoading: boolean;
error: string | null;
}
interface LocationState {
report?: Record<string, unknown>;
metadata?: Record<string, unknown>;
reportId?: string;
clinicId?: string;
}
/**
* Build a MarketingPlan from content_plans DB row.
* content_plans stores AI-generated strategy in JSONB columns.
*/
function buildPlanFromContentPlans(
row: Record<string, unknown>,
clinicName: string,
clinicNameEn: string,
targetUrl: string,
): MarketingPlan {
const channelStrategies = (row.channel_strategies || []) as ChannelStrategyCard[];
const contentStrategy = (row.content_strategy || {}) as ContentStrategyData;
const calendar = (row.calendar || { weeks: [], monthlySummary: [] }) as CalendarData;
return {
id: row.id as string,
reportId: (row.run_id as string) || (row.id as string),
clinicName,
clinicNameEn,
createdAt: (row.created_at as string) || new Date().toISOString(),
targetUrl,
brandGuide: (row.brand_guide as MarketingPlan['brandGuide']) || {
colors: [],
fonts: [],
logoRules: [],
toneOfVoice: {
personality: ['전문적', '친근한', '신뢰할 수 있는'],
communicationStyle: '의료 전문 지식을 쉽고 친근하게 전달',
doExamples: [],
dontExamples: [],
},
channelBranding: [],
brandInconsistencies: [],
},
channelStrategies,
contentStrategy: {
pillars: contentStrategy.pillars || [],
typeMatrix: contentStrategy.typeMatrix || [],
workflow: contentStrategy.workflow || [
{ step: 1, name: '기획', description: 'AI 콘텐츠 주제 선정', owner: 'INFINITH AI', duration: '자동' },
{ step: 2, name: '제작', description: 'AI 초안 생성 + 의료진 감수', owner: 'INFINITH AI', duration: '1시간' },
{ step: 3, name: '편집', description: '영상/이미지 편집', owner: 'INFINITH Studio', duration: '30분' },
{ step: 4, name: '배포', description: '채널별 최적화 배포', owner: 'INFINITH Distribution', duration: '자동' },
{ step: 5, name: '분석', description: '성과 데이터 수집 + 전략 조정', owner: 'INFINITH Analytics', duration: '자동' },
],
repurposingSource: contentStrategy.repurposingSource || '1개 롱폼 영상',
repurposingOutputs: contentStrategy.repurposingOutputs || [],
},
calendar,
assetCollection: { assets: [], youtubeRepurpose: [] },
};
}
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;
async function loadPlan() {
try {
// ─── Dev / Demo: return mock data immediately ───
if (id === 'demo') {
setData(mockPlan);
setIsLoading(false);
return;
}
if (id && id in DEMO_PLANS) {
setData(DEMO_PLANS[id]);
setIsLoading(false);
return;
}
// ─── Source 1: Try content_plans table (AI-generated strategy) ───
// First, resolve clinicId from navigation state or analysis_runs
let clinicId = state?.clinicId || null;
let clinicName = '';
let clinicNameEn = '';
let targetUrl = '';
if (!clinicId) {
// Try to find clinicId from analysis_runs by run/report ID
const { data: run } = await db<{ clinic_id: string }>('run.clinicId', { id });
if (run) clinicId = run.clinic_id;
}
if (clinicId) {
// Fetch clinic info for plan metadata
const { data: clinic } = await db<{ name: string | null; name_en: string | null; url: string | null }>('clinic.byId', { id: clinicId });
if (clinic) {
clinicName = clinic.name || '';
clinicNameEn = clinic.name_en || '';
targetUrl = clinic.url || '';
}
// Try fetching active content plan
const contentPlan = await fetchActiveContentPlan(clinicId);
if (contentPlan) {
const plan = buildPlanFromContentPlans(
contentPlan,
clinicName,
clinicNameEn,
targetUrl,
);
setData(plan);
setIsLoading(false);
return;
}
}
// ─── Source 2: Report data from navigation state (fallback) ───
if (state?.report && state?.metadata) {
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);
return;
}
// ─── Source 3: Fetch report from Supabase and transform (fallback) ───
const row = await fetchReportById(id);
const plan = transformReportToPlan(row);
setData(plan);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch marketing plan');
} finally {
setIsLoading(false);
}
}
loadPlan();
}, [id, location.state]);
return { data, isLoading, error };
}