Files
han-app/codebase/backend/frontend-test-site/src/pending-intent.ts
T

51 lines
1.2 KiB
TypeScript

export type PendingTextIntent = {
text: string;
dialogKey: string;
messageKey: string;
};
const STORAGE_KEY = "han.pending-message";
export function createPendingTextIntent(text: string): PendingTextIntent {
return {
text,
dialogKey: crypto.randomUUID(),
messageKey: crypto.randomUUID(),
};
}
export function savePendingTextIntent(intent: PendingTextIntent) {
if (typeof window !== "undefined") {
window.sessionStorage.setItem(STORAGE_KEY, JSON.stringify(intent));
}
}
export function loadPendingTextIntent(): PendingTextIntent | null {
if (typeof window === "undefined") return null;
const raw = window.sessionStorage.getItem(STORAGE_KEY);
if (!raw) return null;
try {
const value = JSON.parse(raw) as Partial<PendingTextIntent>;
if (
typeof value.text !== "string"
|| typeof value.dialogKey !== "string"
|| typeof value.messageKey !== "string"
) {
return null;
}
return {
text: value.text,
dialogKey: value.dialogKey,
messageKey: value.messageKey,
};
} catch {
return null;
}
}
export function clearPendingTextIntent() {
if (typeof window !== "undefined") {
window.sessionStorage.removeItem(STORAGE_KEY);
}
}