"use client"; import type { Dispatch, FormEvent, SetStateAction } from "react"; import { ProfileFields } from "@/components/profile-fields"; import { activeChartStorageKey, deleteCloudChartProfile, saveCloudChartProfile, updateCloudChartProfile } from "@/lib/home-cloud-sync"; import { chartRelationshipLabel, formatChartUpdatedAt, missingOtherProfileStep, profileBirthTimeLabel, profileBirthTimeStatusLabel, profilePlaceLabel, readProfile, upsertSelfChart, } from "@/lib/home-profile"; import { emptyProfile, timestamp, type Account, type ChartLibraryRecord, type ChartRelationship, type Profile, type SynastryRelationshipType, type SynastryReportCard } from "@/lib/home-types"; export type ChartLibraryPanelProps = { readonly account: Account | null; readonly accountId: string | undefined; readonly chartLibrary: ChartLibraryRecord[]; readonly setChartLibrary: Dispatch>; readonly profile: Profile; readonly profileDraft: Profile; readonly setProfileDraft: Dispatch>; readonly setProfile: Dispatch>; readonly profileSaving: boolean; readonly profileNotice: string; readonly setProfileNotice: Dispatch>; readonly setAccountError: Dispatch>; readonly editingSelfChart: boolean; readonly setEditingSelfChart: Dispatch>; readonly otherProfileDraft: Profile; readonly setOtherProfileDraft: Dispatch>; readonly otherChartRelationship: Exclude; readonly setOtherChartRelationship: Dispatch>>; readonly editingChartId: string | null; readonly setEditingChartId: Dispatch>; readonly synastryRelationshipType: SynastryRelationshipType; readonly setSynastryRelationshipType: Dispatch>; readonly synastryPendingId: string | null; readonly synastryReportCard: SynastryReportCard | null; readonly setSynastryReportCard: Dispatch>; readonly synastryHistory: SynastryReportCard[]; readonly activeChartId: string; readonly setActiveChartId: Dispatch>; readonly saveProfile: (event: FormEvent) => void; readonly draftSynastryQuestionFromChart: (record: ChartLibraryRecord, relationshipType: SynastryRelationshipType) => void; }; export function ChartLibraryPanel({ account, accountId, chartLibrary, setChartLibrary, profile, profileDraft, setProfileDraft, setProfile, profileSaving, profileNotice, setProfileNotice, setAccountError, editingSelfChart, setEditingSelfChart, otherProfileDraft, setOtherProfileDraft, otherChartRelationship, setOtherChartRelationship, editingChartId, setEditingChartId, synastryRelationshipType, setSynastryRelationshipType, synastryPendingId, synastryReportCard, setSynastryReportCard, synastryHistory, activeChartId, setActiveChartId, saveProfile, draftSynastryQuestionFromChart, }: ChartLibraryPanelProps) { async function saveOtherChart(event: FormEvent) { event.preventDefault(); const nextProfile = { ...otherProfileDraft, name: otherProfileDraft.name.trim(), chartRelationship: otherChartRelationship }; if (missingOtherProfileStep(nextProfile)) { setAccountError("请补全其他星盘的称呼、出生时间和出生地点。"); return; } if (!accountId) return; const record: ChartLibraryRecord = { id: editingChartId || globalThis.crypto.randomUUID(), role: "other", profile: nextProfile, relationship: otherChartRelationship, updatedAt: timestamp(), }; try { const saved = editingChartId ? await updateCloudChartProfile(record) : await saveCloudChartProfile(record); const selfProfile = account ? readProfile(account.profile) : profile; setChartLibrary((current) => { const others = editingChartId ? current.map((item) => item.id === saved.id ? saved : item) : [...current, saved]; return upsertSelfChart(others, selfProfile); }); setOtherProfileDraft(emptyProfile); setOtherChartRelationship("other"); setEditingChartId(null); setAccountError(""); setProfileNotice(editingChartId ? "已更新其他人的星盘资料。" : "已保存到云端星盘库。请选择关系类型后点击“用于合盘”。"); } catch { setProfileNotice("保存失败,请重试"); setAccountError(""); } } function editOtherChart(record: ChartLibraryRecord) { if (record.role !== "other") return; setOtherProfileDraft(record.profile); setOtherChartRelationship(record.relationship === "self" ? "other" : record.relationship); setEditingChartId(record.id); setAccountError(""); setProfileNotice(""); } async function deleteOtherChart(recordId: string) { if (!accountId || !window.confirm("确定删除这份其他人的星盘资料吗?删除后无法恢复。")) return; try { await deleteCloudChartProfile(recordId); } catch { setProfileNotice("删除失败,请重试"); setAccountError(""); return; } setChartLibrary((current) => { const next = current.filter((record) => record.id !== recordId || record.role === "self"); if (activeChartId === recordId) { setActiveChartId("self"); localStorage.setItem(activeChartStorageKey(accountId), "self"); } return next; }); setAccountError(""); setProfileNotice("已从云端星盘库删除。"); } function makeDefaultChart(record: ChartLibraryRecord) { if (record.role !== "other" || profileSaving || !accountId) return; setActiveChartId(record.id); localStorage.setItem(activeChartStorageKey(accountId), record.id); setProfile(record.profile); setProfileNotice("已设为当前使用资料,账户本人的出生资料未被覆盖。"); } return (
我的星盘当前账号的默认资料
{chartLibrary.filter((record) => record.role === "self").length}
{chartLibrary.filter((record) => record.role === "self").map((record) => (
{record.profile.name || "未命名"}本人当前默认
{record.profile.date || "出生日期待补全"} · {profileBirthTimeLabel(record.profile)} · {profilePlaceLabel(record.profile)} {profileBirthTimeStatusLabel(record.profile)} · {formatChartUpdatedAt(record.updatedAt)}
))} {editingSelfChart && (
编辑本人星盘这份资料会用于默认解盘与新对话。
{profileNotice &&

{profileNotice}

}
)} {chartLibrary.filter((record) => record.role === "self").length === 0 &&

请先在个人资料中补全你的出生资料。

}
其他人的星盘亲友、伴侣或客户资料
{chartLibrary.filter((record) => record.role === "other").length}
{chartLibrary.filter((record) => record.role === "other").length === 0 &&

还没有其他星盘,先添加一份资料。

} {chartLibrary.filter((record) => record.role === "other").map((record) => (
{record.profile.name || "未命名"}{chartRelationshipLabel(record.relationship)}
{record.profile.date || "出生日期待补全"} · {profileBirthTimeLabel(record.profile)} · {profilePlaceLabel(record.profile)} {profileBirthTimeStatusLabel(record.profile)} · {formatChartUpdatedAt(record.updatedAt)}
))}
{synastryReportCard && (
合盘结果摘要 {synastryReportCard.partnerName} Ashtakoot {synastryReportCard.score ?? "?"}/{synastryReportCard.maxScore ?? "?"} · {synastryReportCard.assessment || synastryReportCard.scoreBand || "待解释"}
{synastryReportCard.headline &&

{synastryReportCard.headline}

}
查看证据
    {(synastryReportCard.strengths || []).map((item) =>
  • {item}
  • )} {(synastryReportCard.risks || []).map((item) =>
  • {item}
  • )}
下一步证据:{(synastryReportCard.nextEvidence || []).join(" / ") || "双方 Dasha / UL-DK / D9 7宫"}
)} {synastryHistory.length > 0 && (
合盘历史 {synastryHistory.slice(0, 5).map((item) => ( ))}
)}
{editingChartId ? "编辑其他人的星盘" : "添加其他人的星盘"}用于合盘、亲友盘或客户盘。
{editingChartId && }
); }