Merge branch 'dev' into test

This commit is contained in:
2026-06-22 18:24:32 +08:00
7 changed files with 78 additions and 17 deletions
+22 -2
View File
@@ -1,12 +1,32 @@
import { Suspense } from "react"; import { Suspense } from "react";
import { MobileShell } from "@/app/_components/core";
import { SubscriptionPageClient } from "./subscription-page-client"; import { SubscriptionPageClient } from "./subscription-page-client";
import { SubscriptionScreen } from "./subscription-screen";
export default function SubscriptionPage() { export default function SubscriptionPage() {
return ( return (
<Suspense fallback={<SubscriptionScreen subscriptionType="vip" />}> <Suspense fallback={<SubscriptionFallback />}>
<SubscriptionPageClient /> <SubscriptionPageClient />
</Suspense> </Suspense>
); );
} }
function SubscriptionFallback() {
return (
<MobileShell>
<div
style={{
minHeight: "60vh",
display: "flex",
alignItems: "center",
justifyContent: "center",
padding: "2rem",
color: "#666",
}}
>
Loading subscription...
</div>
</MobileShell>
);
}
+5 -2
View File
@@ -10,8 +10,11 @@ import { ROUTES } from "@/router/routes";
type ReturnStatus = "loading" | "missing"; type ReturnStatus = "loading" | "missing";
function buildSubscriptionUrl(subscriptionType: "vip" | "voice"): string { function buildSubscriptionUrl(subscriptionType: "vip" | "voice"): string {
if (subscriptionType === "voice") return `${ROUTES.subscription}?type=voice`; const params = new URLSearchParams({
return `${ROUTES.subscription}?type=vip`; type: subscriptionType,
paymentReturn: "1",
});
return `${ROUTES.subscription}?${params.toString()}`;
} }
export default function SubscriptionReturnPage() { export default function SubscriptionReturnPage() {
@@ -14,6 +14,12 @@ function toSubscriptionType(value: string | null): SubscriptionType {
export function SubscriptionPageClient() { export function SubscriptionPageClient() {
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const subscriptionType = toSubscriptionType(searchParams.get("type")); const subscriptionType = toSubscriptionType(searchParams.get("type"));
const shouldResumePendingOrder = searchParams.get("paymentReturn") === "1";
return <SubscriptionScreen subscriptionType={subscriptionType} />; return (
<SubscriptionScreen
subscriptionType={subscriptionType}
shouldResumePendingOrder={shouldResumePendingOrder}
/>
);
} }
+30 -4
View File
@@ -113,10 +113,12 @@ function toPlanView(
export interface SubscriptionScreenProps { export interface SubscriptionScreenProps {
subscriptionType?: SubscriptionType; subscriptionType?: SubscriptionType;
shouldResumePendingOrder?: boolean;
} }
export function SubscriptionScreen({ export function SubscriptionScreen({
subscriptionType = "vip", subscriptionType = "vip",
shouldResumePendingOrder = false,
}: SubscriptionScreenProps) { }: SubscriptionScreenProps) {
const user = useUserState(); const user = useUserState();
const userDispatch = useUserDispatch(); const userDispatch = useUserDispatch();
@@ -139,15 +141,31 @@ export function SubscriptionScreen({
}, [payment.currentOrderId, payment.isPaid, userDispatch]); }, [payment.currentOrderId, payment.isPaid, userDispatch]);
useEffect(() => { useEffect(() => {
if (payment.status !== "ready") return; const canInspectPendingOrder =
payment.status === "ready" ||
(!shouldResumePendingOrder &&
(payment.isPollingOrder || payment.isPaid || payment.status === "failed"));
if (!canInspectPendingOrder) return;
let cancelled = false; let cancelled = false;
const resumePendingOrder = async () => { const handlePendingOrder = async () => {
const result = await PendingPaymentOrderStorage.getPendingOrderForType( const result = await PendingPaymentOrderStorage.getPendingOrderForType(
subscriptionType, subscriptionType,
); );
if (cancelled || !result.success || result.data === null) return; if (cancelled || !result.success || result.data === null) return;
if (!shouldResumePendingOrder) {
await PendingPaymentOrderStorage.clearPendingOrder();
if (
payment.currentOrderId === result.data.orderId &&
(payment.isPollingOrder || payment.isPaid || payment.status === "failed")
) {
paymentDispatch({ type: "PaymentReset" });
}
return;
}
if (payment.currentOrderId === result.data.orderId) return; if (payment.currentOrderId === result.data.orderId) return;
if (resumedPendingOrderRef.current === result.data.orderId) return; if (resumedPendingOrderRef.current === result.data.orderId) return;
@@ -158,11 +176,19 @@ export function SubscriptionScreen({
}); });
}; };
void resumePendingOrder(); void handlePendingOrder();
return () => { return () => {
cancelled = true; cancelled = true;
}; };
}, [payment.currentOrderId, payment.status, paymentDispatch, subscriptionType]); }, [
payment.currentOrderId,
payment.isPaid,
payment.isPollingOrder,
payment.status,
paymentDispatch,
shouldResumePendingOrder,
subscriptionType,
]);
useEffect(() => { useEffect(() => {
if (!payment.currentOrderId) return; if (!payment.currentOrderId) return;
+1 -1
View File
@@ -10,7 +10,7 @@ import { z } from "zod";
export const UiMessageSchema = z.object({ export const UiMessageSchema = z.object({
content: z.string(), content: z.string(),
isFromAI: z.boolean(), isFromAI: z.boolean(),
/** 显示用时间戳(HH:mm */ /** 日期分隔条使用的本地日期(YYYY-MM-DD */
date: z.string(), date: z.string(),
/** 图片 URLbase64 data URL 或 http URL */ /** 图片 URLbase64 data URL 或 http URL */
imageUrl: z.string().optional(), imageUrl: z.string().optional(),
+10 -4
View File
@@ -25,7 +25,7 @@ import { chatRepository } from "@/data/repositories/chat_repository";
import type { IChatRepository } from "@/data/repositories/interfaces"; import type { IChatRepository } from "@/data/repositories/interfaces";
import { ChatStorage } from "@/data/storage/chat/chat_storage"; import { ChatStorage } from "@/data/storage/chat/chat_storage";
import { ChatSendResponse } from "@/data/dto/chat/chat_send_response"; import { ChatSendResponse } from "@/data/dto/chat/chat_send_response";
import { formatDate, Result, Logger } from "@/utils"; import { todayString, Result, Logger } from "@/utils";
import type { ChatState } from "./chat-state"; import type { ChatState } from "./chat-state";
@@ -61,10 +61,16 @@ export function localMessagesToUi(
return records.map((m) => ({ return records.map((m) => ({
content: m.content, content: m.content,
isFromAI: m.role === "assistant", isFromAI: m.role === "assistant",
date: m.createdAt, date: messageDateFromCreatedAt(m.createdAt),
})); }));
} }
function messageDateFromCreatedAt(createdAt: string): string {
const parsed = new Date(createdAt);
if (Number.isNaN(parsed.getTime())) return todayString();
return todayString(parsed);
}
/** /**
* ChatSendResponse → UiMessage(纯函数) * ChatSendResponse → UiMessage(纯函数)
* - 业务事实:后端响应就是 AI 的回复 * - 业务事实:后端响应就是 AI 的回复
@@ -74,7 +80,7 @@ export function sendResponseToUiMessage(response: ChatSendResponse): UiMessage {
return { return {
content: response.reply, content: response.reply,
isFromAI: true, isFromAI: true,
date: formatDate(new Date(response.timestamp)), date: todayString(new Date(response.timestamp)),
}; };
} }
@@ -201,7 +207,7 @@ export async function readAndSyncHistory(): Promise<{
"there were things I couldn't say there. " + "there were things I couldn't say there. " +
"Finally I can relax. How was your day out?", "Finally I can relax. How was your day out?",
isFromAI: true, isFromAI: true,
date: formatDate(), date: todayString(),
}; };
// 1. 读 local // 1. 读 local
+3 -3
View File
@@ -38,7 +38,7 @@
import { setup, assign } from "xstate"; import { setup, assign } from "xstate";
import { ChatStorage } from "@/data/storage/chat/chat_storage"; import { ChatStorage } from "@/data/storage/chat/chat_storage";
import { formatDate, todayString, Logger } from "@/utils"; import { todayString, Logger } from "@/utils";
import { ChatState, initialState } from "./chat-state"; import { ChatState, initialState } from "./chat-state";
import type { ChatEvent } from "./chat-events"; import type { ChatEvent } from "./chat-events";
@@ -229,7 +229,7 @@ export const chatMachine = setup({
messages.push({ messages.push({
content: event.text, content: event.text,
isFromAI: true, isFromAI: true,
date: formatDate(), date: todayString(),
}); });
} else { } else {
const last = messages[messages.length - 1]; const last = messages[messages.length - 1];
@@ -255,7 +255,7 @@ export const chatMachine = setup({
{ {
content: "Something went wrong. Try sending again?", content: "Something went wrong. Try sending again?",
isFromAI: true, isFromAI: true,
date: formatDate(), date: todayString(),
}, },
]; ];
return { messages, isReplyingAI: false }; return { messages, isReplyingAI: false };