Closes #5383 ## Light theme <img width="905" alt="image" src="https://github.com/twentyhq/twenty/assets/3098428/ab0683c5-ded3-420c-ace6-684d38794a2d"> ## Dark theme <img width="903" alt="image" src="https://github.com/twentyhq/twenty/assets/3098428/4e43ca35-438d-4ba0-8388-1f061c6ccfb0">
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { useRecoilState } from 'recoil';
|
|
|
|
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
|
|
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
|
|
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
|
import { Toggle } from '@/ui/input/components/Toggle';
|
|
import { useUpdateWorkspaceMutation } from '~/generated/graphql';
|
|
|
|
export const ToggleImpersonate = () => {
|
|
const { enqueueSnackBar } = useSnackBar();
|
|
|
|
const [currentWorkspace, setCurrentWorkspace] = useRecoilState(
|
|
currentWorkspaceState,
|
|
);
|
|
|
|
const [updateWorkspace] = useUpdateWorkspaceMutation();
|
|
|
|
const handleChange = async (value: boolean) => {
|
|
try {
|
|
if (!currentWorkspace?.id) {
|
|
throw new Error('User is not logged in');
|
|
}
|
|
await updateWorkspace({
|
|
variables: {
|
|
input: {
|
|
allowImpersonation: value,
|
|
},
|
|
},
|
|
});
|
|
setCurrentWorkspace({
|
|
...currentWorkspace,
|
|
allowImpersonation: value,
|
|
});
|
|
} catch (err: any) {
|
|
enqueueSnackBar(err?.message, {
|
|
variant: SnackBarVariant.Error,
|
|
});
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Toggle
|
|
value={currentWorkspace?.allowImpersonation}
|
|
onChange={handleChange}
|
|
/>
|
|
);
|
|
};
|