Files
cozsweet-frontend-nextjs/src/lib/commercial/tip_offer_session.ts
T
Codex abb6e4235f
Docker Image / Quality and Bundle Budgets (push) Successful in 3s
Docker Image / Build and Push Docker Image (push) Successful in 1m52s
Add chat gifts and relationship diary UI
2026-07-21 18:35:01 +08:00

40 lines
1.1 KiB
TypeScript

import type { TipOfferActionRequest } from "@/data/schemas/chat";
import { chatApi } from "@/data/services/api/chat_api";
const SESSION_KEY_PREFIX = "cozsweet.tipOffer.session";
export function getTipOfferSessionId(characterId: string): string {
const key = `${SESSION_KEY_PREFIX}.${characterId}`;
try {
const existing = globalThis.sessionStorage.getItem(key);
if (existing) return existing;
const created = createSessionId();
globalThis.sessionStorage.setItem(key, created);
return created;
} catch {
return createSessionId();
}
}
export function recordTipOfferAction(
characterId: string,
action: TipOfferActionRequest["action"],
triggerSource: TipOfferActionRequest["triggerSource"],
): void {
void chatApi
.recordTipOfferAction({
characterId,
sessionId: getTipOfferSessionId(characterId),
action,
triggerSource,
})
.catch(() => undefined);
}
function createSessionId(): string {
if (typeof globalThis.crypto?.randomUUID === "function") {
return globalThis.crypto.randomUUID();
}
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 14)}`;
}