Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : apps/social-web/src/session.tsx9 * Module : social-web10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Signed-in context: the user, role helpers, the brand list and the brand filter shared by the screens.43import { createContext, useContext, useMemo, useState, type ReactNode } from 'react';44import type { Role } from '@mls/contracts';45import { ROLE_RANK } from './consts.ts';46import { get, type Me } from '../../shared/api.ts';47import type { Brand } from './types.ts';48import { useLoad } from './ui/core.tsx';49import { DEFAULT_TZ } from './time.ts';5051export type Session = {52 me: Me; reloadMe: () => Promise<void>;53 /** True when the user's global role reaches `min`. Routes gated on a global role (admin, owner) follow this exactly. */54 atLeast: (min: Role) => boolean;55 /**56 * Brand-level rights (editor, approver) are decided per brand by the server; /v1/me does not list them.57 * Admins and owners hold every brand right. For the others the UI leaves the action available and shows the server's answer.58 */59 isAdmin: boolean; isOwner: boolean;60 brands: Brand[]; brandsError: unknown; brandsLoading: boolean; reloadBrands: () => void;61 /** Brand filter ('' = all brands). */62 brandId: string; setBrandId: (id: string) => void;63 brand: Brand | undefined; brandById: (id: string | null | undefined) => Brand | undefined;64 /** Display zone: the selected brand's, else the default. */65 tz: string;66};67const Ctx = createContext<Session | null>(null);6869export function SessionProvider({ me, reloadMe, children }: { me: Me; reloadMe: () => Promise<void>; children: ReactNode }) {70 const brands = useLoad(() => get<{ brands: Brand[] }>('/v1/brands'), []);71 const [brandId, setBrandId] = useState('');72 const value = useMemo<Session>(() => {73 const list = brands.data?.brands ?? [];74 const brand = list.find((b) => b.id === brandId);75 const rank = ROLE_RANK[me.user.role] ?? 0;76 return {77 me, reloadMe, atLeast: (min) => rank >= ROLE_RANK[min], isAdmin: rank >= ROLE_RANK.admin, isOwner: me.user.role === 'owner',78 brands: list, brandsError: brands.error, brandsLoading: brands.loading, reloadBrands: brands.reload,79 brandId: brand ? brandId : '', setBrandId, brand, brandById: (id) => list.find((b) => b.id === id), tz: brand?.timezone || DEFAULT_TZ,80 };81 }, [me, reloadMe, brands.data, brands.error, brands.loading, brands.reload, brandId]);82 return <Ctx.Provider value={value}>{children}</Ctx.Provider>;83}84export function useSession(): Session { const s = useContext(Ctx); if (!s) throw new Error('SessionProvider missing'); return s; }85/** Query-string suffix for the brand filter. */86export const brandQuery = (brandId: string, first = true) => (brandId ? `${first ? '?' : '&'}brandId=${encodeURIComponent(brandId)}` : '');87