// CIL[BEGIN chunk_id="TeamSwitcher_teamswitcher_1784761401" type="function" identity="TeamSwitcher" parent="none"] // CIL[DOC-MACRO purpose="Render a team/role selector dropdown from the user's roles"] // CIL[DOC-RESPONSIBILITY responsibility="Provide a Select control that filters work items by inferred team"] // CIL[DOC-CONTEXT module="workbench.components.work" layer="frontend-component" related_files="[\"custom/apps/workbench/hooks/useWorkbenchRole.ts\",\"custom/apps/workbench/components/WorkbenchSidebar.tsx\"]"] // CIL[DOC-CONTRACT inputs="value:string, onChange:(value:string)=>void, label?:string" outputs="JSX team selector" errors="none" side_effects="calls /api/types to load permissions"] // CIL[DOC-MICRO logic_summary="Derives team options from roles, fetches workbench type permissions, and renders a shadcn Select" notable_decisions="[]"] // CIL[DOC-CONFIDENCE pass="first" score="0.90" flags="none"] // CIL[DOC-STABILITY stability="stable"] // CIL[DOC-AUDIENCE audience="developer"] import { useEffect, useMemo, useState } from 'react' import { useAuth } from '@core/contexts/AuthContext' import { apiFetch } from '@core/lib/api' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@core/components/ui/select' function teamFromRole(role: string): string { const withoutPrefix = role.replace(/^role_/, '') return withoutPrefix.split('_')[0] || withoutPrefix } function normalizeTeams(keys: string[]): { value: string; label: string }[] { const prefixes = keys.filter(k => k !== 'system_admin').map(teamFromRole).filter(Boolean) const unique = Array.from(new Set([...prefixes, '*'])) const options = unique.map(value => ({ value, label: value === '*' ? 'All Teams' : value.charAt(0).toUpperCase() + value.slice(1), })) return options.sort((a, b) => { if (a.value === '*') return 1 if (b.value === '*') return -1 return a.label.localeCompare(b.label) }) } export function inferDefaultTeam(roles: string[] = []): string { if (!roles.length) return '*' return teamFromRole(roles[0]) } interface TeamSwitcherProps { value: string onChange: (value: string) => void label?: string } export function TeamSwitcher({ value, onChange, label = 'Team' }: TeamSwitcherProps) { const { user } = useAuth() const [teams, setTeams] = useState<{ value: string; label: string }[]>(() => normalizeTeams(user?.roles || []) ) useEffect(() => { let cancelled = false apiFetch('/api/types?action=get&slug=workbench_work_item') .then(r => (r.ok ? r.json() : null)) .then(json => { if (cancelled || !json?.data?.design_schema?.record_permissions) return const keys = Object.keys(json.data.design_schema.record_permissions) setTeams(normalizeTeams(keys)) }) .catch(() => { // Fallback to user roles already in state }) return () => { cancelled = true } }, []) const valueExists = useMemo(() => teams.some(t => t.value === value), [teams, value]) return (