refactor(storage): use unstorage for session state

This commit is contained in:
2026-07-01 18:36:26 +08:00
parent 0251916a8a
commit ae6578923b
13 changed files with 398 additions and 246 deletions
+54 -75
View File
@@ -1,27 +1,38 @@
"use client";
const STORAGE_KEY = "cozsweet.chat.pendingUnlock";
import { z } from "zod";
import { StorageKeys } from "@/data/storage/storage_keys";
import { Result, SessionAsyncUtil } from "@/utils";
const MAX_AGE_MS = 30 * 60 * 1000;
export type PendingChatUnlockKind = "private" | "voice" | "image";
export type PendingChatUnlockStage = "auth" | "payment";
export interface PendingChatUnlock {
reason: "single_message_unlock";
messageId: string;
kind: PendingChatUnlockKind;
returnUrl: string;
stage: PendingChatUnlockStage;
createdAt: number;
}
const PendingChatUnlockSchema = z.object({
reason: z.literal("single_message_unlock"),
messageId: z.string().min(1),
kind: z.enum(["private", "voice", "image"]),
returnUrl: z
.string()
.min(1)
.refine(
(value) => value.startsWith("/") && !value.startsWith("//"),
"returnUrl must be an internal route",
),
stage: z.enum(["auth", "payment"]),
createdAt: z.number(),
});
export function savePendingChatUnlock(input: {
export type PendingChatUnlock = z.output<typeof PendingChatUnlockSchema>;
export async function savePendingChatUnlock(input: {
messageId: string;
kind: PendingChatUnlockKind;
returnUrl: string;
stage: PendingChatUnlockStage;
}): void {
if (typeof window === "undefined") return;
}): Promise<void> {
const payload: PendingChatUnlock = {
reason: "single_message_unlock",
messageId: input.messageId,
@@ -30,80 +41,48 @@ export function savePendingChatUnlock(input: {
stage: input.stage,
createdAt: Date.now(),
};
window.sessionStorage.setItem(STORAGE_KEY, JSON.stringify(payload));
await SessionAsyncUtil.setJson(
StorageKeys.pendingChatUnlock,
payload,
PendingChatUnlockSchema,
);
}
export function consumePendingChatUnlock(): PendingChatUnlock | null {
if (typeof window === "undefined") return null;
const raw = window.sessionStorage.getItem(STORAGE_KEY);
window.sessionStorage.removeItem(STORAGE_KEY);
if (!raw) return null;
return parsePendingChatUnlock(raw);
export async function consumePendingChatUnlock(): Promise<PendingChatUnlock | null> {
const result = await SessionAsyncUtil.getJson(
StorageKeys.pendingChatUnlock,
PendingChatUnlockSchema,
);
await SessionAsyncUtil.remove(StorageKeys.pendingChatUnlock);
if (Result.isErr(result)) return null;
return parsePendingChatUnlock(result.data);
}
export function peekPendingChatUnlock(): PendingChatUnlock | null {
if (typeof window === "undefined") return null;
const raw = window.sessionStorage.getItem(STORAGE_KEY);
if (!raw) return null;
const value = parsePendingChatUnlock(raw);
export async function peekPendingChatUnlock(): Promise<PendingChatUnlock | null> {
const result = await SessionAsyncUtil.getJson(
StorageKeys.pendingChatUnlock,
PendingChatUnlockSchema,
);
if (Result.isErr(result)) return null;
const value = parsePendingChatUnlock(result.data);
if (!value) {
window.sessionStorage.removeItem(STORAGE_KEY);
await SessionAsyncUtil.remove(StorageKeys.pendingChatUnlock);
}
return value;
}
export function hasPendingChatUnlock(): boolean {
return peekPendingChatUnlock() !== null;
export async function hasPendingChatUnlock(): Promise<boolean> {
return (await peekPendingChatUnlock()) !== null;
}
export function clearPendingChatUnlock(): void {
if (typeof window === "undefined") return;
window.sessionStorage.removeItem(STORAGE_KEY);
export async function clearPendingChatUnlock(): Promise<void> {
await SessionAsyncUtil.remove(StorageKeys.pendingChatUnlock);
}
function parsePendingChatUnlock(raw: string): PendingChatUnlock | null {
try {
const value = JSON.parse(raw) as Partial<PendingChatUnlock>;
if (value.reason !== "single_message_unlock") return null;
if (typeof value.messageId !== "string" || value.messageId.length === 0) {
return null;
}
if (!isPendingChatUnlockKind(value.kind)) return null;
if (!isPendingChatUnlockStage(value.stage)) return null;
if (typeof value.returnUrl !== "string" || value.returnUrl.length === 0) {
return null;
}
if (!value.returnUrl.startsWith("/") || value.returnUrl.startsWith("//")) {
return null;
}
if (typeof value.createdAt !== "number") return null;
if (Date.now() - value.createdAt > MAX_AGE_MS) return null;
return {
reason: value.reason,
messageId: value.messageId,
kind: value.kind,
returnUrl: value.returnUrl,
stage: value.stage,
createdAt: value.createdAt,
};
} catch {
return null;
}
}
function isPendingChatUnlockKind(
value: unknown,
): value is PendingChatUnlockKind {
return value === "private" || value === "voice" || value === "image";
}
function isPendingChatUnlockStage(
value: unknown,
): value is PendingChatUnlockStage {
return value === "auth" || value === "payment";
function parsePendingChatUnlock(
value: PendingChatUnlock | null,
): PendingChatUnlock | null {
if (!value) return null;
if (Date.now() - value.createdAt > MAX_AGE_MS) return null;
return value;
}