import { useEffect } from 'react'; import { CustomSelectControl } from "../ui/custom-select-control"; import type { CustomSelectOption } from "../ui/custom-select-control"; import { Controller, FormProvider, useForm } from 'react-hook-form'; import { Flex } from '../ui/flex'; import { Spacer } from '../ui/spacer'; import { useAuthServiceContext } from '../../services'; import { type StaffWorkspace, getWorkspaceDisplay, } from '../../utils/staffRoleCatalog.ts'; interface WorkspaceSwitcherFormValues { workspace: StaffWorkspace | ''; } interface IRoleSwitcherProps { onSubmit: (data: { workspace: StaffWorkspace }) => void; isProfilePage?: boolean; } type WorkspaceSelectOption = CustomSelectOption & { value: StaffWorkspace | null; }; const RoleSwitcher = ({ onSubmit, isProfilePage = false }: IRoleSwitcherProps) => { const { availableWorkspaces, selectedWorkspace } = useAuthServiceContext(); const methods = useForm({ defaultValues: { workspace: selectedWorkspace || '', }, mode: 'onChange', }); const { control, handleSubmit, formState, reset } = methods; useEffect(() => { reset({ workspace: selectedWorkspace || '' }); }, [reset, selectedWorkspace]); const options: WorkspaceSelectOption[] = [ { key: '', name: 'Select a workspace...', value: null }, ...availableWorkspaces.map((workspace) => ({ key: workspace, name: getWorkspaceDisplay(workspace), value: workspace, })), ]; const submitWorkspace = ({ workspace }: WorkspaceSwitcherFormValues) => { if (!workspace) { return; } onSubmit({ workspace }); }; return (
{ const currentValue = options.find( (option) => option.value === field.value, ) || options[0]; return ( { const selectedOption = newValue.selectedItem as WorkspaceSelectOption; field.onChange(selectedOption.value || ''); }} /> ); }} />
); }; export default RoleSwitcher;