Repository navigation
feat(sdk): org delete blockers UI #2025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+507
−317
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
574ad14
feat(sdk): check delete blockers on click and show them in a dialog
rohanchkrabrty 2ad36cc
chore(sdk): update proton to 680ef693
rohanchkrabrty dbbfb79
fix(sdk): open the delete dialog only when the delete check passes
rohanchkrabrty 3c792c6
Merge remote-tracking branch 'origin/main' into feat/workspace-delete…
rohanchkrabrty 4770603
refactor(sdk): open the delete organization dialog with a handle
rohanchkrabrty 488d0ca
chore(sdk): drop comments from the delete flow changes
rohanchkrabrty 072fe99
Merge remote-tracking branch 'origin/main' into feat/workspace-delete…
rohanchkrabrty 63d4fe4
feat(sdk): let the host app word the blocked delete dialog
rohanchkrabrty 56698ff
chore(sdk): drop comments from the delete blocked dialog changes
rohanchkrabrty 2c01e92
fix(sdk): address review of the delete blocked dialog
rohanchkrabrty 5cacc30
chore(sdk): remove the delete blockers tests
rohanchkrabrty 5797739
refactor(sdk): rename useDeleteFlow to useDeleteCheck
rohanchkrabrty a9080d3
fix(sdk): create the delete dialog handles per mount
rohanchkrabrty 6277d9b
chore(sdk): drop the useTokens enabled option and format the delete f…
rohanchkrabrty b923bac
fix(sdk): show a toast when a refused delete has no readable blockers
rohanchkrabrty 7a874bf
refactor(sdk): render the whole blocked dialog from DeleteBlockedDialog
rohanchkrabrty 6b001b4
fix(sdk): show a toast when a delete is refused for a disabled workspace
rohanchkrabrty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
web/sdk/client/components/delete-blocked-dialog/delete-blocked-dialog.module.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| .content { | ||
| width: 480px !important; | ||
| } | ||
|
|
||
| .list { | ||
| margin: 0; | ||
| padding-left: var(--rs-space-5); | ||
| list-style-type: disc; | ||
| } | ||
143 changes: 143 additions & 0 deletions
143
web/sdk/client/components/delete-blocked-dialog/delete-blocked-dialog.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| 'use client'; | ||
|
|
||
| import { createContext, useContext } from 'react'; | ||
| import { AlertDialog, Button, Flex, Text } from '@raystack/apsara'; | ||
| import type { | ||
| DeleteBlocker, | ||
| DeleteBlockedContext, | ||
| DeleteBlockedPayload | ||
| } from '../../../shared/types'; | ||
| import { useFrontier } from '../../contexts/FrontierContext'; | ||
| import { useTerminology } from '../../hooks/useTerminology'; | ||
| import { blockerLines } from '../../utils/delete-blockers'; | ||
| import styles from './delete-blocked-dialog.module.css'; | ||
|
|
||
| export type DeleteBlockedDialogHandle = ReturnType< | ||
| typeof AlertDialog.createHandle<DeleteBlockedPayload> | ||
| >; | ||
|
|
||
| const DEFAULT_COPY: Record< | ||
| DeleteBlockedPayload['entity'], | ||
| (entityLabel: string) => { title: string; description: string } | ||
| > = { | ||
| organization: entityLabel => ({ | ||
| title: `Unable to delete ${entityLabel}`, | ||
| description: `Please resolve the following issues before deleting this ${entityLabel}:` | ||
| }) | ||
| }; | ||
|
|
||
| const DeleteBlockedDialogContext = createContext<DeleteBlockedContext | null>( | ||
| null | ||
| ); | ||
|
|
||
| export interface DeleteBlockedDialogRootProps { | ||
| handle: DeleteBlockedDialogHandle; | ||
| } | ||
|
|
||
| export function DeleteBlockedDialogRoot({ | ||
| handle | ||
| }: DeleteBlockedDialogRootProps) { | ||
| return ( | ||
| <AlertDialog handle={handle}> | ||
| {({ payload }) => | ||
| payload ? ( | ||
| <DeleteBlockedContent | ||
| payload={payload} | ||
| close={() => handle.close()} | ||
| /> | ||
| ) : null | ||
| } | ||
| </AlertDialog> | ||
| ); | ||
| } | ||
|
|
||
| function DeleteBlockedContent({ | ||
| payload, | ||
| close | ||
| }: { | ||
| payload: DeleteBlockedPayload; | ||
| close: () => void; | ||
| }) { | ||
| const t = useTerminology(); | ||
| const { slots } = useFrontier(); | ||
| const context: DeleteBlockedContext = { | ||
| ...payload, | ||
| entityLabel: t[payload.entity]({ case: 'lower' }), | ||
| close | ||
| }; | ||
| const body = slots.deleteBlockedDialog?.(context); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| const isEmpty = body == null || typeof body === 'boolean'; | ||
| return ( | ||
| <DeleteBlockedDialogContext.Provider value={context}> | ||
| {isEmpty ? <DeleteBlockedDialog /> : body} | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| </DeleteBlockedDialogContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export interface DeleteBlockedDialogProps { | ||
| title?: string; | ||
| description?: string; | ||
| blockers?: DeleteBlocker[]; | ||
| messages?: Record<string, string>; | ||
| } | ||
|
|
||
| export function DeleteBlockedDialog({ | ||
| title, | ||
| description, | ||
| blockers, | ||
| messages = {} | ||
| }: DeleteBlockedDialogProps) { | ||
| const context = useContext(DeleteBlockedDialogContext); | ||
| const t = useTerminology(); | ||
| const { basePlan } = useFrontier(); | ||
| if (!context) { | ||
| throw new Error( | ||
| 'DeleteBlockedDialog must be returned from the deleteBlockedDialog slot on FrontierProvider.' | ||
| ); | ||
| } | ||
|
|
||
| const copy = DEFAULT_COPY[context.entity](context.entityLabel); | ||
| const lines = blockerLines(blockers ?? context.blockers, messages, { | ||
| organizationLabel: t.organization({ case: 'lower' }), | ||
| basePlanTitle: basePlan?.title | ||
| }); | ||
|
|
||
| return ( | ||
| <AlertDialog.Content className={styles.content}> | ||
| <AlertDialog.Header> | ||
| <AlertDialog.Title>{title ?? copy.title}</AlertDialog.Title> | ||
| </AlertDialog.Header> | ||
| <AlertDialog.Body> | ||
| <Flex direction="column" gap={5}> | ||
| <Text size="small" variant="secondary"> | ||
| {description ?? copy.description} | ||
| </Text> | ||
| {lines.length > 0 && ( | ||
| <Text | ||
| size="small" | ||
| variant="secondary" | ||
| render={<ul className={styles.list} />} | ||
| > | ||
| {lines.map(line => ( | ||
| <li key={line}>{line}</li> | ||
| ))} | ||
| </Text> | ||
| )} | ||
| </Flex> | ||
| </AlertDialog.Body> | ||
| <AlertDialog.Footer> | ||
| <AlertDialog.Close | ||
| render={ | ||
| <Button | ||
| variant="outline" | ||
| color="neutral" | ||
| data-test-id="frontier-sdk-delete-blocked-close-btn" | ||
| > | ||
| Close | ||
| </Button> | ||
| } | ||
| /> | ||
| </AlertDialog.Footer> | ||
| </AlertDialog.Content> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export { | ||
| DeleteBlockedDialog, | ||
| DeleteBlockedDialogRoot | ||
| } from './delete-blocked-dialog'; | ||
| export type { | ||
| DeleteBlockedDialogProps, | ||
| DeleteBlockedDialogHandle | ||
| } from './delete-blocked-dialog'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { useState } from 'react'; | ||
| import { AlertDialog, toastManager } from '@raystack/apsara'; | ||
| import type { Blocker } from '@raystack/proton/frontier'; | ||
| import type { DeleteBlocker, DeleteBlockedPayload } from '../../shared/types'; | ||
| import type { DeleteBlockedDialogHandle } from '../components/delete-blocked-dialog'; | ||
| import { | ||
| GENERIC_DELETE_BLOCKED_MESSAGE, | ||
| blockersFromError, | ||
| isOrganizationDisabled, | ||
| toDeleteBlockers | ||
| } from '../utils/delete-blockers'; | ||
| import { useTerminology } from './useTerminology'; | ||
| import { handleConnectError } from '~/utils/error'; | ||
|
|
||
| export interface UseDeleteCheckOptions<TPayload> { | ||
| entity: DeleteBlockedPayload['entity']; | ||
| check: ( | ||
| payload: TPayload | ||
| ) => Promise<{ canDelete: boolean; blockers: Blocker[] }>; | ||
|
rohanchkrabrty marked this conversation as resolved.
Resolved
|
||
| confirmDialogHandle: ReturnType<typeof AlertDialog.createHandle<TPayload>>; | ||
| blockedDialogHandle: DeleteBlockedDialogHandle; | ||
| } | ||
|
|
||
| export function useDeleteCheck<TPayload>({ | ||
| entity, | ||
| check, | ||
| confirmDialogHandle, | ||
| blockedDialogHandle | ||
| }: UseDeleteCheckOptions<TPayload>) { | ||
| const t = useTerminology(); | ||
| const [isChecking, setIsChecking] = useState(false); | ||
|
|
||
| function showBlocked(blockers: DeleteBlocker[]) { | ||
| confirmDialogHandle.close(); | ||
| blockedDialogHandle.openWithPayload({ entity, blockers }); | ||
| } | ||
|
|
||
| function handleDeleteError(error: unknown) { | ||
| handleConnectError(error, { | ||
| FailedPrecondition: err => { | ||
| if (isOrganizationDisabled(err)) { | ||
| const org = t.organization({ case: 'lower' }); | ||
| toastManager.add({ | ||
| title: `Unable to delete ${t[entity]({ case: 'lower' })}`, | ||
| description: `This ${org} is disabled. Contact your administrator to enable it, then try again.`, | ||
| type: 'error' | ||
| }); | ||
| return; | ||
| } | ||
| const blockers = blockersFromError(err); | ||
| if (blockers.length > 0) { | ||
| showBlocked(blockers); | ||
| } else { | ||
| toastManager.add({ | ||
| title: `Unable to delete ${t[entity]({ case: 'lower' })}`, | ||
| description: GENERIC_DELETE_BLOCKED_MESSAGE, | ||
| type: 'error' | ||
| }); | ||
| } | ||
| }, | ||
| PermissionDenied: () => | ||
| toastManager.add({ | ||
| title: "You don't have permission to perform this action", | ||
| type: 'error' | ||
| }), | ||
| NotFound: () => | ||
| toastManager.add({ | ||
| title: 'Not found', | ||
| description: `This ${t[entity]({ case: 'lower' })} no longer exists.`, | ||
| type: 'error' | ||
| }), | ||
| Default: () => | ||
| toastManager.add({ | ||
| title: 'Something went wrong', | ||
| description: 'Please try again later or contact support.', | ||
| type: 'error' | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| async function startDelete(payload: TPayload) { | ||
| setIsChecking(true); | ||
| try { | ||
| const { canDelete, blockers } = await check(payload); | ||
| if (canDelete) { | ||
| confirmDialogHandle.openWithPayload(payload); | ||
| } else { | ||
| showBlocked(toDeleteBlockers(blockers)); | ||
| } | ||
| } catch (error) { | ||
| handleDeleteError(error); | ||
| } finally { | ||
| setIsChecking(false); | ||
| } | ||
| } | ||
|
|
||
| return { startDelete, isChecking, handleDeleteError }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.