'use client'; import { useState, useEffect, useRef } from 'react'; import PaymentButton from '@/app/components/PaymentButton'; interface BirthKey { birth_year: number; birth_month: number; birth_day: number; birth_hour?: number; gender: string; } interface SajuAISectionProps { hasPaid: boolean; savedInterpretation: string | null; sajuData: object; daeun: object | null; daeunList: object[]; gender: string; birthKey: BirthKey; currentUrl: string; // Python 엔진 데이터 (더 정밀한 절기 계산 결과) engineData?: { interactions?: any[]; shinsal?: any[]; gongmang?: any; hiddenStems?: any[]; }; } export default function SajuAISection({ hasPaid, savedInterpretation, sajuData, daeun, daeunList, gender, birthKey, currentUrl, engineData, }: SajuAISectionProps) { const [status, setStatus] = useState<'idle' | 'loading' | 'done' | 'error'>( savedInterpretation ? 'done' : 'idle' ); const [interpretation, setInterpretation] = useState(savedInterpretation ?? ''); const called = useRef(false); useEffect(() => { if (!hasPaid || savedInterpretation || called.current) return; called.current = true; setStatus('loading'); fetch('/api/saju/analyze', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ saju: sajuData, daeun, daeunList, gender, engineData, // Python 엔진 데이터 전달 }), }) .then((r) => r.json()) .then((data) => { if (data.interpretation) { setInterpretation(data.interpretation); setStatus('done'); // birthKey 유효성 검사 후 저장 (NaN/null 방지) const { birth_year, birth_month, birth_day } = birthKey; if ( typeof birth_year === 'number' && !isNaN(birth_year) && typeof birth_month === 'number' && !isNaN(birth_month) && typeof birth_day === 'number' && !isNaN(birth_day) ) { fetch('/api/saju/save-interpretation', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ interpretation: data.interpretation, birthKey }), }).catch(() => {}); } } else { setStatus('error'); } }) .catch(() => setStatus('error')); }, [hasPaid]); // 미결제 상태 if (!hasPaid) { return (
성격, 재물운, 직업 적성, 애정운, 건강운, 대운 분석 등
GPT-4o가 생성하는 맞춤형 사주 해석을 받아보세요.
AI가 사주를 분석하는 중입니다...
약 20~30초 소요될 수 있습니다
AI 해석 생성에 실패했습니다.