dba0245cb1
Server Actions + createStreamableValue kept failing in production; fetch-based text stream avoids RSC serialization issues and shows readable error messages. Co-authored-by: Cursor <cursoragent@cursor.com>
178 lines
4.9 KiB
TypeScript
178 lines
4.9 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { BrainCircuit } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import ResultAI from "@/components/result-ai";
|
|
import BaziChartDisplay from "@/components/modes/bazi-chart";
|
|
import DateTimePicker, { nowDateString } from "@/components/shared/datetime-picker";
|
|
import RegionSelect, {
|
|
useRegionLocation,
|
|
} from "@/components/shared/region-select";
|
|
import { calculateBazi, type BaziChart } from "@/lib/calc/bazi";
|
|
import { streamAiCompletion } from "@/lib/ai/client-stream";
|
|
|
|
export default function BaziForm() {
|
|
const [date, setDate] = useState(nowDateString());
|
|
const [time, setTime] = useState("12:00");
|
|
const [unknownHour, setUnknownHour] = useState(false);
|
|
const [gender, setGender] = useState<"male" | "female">("male");
|
|
const [provinceCode, setProvinceCode] = useState("");
|
|
const [cityCode, setCityCode] = useState("");
|
|
const [question, setQuestion] = useState("");
|
|
const [chart, setChart] = useState<BaziChart | null>(null);
|
|
const [completion, setCompletion] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [showAi, setShowAi] = useState(false);
|
|
|
|
const location = useRegionLocation(provinceCode, cityCode);
|
|
|
|
function buildInput() {
|
|
if (!location) {
|
|
return null;
|
|
}
|
|
return {
|
|
date,
|
|
time: unknownHour ? "12:00" : time,
|
|
gender,
|
|
longitude: location.longitude,
|
|
unknownHour,
|
|
};
|
|
}
|
|
|
|
function handleCalculate() {
|
|
const input = buildInput();
|
|
if (!input) {
|
|
setError("请选择出生地域");
|
|
return;
|
|
}
|
|
if (!question.trim()) {
|
|
setError("请输入问事");
|
|
return;
|
|
}
|
|
setError("");
|
|
setChart(calculateBazi(input));
|
|
setShowAi(false);
|
|
setCompletion("");
|
|
}
|
|
|
|
async function handleAnalyze() {
|
|
const input = buildInput();
|
|
if (!input || !chart) {
|
|
return;
|
|
}
|
|
setError("");
|
|
setCompletion("");
|
|
setIsLoading(true);
|
|
setShowAi(true);
|
|
try {
|
|
await streamAiCompletion(
|
|
{
|
|
mode: "bazi",
|
|
payload: {
|
|
input,
|
|
question,
|
|
birthPlaceName: location!.name,
|
|
},
|
|
},
|
|
setCompletion,
|
|
);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex flex-1 flex-col gap-4 px-4 py-6">
|
|
<div className="mx-auto w-full max-w-lg space-y-4">
|
|
<DateTimePicker
|
|
label="出生日期 / 时间"
|
|
date={date}
|
|
time={time}
|
|
timeDisabled={unknownHour}
|
|
onDateChange={setDate}
|
|
onTimeChange={setTime}
|
|
/>
|
|
<label className="flex items-center gap-2 text-xs text-muted-foreground">
|
|
<input
|
|
type="checkbox"
|
|
checked={unknownHour}
|
|
onChange={(e) => setUnknownHour(e.target.checked)}
|
|
/>
|
|
时辰不详
|
|
</label>
|
|
|
|
<div className="space-y-1">
|
|
<label className="text-sm font-medium">性别</label>
|
|
<div className="flex gap-4">
|
|
{(["male", "female"] as const).map((g) => (
|
|
<label key={g} className="flex items-center gap-2 text-sm">
|
|
<input
|
|
type="radio"
|
|
name="gender"
|
|
checked={gender === g}
|
|
onChange={() => setGender(g)}
|
|
/>
|
|
{g === "male" ? "男" : "女"}
|
|
</label>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
<RegionSelect
|
|
label="出生地域"
|
|
provinceCode={provinceCode}
|
|
cityCode={cityCode}
|
|
onProvinceChange={setProvinceCode}
|
|
onCityChange={setCityCode}
|
|
/>
|
|
|
|
<div className="space-y-1">
|
|
<label className="text-sm font-medium">问事</label>
|
|
<Textarea
|
|
placeholder="事业、婚姻、健康等..."
|
|
value={question}
|
|
onChange={(e) => setQuestion(e.target.value)}
|
|
rows={3}
|
|
/>
|
|
</div>
|
|
|
|
{error && !showAi && (
|
|
<p className="text-sm text-destructive">{error}</p>
|
|
)}
|
|
|
|
<Button onClick={handleCalculate} className="w-full">
|
|
排盘
|
|
</Button>
|
|
</div>
|
|
|
|
{chart && (
|
|
<div className="mx-auto w-full max-w-lg space-y-4">
|
|
<BaziChartDisplay chart={chart} />
|
|
{!showAi && (
|
|
<Button onClick={handleAnalyze} className="w-full">
|
|
<BrainCircuit size={16} className="mr-1" />
|
|
测算
|
|
</Button>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{showAi && (
|
|
<div className="mx-auto h-96 w-full max-w-lg flex-1">
|
|
<ResultAI
|
|
completion={completion}
|
|
isLoading={isLoading}
|
|
onCompletion={handleAnalyze}
|
|
error={error}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|