Skip to content

Commit 885fc0f

Browse files
feat(sdk): put the delete check behind enableDeleteCheck (#2050)
1 parent f38ac11 commit 885fc0f

5 files changed

Lines changed: 88 additions & 12 deletions

File tree

‎web/apps/client-demo/src/config/frontier.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FrontierClientOptions } from '@raystack/frontier/client';
22

33
const config: FrontierClientOptions = {
44
endpoint: '/api',
5+
enableDeleteCheck: true,
56
billing: {
67
basePlan: {
78
title: 'Standard Plan'

‎web/sdk/client/components/delete-user-dialog/delete-user-dialog.tsx‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function DeleteUserDialog({
6161
handle,
6262
onDeleted
6363
}: DeleteUserDialogProps) {
64+
const { config } = useFrontier();
6465
const [dialogHandle] = useState(() =>
6566
AlertDialog.createHandle<DeleteUserPayload>()
6667
);
@@ -84,8 +85,12 @@ export function DeleteUserDialog({
8485
}, [checkCurrentUserDelete, dialogHandle]);
8586

8687
const startDelete = useCallback(() => {
88+
if (!config.enableDeleteCheck) {
89+
dialogHandle.openWithPayload({ step: 'confirm' });
90+
return;
91+
}
8792
void checkAndOpen();
88-
}, [checkAndOpen]);
93+
}, [config.enableDeleteCheck, dialogHandle, checkAndOpen]);
8994

9095
useEffect(() => {
9196
if (!handle) return;
@@ -96,13 +101,17 @@ export function DeleteUserDialog({
96101
}, [handle, startDelete]);
97102

98103
async function onDeleteRefused() {
99-
if (await checkAndOpen()) {
100-
toastManager.add({
101-
title: 'Unable to delete account',
102-
description: GENERIC_DELETE_BLOCKED_MESSAGE,
103-
type: 'error'
104-
});
104+
if (config.enableDeleteCheck) {
105+
// The check opens the blocked step. Toast only when it finds nothing.
106+
if (!(await checkAndOpen())) return;
107+
} else {
108+
dialogHandle.close();
105109
}
110+
toastManager.add({
111+
title: 'Unable to delete account',
112+
description: GENERIC_DELETE_BLOCKED_MESSAGE,
113+
type: 'error'
114+
});
106115
}
107116

108117
return (

‎web/sdk/client/hooks/useDeleteCheck.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
toDeleteBlockers
1111
} from '../utils/delete-blockers';
1212
import { useTerminology } from './useTerminology';
13+
import { useFrontier } from '../contexts/FrontierContext';
1314
import { handleConnectError } from '~/utils/error';
1415

1516
export interface UseDeleteCheckOptions<TPayload> {
@@ -28,6 +29,7 @@ export function useDeleteCheck<TPayload>({
2829
blockedDialogHandle
2930
}: UseDeleteCheckOptions<TPayload>) {
3031
const t = useTerminology();
32+
const { config } = useFrontier();
3133
const [isChecking, setIsChecking] = useState(false);
3234

3335
function showBlocked(blockers: DeleteBlocker[]) {
@@ -79,6 +81,10 @@ export function useDeleteCheck<TPayload>({
7981
}
8082

8183
async function startDelete(payload: TPayload) {
84+
if (!config.enableDeleteCheck) {
85+
confirmDialogHandle.openWithPayload(payload);
86+
return;
87+
}
8288
setIsChecking(true);
8389
try {
8490
const { canDelete, blockers } = await check(payload);

‎web/sdk/client/views/general/general-view.tsx‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { create } from '@bufbuild/protobuf';
88
import {
99
createConnectQueryKey,
1010
useMutation,
11+
useQuery,
1112
useTransport
1213
} from '@connectrpc/connect-query';
1314
import { useQueryClient } from '@tanstack/react-query';
@@ -31,6 +32,7 @@ import { useFrontier } from '../../contexts/FrontierContext';
3132
import { usePermissions } from '../../hooks/usePermissions';
3233
import { useTerminology } from '../../hooks/useTerminology';
3334
import { useDeleteCheck } from '../../hooks/useDeleteCheck';
35+
import { blockerLines, toDeleteBlockers } from '../../utils/delete-blockers';
3436
import { PERMISSIONS, shouldShowComponent } from '../../../utils';
3537
import { AuthTooltipMessage } from '../../utils';
3638
import { ViewContainer } from '../../components/view-container';
@@ -62,7 +64,9 @@ export function GeneralView({ onDeleteSuccess, urlPrefix }: GeneralViewProps = {
6264
const {
6365
activeOrganization: organization,
6466
isActiveOrganizationLoading,
65-
setActiveOrganization
67+
setActiveOrganization,
68+
config,
69+
basePlan
6670
} = useFrontier();
6771
const queryClient = useQueryClient();
6872
const transport = useTransport();
@@ -131,6 +135,40 @@ export function GeneralView({ onDeleteSuccess, urlPrefix }: GeneralViewProps = {
131135
blockedDialogHandle: deleteBlockedDialogHandle
132136
});
133137

138+
// Without enableDeleteCheck, check on load instead. While the answer is
139+
// loading, the delete button stays disabled. If this check itself fails,
140+
// the button stays enabled: the server still refuses a blocked delete.
141+
const { data: loadDeleteCheck, isLoading: isLoadDeleteCheckLoading } =
142+
useQuery(
143+
FrontierServiceQueries.checkOrganizationDelete,
144+
create(CheckOrganizationDeleteRequestSchema, {
145+
id: organization?.id ?? ''
146+
}),
147+
{
148+
enabled:
149+
!config.enableDeleteCheck && canDeleteWorkspace && !!organization?.id,
150+
retry: false
151+
}
152+
);
153+
// A disabled query keeps its last data, so a result fetched before the
154+
// option turned on must not block the click check.
155+
const isDeleteBlocked =
156+
!config.enableDeleteCheck &&
157+
!!loadDeleteCheck &&
158+
!loadDeleteCheck.canDelete;
159+
const deleteBlockerLines = useMemo(
160+
() =>
161+
blockerLines(
162+
toDeleteBlockers(loadDeleteCheck?.blockers ?? []),
163+
{},
164+
{
165+
organizationRef: `this ${t.organization({ case: 'lower' })}`,
166+
basePlanTitle: basePlan?.title
167+
}
168+
),
169+
[loadDeleteCheck, t, basePlan]
170+
);
171+
134172
// Update organization form
135173
const { mutateAsync: updateOrganization } = useMutation(
136174
FrontierServiceQueries.updateOrganization,
@@ -312,24 +350,37 @@ export function GeneralView({ onDeleteSuccess, urlPrefix }: GeneralViewProps = {
312350
</Text>
313351
<Tooltip>
314352
<Tooltip.Trigger
315-
disabled={canDeleteWorkspace}
353+
disabled={canDeleteWorkspace && !isDeleteBlocked}
316354
render={<span className={styles.fitContent} />}
317355
>
318356
<Button
319357
variant="solid"
320358
color="danger"
321359
onClick={() => startDelete(null)}
322-
disabled={!canDeleteWorkspace || isCheckingDelete}
360+
disabled={
361+
!canDeleteWorkspace ||
362+
isCheckingDelete ||
363+
isDeleteBlocked ||
364+
isLoadDeleteCheckLoading
365+
}
323366
loading={isCheckingDelete}
324367
loaderText={`Delete ${orgLabelLower}`}
325368
data-test-id="frontier-sdk-delete-organization-btn"
326369
>
327370
Delete {orgLabelLower}
328371
</Button>
329372
</Tooltip.Trigger>
330-
{!canDeleteWorkspace && (
373+
{!canDeleteWorkspace ? (
331374
<Tooltip.Content>{AuthTooltipMessage}</Tooltip.Content>
332-
)}
375+
) : isDeleteBlocked ? (
376+
<Tooltip.Content>
377+
<Flex direction="column" gap={2}>
378+
{deleteBlockerLines.map(line => (
379+
<Text key={line}>{line}</Text>
380+
))}
381+
</Flex>
382+
</Tooltip.Content>
383+
) : null}
333384
</Tooltip>
334385
</>
335386
)}

‎web/sdk/shared/types.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ export interface FrontierClientOptions {
5858
shortDateFormat?: string;
5959
billing?: FrontierClientBillingOptions;
6060
customization?: FrontierClientCustomizationOptions;
61+
/**
62+
* Check what blocks an organization, project or account delete when the
63+
* delete button is clicked, and show the blockers in a dialog. The server
64+
* must serve CheckOrganizationDelete, CheckProjectDelete and
65+
* CheckCurrentUserDelete. Off by default: the delete button opens the
66+
* confirm dialog directly, and the organization page checks on load and
67+
* disables the button while blocked.
68+
*/
69+
enableDeleteCheck?: boolean;
6170
}
6271

6372
export interface InitialState {

0 commit comments

Comments
 (0)