import { environment } from '../environments/environment'; import { isHostedMount } from './hostedMount'; import { bareHttpsOrigin } from '../environments/demoOrigin'; // The public demo entrance and its role choices. Each role opens the configured staging origin on its own // physical mount, where /demo asks for the demo-only agreement first. A link carries the role and nothing else. export type DemoRole = 'patient' | 'admin' | 'desk' | 'clinician'; export interface DemoRoleChoice { role: DemoRole; label: string; /** The physical mount the role opens: the patient portal, or the staff portal. */ mount: 'frontend' | 'staff'; tries: [string, string, string]; } export const DEMO_ROLES: Record = { patient: { role: 'patient', label: 'Patient', mount: 'frontend', tries: ['Book a visit today', 'Message your care team', 'Fill in your intake form'], }, desk: { role: 'desk', label: 'Front desk', mount: 'staff', tries: ['Check in the patients booked today', 'Answer the front desk inbox', 'Book a visit for a patient'], }, clinician: { role: 'clinician', label: 'Clinician', mount: 'staff', tries: ['Open the patient being seen today', 'Write the visit note', 'See what the patient sent'], }, admin: { role: 'admin', label: 'Administrator', mount: 'staff', tries: ['Add a service', "Set a staff member's permissions", "Review the clinic's day"], }, }; export const DEMO_STAFF_ROLES: DemoRole[] = ['admin', 'desk', 'clinician']; export const DEMO_BANNER_TEXT = 'Fictional data only. Do not enter real patient or identifying information.'; /** The public demo entrance, used where a stack names no landing of its own. */ export const DEMO_ENTRANCE_URL = 'https://demo.akashic.health/'; /** Where Leave and an ended visit go: this stack's own demo entrance, the first landing it names. */ export const demoEntranceUrl = (landingOrigins: readonly string[] = environment.demoLandingOrigins): string => { const own = landingOrigins.map((origin) => bareHttpsOrigin(origin)).find(Boolean); return own ? own + '/' : DEMO_ENTRANCE_URL; }; /** The agreement step, on either physical mount. */ export const isDemoEntryPath = (pathname: string = window.location.pathname): boolean => /^\/(staff-portal\/)?demo\/?$/.test(pathname); /** * Which demo page, if any, this hosted address is: the entrance on a configured landing origin, or the * agreement on a demo path of the configured demo app origin. Undefined everywhere else, and always on a * WordPress page, so those mount the app. */ export const demoPageFor = ( location: { origin: string; pathname: string } = window.location, hosted: boolean = isHostedMount(), landingOrigins: readonly string[] = environment.demoLandingOrigins, appOrigin: string | null | undefined = demoAppOrigin(), production: boolean = environment.production, ): 'landing' | 'entry' | undefined => { // A production build serves no demo page, the landing included. if (!hosted || production) return undefined; if (landingOrigins.some((origin) => bareHttpsOrigin(origin) === location.origin)) return 'landing'; return !!appOrigin && location.origin === appOrigin && isDemoEntryPath(location.pathname) ? 'entry' : undefined; }; export const demoRoleFrom = (search: string = window.location.search): DemoRole | undefined => { const role = new URLSearchParams(search).get('role'); return role && Object.prototype.hasOwnProperty.call(DEMO_ROLES, role) ? (role as DemoRole) : undefined; }; /** * The configured demo origin, or undefined. Only a bare https origin is accepted - no path, query, fragment * or credentials - and never on a production stack, so a missing or odd value offers no way in. */ export const demoAppOrigin = ( configured: string | undefined = environment.demoAppOrigin, production: boolean = environment.production, ): string | undefined => { return !configured || production ? undefined : bareHttpsOrigin(configured); }; /** Where a role choice goes: its physical mount on the demo origin, with the role in the hash route. */ export const demoEntryUrl = (role: DemoRole, origin: string | undefined = demoAppOrigin()): string | undefined => { if (!origin) return undefined; const path = DEMO_ROLES[role].mount === 'staff' ? '/staff-portal/demo' : '/demo'; return origin + path + '#/demo?role=' + role; };