Files
twenty/packages/twenty-front/src/modules/settings/admin-panel/components/SettingsAdminServerAdminAccess.tsx
T
Raphaël Bosi 8034c7725f Reorganize twenty-ui into best-practice component domains and per-component folders (#21745)
Reorganizes `twenty-ui`'s component organization to follow how the best
UI libraries (MUI, Mantine, Base UI, Polaris) structure their source,
now that the package has stabilized.

**Taxonomy** — dissolves the meaningless `components/` junk-drawer and
the 107-file `display/` mega-category. New domains/subpaths:
`data-display`, `typography`, `icon`, `surfaces`; `feedback` and
`layout` absorb the rest (banners/callout/info + placeholders →
feedback; modal/card → surfaces; motion + separators → layout).

**Per-component layout** — every component is now
`<domain>/<ComponentName>/<ComponentName>.tsx` with colocated
styles/stories/types, `internal/` for private helpers and `parts/` for
re-exported compound sub-parts. The redundant inner `/components/` is
gone. `icon` and `json-visualizer` are kept as cohesive subsystems.

**Also:** adds a tree-shakeable root barrel (`import { Button } from
'twenty-ui'`), the generator now owns `individual-entry.ts`, and a real
barrel-leak bug is fixed (private `internals/` parts were leaking into
the public API).

Consumer imports (~1.2k files) and the `twenty-sdk` UI aggregator were
updated by codemod. The change is **export-neutral** except 16
intentionally-removed private internals symbols (all verified
unconsumed). Gates green: typecheck, lint, build, size-limit, storybook.


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/21745?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
2026-06-18 10:31:29 +02:00

272 lines
9.2 KiB
TypeScript

import { useApolloAdminClient } from '@/settings/admin-panel/apollo/hooks/useApolloAdminClient';
import { TwoFactorAuthenticationVerificationCodeDash } from '@/settings/two-factor-authentication/components/TwoFactorAuthenticationVerificationCodeDash';
import { TwoFactorAuthenticationVerificationCodeSlot } from '@/settings/two-factor-authentication/components/TwoFactorAuthenticationVerificationCodeSlot';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown';
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { useCloseDropdown } from '@/ui/layout/dropdown/hooks/useCloseDropdown';
import { ConfirmationModal } from '@/ui/layout/modal/components/ConfirmationModal';
import { useModal } from '@/ui/layout/modal/hooks/useModal';
import { CombinedGraphQLErrors } from '@apollo/client/errors';
import { useMutation, useQuery } from '@apollo/client/react';
import { styled } from '@linaria/react';
import { t } from '@lingui/core/macro';
import { OTPInput } from 'input-otp';
import { useState } from 'react';
import { Status } from 'twenty-ui/data-display';
import { IconDotsVertical } from 'twenty-ui/icon';
import { LightIconButton } from 'twenty-ui/input';
import { MenuItem } from 'twenty-ui/navigation';
import { themeCssVariables } from 'twenty-ui/theme-constants';
import {
GetServerAdminsDocument,
UpdateServerAdminAccessDocument,
} from '~/generated-admin/graphql';
type ServerAdminAccessUpdate = {
canAccessFullAdminPanel?: boolean;
canImpersonate?: boolean;
};
type PendingServerAdminChange = {
description: string;
isRevoking: boolean;
update: ServerAdminAccessUpdate;
};
const SERVER_ADMIN_ACCESS_CONFIRMATION_MODAL_ID =
'server-admin-access-confirmation';
const StyledValue = styled.div`
align-items: center;
display: flex;
gap: ${themeCssVariables.spacing[1]};
`;
const StyledChips = styled.div`
align-items: center;
display: flex;
flex: 1;
gap: ${themeCssVariables.spacing[2]};
`;
const StyledNoAccess = styled.span`
color: ${themeCssVariables.font.color.tertiary};
flex: 1;
`;
const StyledConfirmationContent = styled.div`
align-items: center;
display: flex;
flex-direction: column;
gap: ${themeCssVariables.spacing[4]};
`;
const StyledOTPContainer = styled.div`
display: flex;
gap: ${themeCssVariables.spacing[1]};
`;
export const SettingsAdminServerAdminAccess = ({
userId,
userLabel,
}: {
userId: string;
userLabel: string;
}) => {
const dropdownId = `server-admin-access-${userId}`;
const apolloAdminClient = useApolloAdminClient();
const { openModal } = useModal();
const { closeDropdown } = useCloseDropdown();
const { enqueueSuccessSnackBar, enqueueErrorSnackBar } = useSnackBar();
const [pendingChange, setPendingChange] =
useState<PendingServerAdminChange | null>(null);
const [otp, setOtp] = useState('');
const { data, refetch } = useQuery(GetServerAdminsDocument, {
client: apolloAdminClient,
});
const [updateServerAdminAccess] = useMutation(
UpdateServerAdminAccessDocument,
{ client: apolloAdminClient },
);
const serverAdmins = data?.getServerAdmins ?? [];
const currentAccess = serverAdmins.find((admin) => admin.id === userId);
const canAccessFullAdminPanel =
currentAccess?.canAccessFullAdminPanel ?? false;
const canImpersonate = currentAccess?.canImpersonate ?? false;
const fullAdminCount = serverAdmins.filter(
(admin) => admin.canAccessFullAdminPanel,
).length;
const isLastFullAdmin = canAccessFullAdminPanel && fullAdminCount <= 1;
const hasAnyAccess = canAccessFullAdminPanel || canImpersonate;
const hasFullAccess = canAccessFullAdminPanel && canImpersonate;
const requestChange = (change: PendingServerAdminChange) => {
closeDropdown(dropdownId);
setOtp('');
setPendingChange(change);
openModal(SERVER_ADMIN_ACCESS_CONFIRMATION_MODAL_ID);
};
const handleConfirm = async () => {
if (pendingChange === null) {
return;
}
try {
await updateServerAdminAccess({
variables: {
userId,
otp: otp.length > 0 ? otp : undefined,
...pendingChange.update,
},
});
await refetch();
enqueueSuccessSnackBar({
message: t`Server administrator access updated.`,
});
} catch (error) {
enqueueErrorSnackBar({
...(CombinedGraphQLErrors.is(error)
? { apolloError: error }
: { message: t`Failed to update server administrator access.` }),
});
} finally {
setOtp('');
setPendingChange(null);
}
};
return (
<>
<StyledValue>
{hasAnyAccess ? (
<StyledChips>
{canAccessFullAdminPanel && (
<Status color="green" text={t`Admin panel`} weight="medium" />
)}
{canImpersonate && (
<Status color="blue" text={t`Impersonation`} weight="medium" />
)}
</StyledChips>
) : (
<StyledNoAccess>{t`No access`}</StyledNoAccess>
)}
<Dropdown
dropdownId={dropdownId}
dropdownPlacement="right-start"
clickableComponent={
<LightIconButton Icon={IconDotsVertical} accent="tertiary" />
}
dropdownComponents={
<DropdownContent>
<DropdownMenuItemsContainer>
<MenuItem
text={
canAccessFullAdminPanel
? t`Revoke admin panel access`
: t`Grant admin panel access`
}
disabled={isLastFullAdmin}
onClick={() =>
requestChange({
description: t`full admin panel access`,
isRevoking: canAccessFullAdminPanel,
update: {
canAccessFullAdminPanel: !canAccessFullAdminPanel,
},
})
}
/>
<MenuItem
text={
canImpersonate
? t`Disable impersonation`
: t`Enable impersonation`
}
onClick={() =>
requestChange({
description: t`impersonation`,
isRevoking: canImpersonate,
update: { canImpersonate: !canImpersonate },
})
}
/>
{!hasFullAccess && (
<MenuItem
text={t`Grant full access`}
onClick={() =>
requestChange({
description: t`full server access`,
isRevoking: false,
update: {
canAccessFullAdminPanel: true,
canImpersonate: true,
},
})
}
/>
)}
</DropdownMenuItemsContainer>
</DropdownContent>
}
/>
</StyledValue>
<ConfirmationModal
modalInstanceId={SERVER_ADMIN_ACCESS_CONFIRMATION_MODAL_ID}
title={pendingChange?.isRevoking ? t`Revoke access` : t`Grant access`}
confirmButtonAccent={pendingChange?.isRevoking ? 'danger' : 'blue'}
confirmButtonText={t`Confirm`}
onConfirmClick={handleConfirm}
onClose={() => {
setOtp('');
setPendingChange(null);
}}
subtitle={
<StyledConfirmationContent>
<div>
{pendingChange?.isRevoking
? t`This will revoke ${pendingChange?.description ?? ''} for ${userLabel}.`
: t`This will grant ${pendingChange?.description ?? ''} to ${userLabel}.`}
</div>
<div>{t`Enter your two-factor authentication code to confirm.`}</div>
<OTPInput
maxLength={6}
value={otp}
onChange={setOtp}
render={({ slots }) => (
<StyledOTPContainer>
{slots.slice(0, 3).map((slot, index) => (
<TwoFactorAuthenticationVerificationCodeSlot
key={index}
char={slot.char}
placeholderChar={slot.placeholderChar}
isActive={slot.isActive}
hasFakeCaret={slot.hasFakeCaret}
/>
))}
<TwoFactorAuthenticationVerificationCodeDash />
{slots.slice(3).map((slot, index) => (
<TwoFactorAuthenticationVerificationCodeSlot
key={index + 3}
char={slot.char}
placeholderChar={slot.placeholderChar}
isActive={slot.isActive}
hasFakeCaret={slot.hasFakeCaret}
/>
))}
</StyledOTPContainer>
)}
/>
</StyledConfirmationContent>
}
/>
</>
);
};