// CIL[BEGIN chunk_id="useWorkbenchRole_useworkbenchrole_1785096505" type="hook" identity="useWorkbenchRole" parent="none"] // CIL[DOC-MACRO purpose="Manage and persist the user's active workbench role"] // CIL[DOC-RESPONSIBILITY responsibility="Read the selected role from localStorage, keep it in sync across tabs, and provide setter helpers"] // CIL[DOC-CONTEXT module="workbench.hooks" layer="frontend-hook" related_files="[\"custom/apps/workbench/components/WorkbenchSidebar.tsx\",\"custom/apps/workbench/components/work/TeamSwitcher.tsx\"]"] // CIL[DOC-CONTRACT inputs="none" outputs="{ role, selectRole, roles } plus helpers getWorkbenchRole/setWorkbenchRole" errors="none" side_effects="reads/writes localStorage, listens to storage events"] // CIL[DOC-MICRO logic_summary="Provides WORKBENCH_ROLES constant, localStorage helpers, and a React hook that syncs role across browser tabs" notable_decisions="[]"] // CIL[DOC-CONFIDENCE pass="first" score="0.90" flags="none"] // CIL[DOC-STABILITY stability="stable"] // CIL[DOC-AUDIENCE audience="developer"] import { useEffect, useState } from 'react' import { useLocation, useNavigate } from 'react-router-dom' import { useAppPath } from '@core/hooks/useAppPath' const STORAGE_KEY = 'workbench:selected-role' const DEFAULT_ROLE = 'engineering' export const WORKBENCH_ROLES = [ 'product', 'engineering', 'devops', 'qa', 'delivery', 'docs', 'support', 'sales', 'management', 'proserve', ] const ROLE_PATH_RE = new RegExp(`^/workbench/work/(${WORKBENCH_ROLES.join('|')})(?:/|$)`) export function getWorkbenchRole(): string { if (typeof window === 'undefined') return DEFAULT_ROLE const saved = localStorage.getItem(STORAGE_KEY) return saved && WORKBENCH_ROLES.includes(saved) ? saved : DEFAULT_ROLE } export function setWorkbenchRole(role: string) { if (typeof window === 'undefined') return localStorage.setItem(STORAGE_KEY, role) } export function useWorkbenchRole() { const navigate = useNavigate() const location = useLocation() const appPath = useAppPath() const [storedRole, setStoredRole] = useState(getWorkbenchRole()) const urlRoleMatch = location.pathname.match(ROLE_PATH_RE) const urlRole = urlRoleMatch ? urlRoleMatch[1] : null const role = urlRole || storedRole useEffect(() => { const handleStorage = (e: StorageEvent) => { if (e.key === STORAGE_KEY) { setStoredRole(e.newValue || DEFAULT_ROLE) } } window.addEventListener('storage', handleStorage) return () => window.removeEventListener('storage', handleStorage) }, []) const selectRole = (next: string) => { setWorkbenchRole(next) setStoredRole(next) const current = location.pathname const restMatch = current.match(ROLE_PATH_RE) if (restMatch) { const rest = current.slice(`/workbench/work/${restMatch[1]}`.length) navigate(appPath(`/work/${next}${rest}`)) } } return { role, selectRole, roles: WORKBENCH_ROLES, setStoredRole } } // CIL[END chunk_id="useWorkbenchRole_useworkbenchrole_1785096505"]