nxtgauge-frontend-solid/src/lib/guided-tour.ts

50 lines
1.4 KiB
TypeScript

export type GuidedTourKind = 'welcome' | 'role-approved';
export const WELCOME_TOUR_VALUE = '1';
export function normalizeRoleForTour(role: string | null | undefined): string {
return String(role ?? '')
.trim()
.toUpperCase()
.replace(/[\s-]+/g, '_');
}
export function getWelcomeTourStorageKey(userId: string): string {
return `nxtgauge_tour_welcome_seen_${userId}`;
}
export function getRoleTourStorageKey(userId: string): string {
return `nxtgauge_tour_roles_seen_${userId}`;
}
export function readSeenRoleTours(raw: string | null | undefined): Set<string> {
if (!raw) return new Set();
return new Set(
raw
.split(',')
.map((item) => normalizeRoleForTour(item))
.filter(Boolean),
);
}
export function writeSeenRoleTours(roles: Iterable<string>): string {
const normalized = Array.from(roles)
.map((role) => normalizeRoleForTour(role))
.filter(Boolean);
return Array.from(new Set(normalized)).join(',');
}
export function pickGuidedTour(params: {
userId: string | null | undefined;
activeRole: string | null | undefined;
welcomeTourSeen: boolean;
seenRoleTours: Set<string>;
}): GuidedTourKind | null {
if (!params.userId) return null;
if (!params.welcomeTourSeen) return 'welcome';
const activeRole = normalizeRoleForTour(params.activeRole);
if (!activeRole || activeRole === 'USER') return null;
if (params.seenRoleTours.has(activeRole)) return null;
return 'role-approved';
}