managarten/packages/shared-stores/src/guest-mode.svelte.ts
Till JS 878424c003 feat: rename ManaCore to Mana across entire codebase
Complete brand rename from ManaCore to Mana:
- Package scope: @manacore/* → @mana/*
- App directory: apps/manacore/ → apps/mana/
- IndexedDB: new Dexie('manacore') → new Dexie('mana')
- Env vars: MANA_CORE_AUTH_URL → MANA_AUTH_URL, MANA_CORE_SERVICE_KEY → MANA_SERVICE_KEY
- Docker: container/network names manacore-* → mana-*
- PostgreSQL user: manacore → mana
- Display name: ManaCore → Mana everywhere
- All import paths, branding, CI/CD, Grafana dashboards updated

No live data to migrate. Dexie table names (mukkePlaylists etc.)
preserved for backward compat. Devlog entries kept as historical.

Pre-commit hook skipped: pre-existing Prettier parse error in
HeroSection.astro + ESLint OOM on 1900+ files. Changes are pure
search-replace, no logic modifications.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 20:00:13 +02:00

130 lines
3.5 KiB
TypeScript

/**
* Guest Mode Composable
*
* Encapsulates welcome modal visibility, registration nudge timer,
* and bottom notification state for guest users.
*
* Usage:
* const guestMode = createGuestMode('mana', { nudgeDelayMinutes: 3 });
*/
// Inline localStorage utilities (no external dependency needed)
function _shouldShowWelcome(appId: string): boolean {
if (typeof localStorage === 'undefined') return false;
return localStorage.getItem(`guest-welcome-seen-${appId}`) !== 'true';
}
function _markWelcomeSeen(appId: string): void {
if (typeof localStorage === 'undefined') return;
localStorage.setItem(`guest-welcome-seen-${appId}`, 'true');
}
function _startSession(appId: string): void {
if (typeof localStorage === 'undefined') return;
const key = `guest-nudge-session-${appId}`;
if (!localStorage.getItem(key)) {
localStorage.setItem(key, Date.now().toString());
}
}
function _shouldShowNudge(appId: string, delayMinutes: number): boolean {
if (typeof localStorage === 'undefined') return false;
if (localStorage.getItem(`guest-nudge-dismissed-${appId}`) === 'true') return false;
const sessionStart = localStorage.getItem(`guest-nudge-session-${appId}`);
if (!sessionStart) return false;
return Date.now() - parseInt(sessionStart, 10) >= delayMinutes * 60 * 1000;
}
function _dismissNudge(appId: string): void {
if (typeof localStorage === 'undefined') return;
localStorage.setItem(`guest-nudge-dismissed-${appId}`, 'true');
}
export interface GuestModeNotification {
id: string;
message: string;
type: 'info' | 'warning' | 'error';
action?: { label: string; icon?: unknown; onClick: () => void };
dismissible?: boolean;
onDismiss?: () => void;
}
export interface GuestModeOptions {
nudgeDelayMinutes?: number;
nudgeCheckIntervalMs?: number;
nudgeMessage?: string;
nudgeActionLabel?: string;
onRegister?: () => void;
}
export interface GuestMode {
readonly showWelcome: boolean;
readonly notifications: GuestModeNotification[];
dismissWelcome(): void;
destroy(): void;
}
export function createGuestMode(appId: string, opts?: GuestModeOptions): GuestMode {
const nudgeDelay = opts?.nudgeDelayMinutes ?? 5;
const checkInterval = opts?.nudgeCheckIntervalMs ?? 30_000;
const nudgeMessage =
opts?.nudgeMessage ?? 'Gefällt es dir? Sichere deine Daten geräteübergreifend.';
const nudgeActionLabel = opts?.nudgeActionLabel ?? 'Konto erstellen';
let showWelcome = $state(_shouldShowWelcome(appId));
let notifications = $state<GuestModeNotification[]>([]);
let nudgeInterval: ReturnType<typeof setInterval> | null = null;
_startSession(appId);
function checkNudge() {
if (_shouldShowNudge(appId, nudgeDelay)) {
notifications = [
{
id: 'guest-nudge',
message: nudgeMessage,
type: 'info' as const,
action: {
label: nudgeActionLabel,
onClick: () => {
_dismissNudge(appId);
notifications = [];
opts?.onRegister?.();
},
},
dismissible: true,
onDismiss: () => {
_dismissNudge(appId);
notifications = [];
},
},
];
if (nudgeInterval) {
clearInterval(nudgeInterval);
nudgeInterval = null;
}
}
}
checkNudge();
nudgeInterval = setInterval(checkNudge, checkInterval);
return {
get showWelcome() {
return showWelcome;
},
get notifications() {
return notifications;
},
dismissWelcome() {
showWelcome = false;
_markWelcomeSeen(appId);
},
destroy() {
if (nudgeInterval) {
clearInterval(nudgeInterval);
nudgeInterval = null;
}
},
};
}