-
Notifications
You must be signed in to change notification settings - Fork 610
feat: keys multiple selection #3234
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b4236e3
feat: add selection controls
ogzhanolguncu c30a8fc
feat: allow disabling and enabling in batches
ogzhanolguncu e41ce82
feat: add delete key
ogzhanolguncu 60a4cb7
feat: add proper batching to externalId
ogzhanolguncu 6cac387
feat: finalize batch actions
ogzhanolguncu e109e62
fix: minor style issues
ogzhanolguncu dd99855
fix: minor type issues
ogzhanolguncu d43e82b
fix: coderabbit issues
ogzhanolguncu 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
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
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
120 changes: 83 additions & 37 deletions
120
.../_components/components/table/components/actions/components/hooks/use-edit-external-id.ts
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 |
|---|---|---|
| @@ -1,68 +1,114 @@ | ||
| import { toast } from "@/components/ui/toaster"; | ||
| import { trpc } from "@/lib/trpc/client"; | ||
| import type { TRPCClientErrorLike } from "@trpc/client"; | ||
| import type { TRPCErrorShape } from "@trpc/server/rpc"; | ||
|
|
||
| const handleKeyOwnerUpdateError = (err: TRPCClientErrorLike<TRPCErrorShape>) => { | ||
| if (err.data?.code === "NOT_FOUND") { | ||
| toast.error("Key Update Failed", { | ||
| description: "Unable to find the key(s). Please refresh and try again.", | ||
| }); | ||
| } else if (err.data?.code === "BAD_REQUEST") { | ||
| toast.error("Invalid External ID Information", { | ||
| description: | ||
| err.message || "Please ensure your External ID information is valid and try again.", | ||
| }); | ||
| } else if (err.data?.code === "INTERNAL_SERVER_ERROR") { | ||
| toast.error("Server Error", { | ||
| description: | ||
| "We are unable to update External ID information on this key. Please try again or contact support@unkey.dev", | ||
| }); | ||
| } else { | ||
| toast.error("Failed to Update Key External ID", { | ||
| description: | ||
| err.message || | ||
| "An unexpected error occurred. Please try again or contact support@unkey.dev", | ||
| action: { | ||
| label: "Contact Support", | ||
| onClick: () => window.open("mailto:support@unkey.dev", "_blank"), | ||
| }, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export const useEditExternalId = (onSuccess?: () => void) => { | ||
| const trpcUtils = trpc.useUtils(); | ||
|
|
||
| const updateKeyOwner = trpc.key.update.ownerId.useMutation({ | ||
| onSuccess(_, variables) { | ||
| let description = ""; | ||
| const keyId = Array.isArray(variables.keyIds) ? variables.keyIds[0] : variables.keyIds; | ||
|
|
||
| if (variables.ownerType === "v2") { | ||
| if (variables.identity?.id) { | ||
| description = `Identity for key ${variables.keyId} has been updated`; | ||
| } else { | ||
| description = `Identity has been removed from key ${variables.keyId}`; | ||
| } | ||
| } else { | ||
| if (variables.ownerId) { | ||
| description = `Owner for key ${variables.keyId} has been updated`; | ||
| description = `Identity for key ${keyId} has been updated`; | ||
| } else { | ||
| description = `Owner has been removed from key ${variables.keyId}`; | ||
| description = `Identity has been removed from key ${keyId}`; | ||
| } | ||
| } | ||
|
|
||
| toast.success("Key External ID Updated", { | ||
| description, | ||
| duration: 5000, | ||
| }); | ||
|
|
||
| trpcUtils.api.keys.list.invalidate(); | ||
|
|
||
| if (onSuccess) { | ||
| onSuccess(); | ||
| } | ||
| }, | ||
|
|
||
| onError(err) { | ||
| if (err.data?.code === "NOT_FOUND") { | ||
| toast.error("Key Update Failed", { | ||
| description: | ||
| "We are unable to find the correct key. Please try again or contact support@unkey.dev.", | ||
| }); | ||
| } else if (err.data?.code === "BAD_REQUEST") { | ||
| toast.error("Invalid External ID Information", { | ||
| description: | ||
| err.message || "Please ensure your external ID information is valid and try again.", | ||
| }); | ||
| } else if (err.data?.code === "INTERNAL_SERVER_ERROR") { | ||
| toast.error("Server Error", { | ||
| description: | ||
| "We are unable to update external ID information on this key. Please try again or contact support@unkey.dev", | ||
| }); | ||
| } else { | ||
| toast.error("Failed to Update Key External ID", { | ||
| description: | ||
| err.message || | ||
| "An unexpected error occurred. Please try again or contact support@unkey.dev", | ||
| action: { | ||
| label: "Contact Support", | ||
| onClick: () => window.open("mailto:support@unkey.dev", "_blank"), | ||
| }, | ||
| handleKeyOwnerUpdateError(err); | ||
| }, | ||
| }); | ||
|
|
||
| return updateKeyOwner; | ||
| }; | ||
|
|
||
| export const useBatchEditExternalId = (onSuccess?: () => void) => { | ||
| const trpcUtils = trpc.useUtils(); | ||
| const batchUpdateKeyOwner = trpc.key.update.ownerId.useMutation({ | ||
| onSuccess(data, variables) { | ||
| const updatedCount = data.updatedCount; | ||
| let description = ""; | ||
|
|
||
| if (variables.ownerType === "v2") { | ||
| if (variables.identity?.id) { | ||
| description = `Identity has been updated for ${updatedCount} ${ | ||
| updatedCount === 1 ? "key" : "keys" | ||
| }`; | ||
| } else { | ||
| description = `Identity has been removed from ${updatedCount} ${ | ||
| updatedCount === 1 ? "key" : "keys" | ||
| }`; | ||
| } | ||
| } | ||
| toast.success("Key External ID Updated", { | ||
| description, | ||
| duration: 5000, | ||
| }); | ||
|
|
||
| // Show warning if some keys were not found (if that info is available in the response) | ||
| const missingCount = Array.isArray(variables.keyIds) | ||
| ? variables.keyIds.length - updatedCount | ||
| : 0; | ||
|
|
||
| if (missingCount > 0) { | ||
| toast.warning("Some Keys Not Found", { | ||
| description: `${missingCount} ${ | ||
| missingCount === 1 ? "key was" : "keys were" | ||
| } not found and could not be updated.`, | ||
| duration: 7000, | ||
| }); | ||
| } | ||
|
|
||
| trpcUtils.api.keys.list.invalidate(); | ||
| if (onSuccess) { | ||
| onSuccess(); | ||
| } | ||
| }, | ||
| onError(err) { | ||
| handleKeyOwnerUpdateError(err); | ||
| }, | ||
| }); | ||
|
|
||
| return updateKeyOwner; | ||
| return batchUpdateKeyOwner; | ||
| }; |
81 changes: 60 additions & 21 deletions
81
...components/components/table/components/actions/components/hooks/use-update-key-status.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 |
|---|---|---|
| @@ -1,47 +1,86 @@ | ||
| import { toast } from "@/components/ui/toaster"; | ||
| import { trpc } from "@/lib/trpc/client"; | ||
| import type { TRPCClientErrorLike } from "@trpc/client"; | ||
| import type { TRPCErrorShape } from "@trpc/server/rpc"; | ||
|
|
||
| const handleKeyUpdateError = (err: TRPCClientErrorLike<TRPCErrorShape>) => { | ||
| const errorMessage = err.message || ""; | ||
| if (err.data?.code === "NOT_FOUND") { | ||
| toast.error("Key Update Failed", { | ||
| description: "Unable to find the key(s). Please refresh and try again.", | ||
| }); | ||
| } else if (err.data?.code === "INTERNAL_SERVER_ERROR") { | ||
| toast.error("Server Error", { | ||
| description: | ||
| "We encountered an issue while updating your key(s). Please try again later or contact support at support.unkey.dev", | ||
| }); | ||
| } else { | ||
| toast.error("Failed to Update Key Status", { | ||
| description: errorMessage || "An unexpected error occurred. Please try again later.", | ||
| action: { | ||
| label: "Contact Support", | ||
| onClick: () => window.open("https://support.unkey.dev", "_blank"), | ||
| }, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export const useUpdateKeyStatus = (onSuccess?: () => void) => { | ||
| const trpcUtils = trpc.useUtils(); | ||
|
|
||
| const updateKeyEnabled = trpc.key.update.enabled.useMutation({ | ||
| onSuccess(data) { | ||
| toast.success(`Key ${data.enabled ? "Enabled" : "Disabled"}`, { | ||
| description: `Your key ${data.keyId} has been ${ | ||
| description: `Your key ${data.updatedKeyIds[0]} has been ${ | ||
| data.enabled ? "enabled" : "disabled" | ||
| } successfully`, | ||
| duration: 5000, | ||
| }); | ||
|
|
||
| trpcUtils.api.keys.list.invalidate(); | ||
|
|
||
| if (onSuccess) { | ||
| onSuccess(); | ||
| } | ||
| }, | ||
| onError(err) { | ||
| const errorMessage = err.message || ""; | ||
| handleKeyUpdateError(err); | ||
| }, | ||
| }); | ||
|
|
||
| if (err.data?.code === "NOT_FOUND") { | ||
| toast.error("Key Update Failed", { | ||
| description: "Unable to find the key. Please refresh and try again.", | ||
| }); | ||
| } else if (err.data?.code === "INTERNAL_SERVER_ERROR") { | ||
| toast.error("Server Error", { | ||
| description: | ||
| "We encountered an issue while updating your key. Please try again later or contact support at support.unkey.dev", | ||
| }); | ||
| } else { | ||
| toast.error("Failed to Update Key Status", { | ||
| description: errorMessage || "An unexpected error occurred. Please try again later.", | ||
| action: { | ||
| label: "Contact Support", | ||
| onClick: () => window.open("https://support.unkey.dev", "_blank"), | ||
| }, | ||
| return updateKeyEnabled; | ||
| }; | ||
|
|
||
| export const useBatchUpdateKeyStatus = (onSuccess?: () => void) => { | ||
| const trpcUtils = trpc.useUtils(); | ||
|
|
||
| const updateMultipleKeysEnabled = trpc.key.update.enabled.useMutation({ | ||
| onSuccess(data) { | ||
| const updatedCount = data.updatedKeyIds.length; | ||
| toast.success(`Keys ${data.enabled ? "Enabled" : "Disabled"}`, { | ||
| description: `${updatedCount} ${ | ||
| updatedCount === 1 ? "key has" : "keys have" | ||
| } been ${data.enabled ? "enabled" : "disabled"} successfully`, | ||
| duration: 5000, | ||
| }); | ||
|
|
||
| // Show warning if some keys were not found | ||
| if (data.missingKeyIds && data.missingKeyIds.length > 0) { | ||
| toast.warning("Some Keys Not Found", { | ||
| description: `${data.missingKeyIds.length} ${ | ||
| data.missingKeyIds.length === 1 ? "key was" : "keys were" | ||
| } not found and could not be updated.`, | ||
| duration: 7000, | ||
| }); | ||
| } | ||
|
|
||
| trpcUtils.api.keys.list.invalidate(); | ||
| if (onSuccess) { | ||
| onSuccess(); | ||
| } | ||
| }, | ||
| onError(err) { | ||
| handleKeyUpdateError(err); | ||
| }, | ||
| }); | ||
|
|
||
| return updateKeyEnabled; | ||
| return updateMultipleKeysEnabled; | ||
| }; |
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.
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.