Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import { z } from "zod";

const formSchema = z.object({
keyId: z.string(),
workspaceId: z.string(),
enabled: z.boolean(),
});
type Props = {
apiKey: {
id: string;
workspaceId: string;
enabled: boolean;
};
};
Expand All @@ -40,7 +38,6 @@ export const UpdateKeyEnabled: React.FC<Props> = ({ apiKey }) => {
delayError: 100,
defaultValues: {
keyId: apiKey.id,
workspaceId: apiKey.workspaceId,
enabled: apiKey.enabled,
},
});
Expand All @@ -56,7 +53,10 @@ export const UpdateKeyEnabled: React.FC<Props> = ({ apiKey }) => {
});

async function onSubmit(values: z.infer<typeof formSchema>) {
await updateEnabled.mutateAsync(values);
await updateEnabled.mutateAsync({
enabled: values.enabled,
keyIds: values.keyId,
});
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export const UpdateKeyOwnerId: React.FC<Props> = ({ apiKey }) => {
});

async function onSubmit(values: z.infer<typeof formSchema>) {
await updateOwnerId.mutateAsync({ ...values, ownerType: "v1" });
await updateOwnerId.mutateAsync({
ownerId: values.ownerId,
ownerType: "v1",
keyIds: values.keyId,
});
}
return (
<Form {...form}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const UpdateKeyStatus = ({ keyDetails, isOpen, onClose }: UpdateKeyStatus
try {
setIsLoading(true);
await updateKeyStatus.mutateAsync({
keyId: keyDetails.id,
keyIds: [keyDetails.id],
enabled: isEnabling,
});
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const EditExternalId = ({

const handleSubmit = () => {
updateKeyOwner.mutate({
keyId: keyDetails.id,
keyIds: keyDetails.id,
ownerType: "v2",
identity: {
id: selectedIdentityId,
Expand All @@ -59,7 +59,7 @@ export const EditExternalId = ({
const clearSelection = async () => {
setSelectedIdentityId(null);
await updateKeyOwner.mutateAsync({
keyId: keyDetails.id,
keyIds: keyDetails.id,
ownerType: "v2",
identity: {
id: null,
Expand All @@ -71,7 +71,7 @@ export const EditExternalId = ({
<>
<DialogContainer
isOpen={isOpen}
subTitle="Provide an owner to this key, like a userId from your system"
subTitle="Provide an owner to this key, like a userID from your system"
onOpenChange={handleDialogOpenChange}
title="Edit External ID"
footer={
Expand Down
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;
};
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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const KeysTableActionPopover = ({ items, align = "end" }: BaseTableAction
type="button"
className={cn(
"group-data-[state=open]:bg-gray-6 group-hover:bg-gray-6 group size-5 p-0 rounded m-0 items-center flex justify-center",
"border border-gray-6 hover:border-gray-8 ring-2 ring-transparent focus-visible:ring-gray-7 focus-visible:border-gray-7",
"border border-gray-6 group-hover:border-gray-8 ring-2 ring-transparent focus-visible:ring-gray-7 focus-visible:border-gray-7",
)}
>
<Dots className="group-hover:text-gray-12 text-gray-11" size="sm-regular" />
Expand Down
Loading