import type { Role } from '../core/store/AuthStore'; /** * Who sits at the editor (#428): somebody signed in, with their role, or a guest trying a demo * copy without an account. */ export interface Session { guest: boolean; role: Role; } /** * Everything the editor does that writes to the server or manages it. A guest's project lives in * the tab alone, so a guest does none of these; an account does all of them but the * administrators' own. */ export type SessionAction = | 'save' | 'upload' | 'deleteFile' | 'projects' | 'snapshots' | 'release' | 'password' | 'users' | 'demoCopy' | 'offerDemo'; const ADMINISTRATORS_ONLY: ReadonlySet = new Set(['users', 'demoCopy', 'offerDemo']); /** Whether `session` may do `action`. The server decides again; this is what the editor offers. */ export function sessionAllows(session: Session, action: SessionAction): boolean { if (session.guest) return false; return !ADMINISTRATORS_ONLY.has(action) || session.role === 'admin'; } /** What a guest is told when something would need the server. */ export const GUEST_REFUSAL = 'Guests keep nothing on the server – sign in to keep your work'; let current: Session = { guest: false, role: 'user' }; /** * The session of this page, for the places that are not handed the account: the pickers, a file * dropped on the editor, a finished recording. One page holds one session at a time. */ export function setSession(session: Session): void { current = session; } export function allows(action: SessionAction): boolean { return sessionAllows(current, action); }