Skip to content

Commit 130efe4

Browse files
fix(sdk): reword the delete organization blocker messages (#1921)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 88c7b71 commit 130efe4

4 files changed

Lines changed: 139 additions & 39 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Code, ConnectError } from '@connectrpc/connect';
2+
import {
3+
GENERIC_DELETE_BLOCKED_MESSAGE,
4+
deleteBlockedDescription,
5+
instructionLines
6+
} from './delete-blockers';
7+
8+
const wording = { organizationLabel: 'workspace', basePlanTitle: 'Standard Plan' };
9+
10+
describe('instructionLines', () => {
11+
it('writes one full sentence per kind of blocker', () => {
12+
const lines = instructionLines(
13+
[
14+
{ type: 'ACTIVE_SUBSCRIPTION' },
15+
{ type: 'UNPAID_INVOICE' },
16+
{ type: 'NEGATIVE_TOKEN_BALANCE' }
17+
],
18+
wording
19+
);
20+
expect(lines).toEqual([
21+
'Downgrade your current subscription and switch to the Standard Plan before deleting this workspace.',
22+
'Please pay your outstanding invoice before deleting this workspace.',
23+
'Please purchase enough tokens to clear your outstanding token balance before deleting this workspace.'
24+
]);
25+
});
26+
27+
it('counts blockers of the same kind', () => {
28+
const lines = instructionLines(
29+
[{ type: 'UNPAID_INVOICE' }, { type: 'UNPAID_INVOICE' }],
30+
wording
31+
);
32+
expect(lines).toEqual([
33+
'Please pay your 2 outstanding invoices before deleting this workspace.'
34+
]);
35+
});
36+
37+
it('falls back to plain words when the host app set no terminology', () => {
38+
const lines = instructionLines([{ type: 'ACTIVE_SUBSCRIPTION' }]);
39+
expect(lines).toEqual([
40+
'Downgrade your current subscription and switch to the standard plan before deleting this organization.'
41+
]);
42+
});
43+
44+
it('shows the generic message once for unknown kinds', () => {
45+
const lines = instructionLines(
46+
[{ type: 'SOMETHING_NEW' }, { type: 'SOMETHING_ELSE' }],
47+
wording
48+
);
49+
expect(lines).toEqual([GENERIC_DELETE_BLOCKED_MESSAGE]);
50+
});
51+
});
52+
53+
describe('deleteBlockedDescription', () => {
54+
it('reads the blockers out of the precondition failure detail', () => {
55+
const err = new ConnectError('blocked', Code.FailedPrecondition);
56+
err.details.push({
57+
type: 'google.rpc.PreconditionFailure',
58+
value: new Uint8Array(),
59+
debug: {
60+
violations: [
61+
{ type: 'ACTIVE_SUBSCRIPTION', subject: 'sub-1' },
62+
{ type: 'NEGATIVE_TOKEN_BALANCE', subject: 'acct-1' }
63+
]
64+
}
65+
});
66+
expect(deleteBlockedDescription(err, wording)).toBe(
67+
'Downgrade your current subscription and switch to the Standard Plan before deleting this workspace. ' +
68+
'Please purchase enough tokens to clear your outstanding token balance before deleting this workspace.'
69+
);
70+
});
71+
72+
it('returns the generic message when the error carries no blockers', () => {
73+
const err = new ConnectError('blocked', Code.FailedPrecondition);
74+
expect(deleteBlockedDescription(err, wording)).toBe(
75+
GENERIC_DELETE_BLOCKED_MESSAGE
76+
);
77+
});
78+
});

‎web/sdk/client/utils/delete-blockers.ts‎

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
import { ConnectError } from '@connectrpc/connect';
22

3-
// The server reports why an organization cannot be deleted as a list of
4-
// blockers, each with a machine-readable type. These are the types the
5-
// server knows today, mapped to a short instruction the user can act on.
6-
// The wording must stay in line with the server behavior: a paid
7-
// subscription must be downgraded, unpaid invoices must be paid, and a
8-
// token debt is settled through support.
9-
const BLOCKER_INSTRUCTIONS: Record<string, (count: number) => string> = {
10-
ACTIVE_SUBSCRIPTION: () =>
11-
'Please downgrade the subscription to the standard plan',
12-
UNPAID_INVOICE: count =>
3+
// Words that come from the host app: what it calls an organization
4+
// and what it calls its base plan.
5+
export interface BlockerWording {
6+
organizationLabel: string;
7+
basePlanTitle?: string;
8+
}
9+
10+
const DEFAULT_WORDING: BlockerWording = {
11+
organizationLabel: 'organization'
12+
};
13+
14+
// One sentence per blocker type the server can return.
15+
const BLOCKER_INSTRUCTIONS: Record<
16+
string,
17+
(count: number, wording: BlockerWording) => string
18+
> = {
19+
ACTIVE_SUBSCRIPTION: (_, { organizationLabel, basePlanTitle }) =>
20+
`Downgrade your current subscription and switch to the ${
21+
basePlanTitle || 'standard plan'
22+
} before deleting this ${organizationLabel}.`,
23+
UNPAID_INVOICE: (count, { organizationLabel }) =>
1324
count > 1
14-
? `Please pay the ${count} open invoices from the billing page`
15-
: 'Please pay the open invoice from the billing page',
16-
NEGATIVE_TOKEN_BALANCE: () =>
17-
'Your token balance is negative. Please add tokens from the Tokens page to bring it back up, or pay the invoice when it arrives, and then you can delete'
25+
? `Please pay your ${count} outstanding invoices before deleting this ${organizationLabel}.`
26+
: `Please pay your outstanding invoice before deleting this ${organizationLabel}.`,
27+
NEGATIVE_TOKEN_BALANCE: (_, { organizationLabel }) =>
28+
`Please purchase enough tokens to clear your outstanding token balance before deleting this ${organizationLabel}.`
1829
};
1930

20-
// Shown when the server reports a blocker kind this version does not know,
21-
// or when the error could not be read at all.
31+
// Used for blocker types this version does not know.
2232
export const GENERIC_DELETE_BLOCKED_MESSAGE =
2333
'Something is blocking the delete right now. Please try again later or contact support.';
2434

25-
// instructionLines turns a list of blockers into one instruction per kind
26-
// of blocker. Blockers of the same kind are counted so the instruction can
27-
// say "the 2 open invoices". A kind without a known instruction becomes the
28-
// generic message, once.
29-
export function instructionLines(blockers: { type: string }[]): string[] {
35+
// Returns one sentence per blocker type. Same-type blockers are counted.
36+
export function instructionLines(
37+
blockers: { type: string }[],
38+
wording: BlockerWording = DEFAULT_WORDING
39+
): string[] {
3040
const counts = new Map<string, number>();
3141
for (const blocker of blockers) {
3242
counts.set(blocker.type, (counts.get(blocker.type) ?? 0) + 1);
@@ -36,7 +46,7 @@ export function instructionLines(blockers: { type: string }[]): string[] {
3646
for (const [type, count] of counts) {
3747
const instruction = BLOCKER_INSTRUCTIONS[type];
3848
if (instruction) {
39-
lines.push(instruction(count));
49+
lines.push(instruction(count, wording));
4050
} else {
4151
hasUnknown = true;
4252
}
@@ -47,13 +57,14 @@ export function instructionLines(blockers: { type: string }[]): string[] {
4757
return lines;
4858
}
4959

50-
// deleteBlockedDescription reads the blockers out of a failed_precondition
51-
// error from DeleteOrganization and returns the instructions as one string.
52-
// The server attaches them as a google.rpc.PreconditionFailure detail; over
53-
// the Connect JSON protocol that detail arrives with a ready-made JSON copy
54-
// in its debug field. When the error carries nothing readable, the generic
55-
// message is returned, so the raw server text is never shown.
56-
export function deleteBlockedDescription(err: ConnectError): string {
60+
// Reads the blockers from a failed_precondition error and returns the
61+
// sentences as one string. The server sends them as a
62+
// google.rpc.PreconditionFailure detail, which Connect exposes in the
63+
// detail's debug field.
64+
export function deleteBlockedDescription(
65+
err: ConnectError,
66+
wording: BlockerWording = DEFAULT_WORDING
67+
): string {
5768
for (const detail of err.details) {
5869
if (!('type' in detail) || detail.type !== 'google.rpc.PreconditionFailure') {
5970
continue;
@@ -73,9 +84,9 @@ export function deleteBlockedDescription(err: ConnectError): string {
7384
violation !== null &&
7485
typeof (violation as { type?: unknown }).type === 'string'
7586
);
76-
const lines = instructionLines(blockers);
87+
const lines = instructionLines(blockers, wording);
7788
if (lines.length > 0) {
78-
return lines.join('. ');
89+
return lines.join(' ');
7990
}
8091
}
8192
return GENERIC_DELETE_BLOCKED_MESSAGE;

‎web/sdk/client/views/general/components/delete-organization-dialog.tsx‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const DeleteOrganizationDialog = ({
4242
onDeleteSuccess
4343
}: DeleteOrganizationDialogProps) => {
4444
const t = useTerminology();
45-
const { activeOrganization: organization } = useFrontier();
45+
const { activeOrganization: organization, basePlan } = useFrontier();
4646
const orgLabel = t.organization({ case: 'capital' });
4747
const orgLabelLower = t.organization({ case: 'lower' });
4848
const [isAcknowledged, setIsAcknowledged] = useState(false);
@@ -89,9 +89,15 @@ export const DeleteOrganizationDialog = ({
8989
} catch (error) {
9090
handleConnectError(error, {
9191
PermissionDenied: () => toastManager.add({ title: "You don't have permission to perform this action", type: 'error' }),
92-
// the server names what blocks the delete; show the matching
93-
// instructions instead of the raw server text
94-
FailedPrecondition: (err) => toastManager.add({ title: `Cannot delete this ${orgLabelLower} yet`, description: deleteBlockedDescription(err), type: 'error' }),
92+
FailedPrecondition: (err) =>
93+
toastManager.add({
94+
title: `Cannot delete this ${orgLabelLower} yet`,
95+
description: deleteBlockedDescription(err, {
96+
organizationLabel: orgLabelLower,
97+
basePlanTitle: basePlan?.title
98+
}),
99+
type: 'error'
100+
}),
95101
NotFound: () => toastManager.add({ title: 'Not found', description: `This ${orgLabelLower} no longer exists.`, type: 'error' }),
96102
Default: () => toastManager.add({ title: 'Something went wrong', description: 'Please try again later or contact support.', type: 'error' }),
97103
});

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export function GeneralView({ onDeleteSuccess, urlPrefix }: GeneralViewProps = {
6060
const {
6161
activeOrganization: organization,
6262
isActiveOrganizationLoading,
63-
setActiveOrganization
63+
setActiveOrganization,
64+
basePlan
6465
} = useFrontier();
6566
const queryClient = useQueryClient();
6667
const transport = useTransport();
@@ -116,8 +117,12 @@ export function GeneralView({ onDeleteSuccess, urlPrefix }: GeneralViewProps = {
116117
);
117118
const isDeleteBlocked = !!deleteCheck && !deleteCheck.canDelete;
118119
const blockerLines = useMemo(
119-
() => instructionLines(deleteCheck?.blockers ?? []),
120-
[deleteCheck]
120+
() =>
121+
instructionLines(deleteCheck?.blockers ?? [], {
122+
organizationLabel: t.organization({ case: 'lower' }),
123+
basePlanTitle: basePlan?.title
124+
}),
125+
[deleteCheck, t, basePlan]
121126
);
122127

123128
// Update organization form
@@ -326,7 +331,7 @@ export function GeneralView({ onDeleteSuccess, urlPrefix }: GeneralViewProps = {
326331
<Tooltip.Content>
327332
<Flex direction="column" gap={2}>
328333
{blockerLines.map(line => (
329-
<span key={line}>{line}</span>
334+
<Text key={line}>{line}</Text>
330335
))}
331336
</Flex>
332337
</Tooltip.Content>

0 commit comments

Comments
 (0)