40 lines
1.1 KiB
TypeScript
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)}`;
|
|
}
|