Skip to content
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

Improvement(notifications): Add copyable request IDs to server side errors #2839

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(notifications): add copyable request IDs
  • Loading branch information
McPizza0 committed Dec 4, 2024
commit 48cb5f6e9bbd42765bb88d204cf9a8c3fde3bea3
38 changes: 37 additions & 1 deletion frontend/src/components/notifications/Notifications.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import { ReactNode } from "react";
import { Id, toast, ToastContainer, ToastOptions, TypeOptions } from "react-toastify";
import { faCopy, IconDefinition } from "@fortawesome/free-solid-svg-icons";
import { twMerge } from "tailwind-merge";

import { CopyButton } from "../v2/CopyButton";

export type TNotification = {
title?: string;
text: ReactNode;
children?: ReactNode;
cta?: ReactNode;
McPizza0 marked this conversation as resolved.
Show resolved Hide resolved
copyActions?: { icon?: IconDefinition; value: string; name: string; label?: string }[];
};

export const NotificationContent = ({ title, text, children }: TNotification) => {
export const NotificationContent = ({ title, text, children, cta, copyActions }: TNotification) => {
return (
<div className="msg-container">
{title && <div className="text-md mb-1 font-medium">{title}</div>}
<div className={title ? "text-sm text-neutral-400" : "text-md"}>{text}</div>
{children && <div className="mt-2">{children}</div>}
{(cta || copyActions) && (
<div
className={twMerge(
"mt-2 flex h-7 w-full flex-row items-end gap-2",
cta ? "justify-between" : "justify-end"
)}
>
{cta}

{copyActions && (
<div className="flex h-7 flex-row items-center gap-2">
{copyActions.map((action) => (
<div className="flex flex-row items-center gap-2" key={`copy-${action.name}`}>
{action.label && (
<span className="ml-2 text-xs text-mineshaft-400">{action.label}</span>
)}
<CopyButton
value={action.value}
name={action.name}
size="xs"
variant="plain"
color="text-mineshaft-400"
icon={action.icon ?? faCopy}
/>
</div>
))}
</div>
)}
</div>
)}
</div>
);
};
Expand Down
57 changes: 57 additions & 0 deletions frontend/src/components/v2/CopyButton/CopyButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { faCheck, faCopy, IconDefinition } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { twMerge } from "tailwind-merge";

import { useTimedReset } from "@app/hooks";

import { IconButton } from "../IconButton";
import { Tooltip } from "../Tooltip";

export type CopyButtonProps = {
value: string;
size?: "xs" | "sm" | "md" | "lg";
variant?: "solid" | "outline" | "plain" | "star" | "outline_bg";
color?: string;
name?: string;
icon?: IconDefinition;
};

export const CopyButton = ({
value,
size = "sm",
variant = "solid",
color,
name,
icon = faCopy
}: CopyButtonProps) => {
const tooltipText = name ? `Copy ${name}` : "Copy to clipboard";
McPizza0 marked this conversation as resolved.
Show resolved Hide resolved

const [copyText, isCopying, setCopyText] = useTimedReset<string>({
initialState: tooltipText
});

async function handleCopyText() {
setCopyText("Copied");
navigator.clipboard.writeText(value);
}

return (
<div>
<Tooltip content={copyText} size={size === "xs" || size === "sm" ? "sm" : "md"}>
<IconButton
ariaLabel={tooltipText}
variant={variant}
className={twMerge("group relative", color)}
size={size}
onClick={() => {
handleCopyText();
}}
>
<FontAwesomeIcon icon={isCopying ? faCheck : icon} />
</IconButton>
</Tooltip>
</div>
);
};

CopyButton.displayName = "CopyButton";
2 changes: 2 additions & 0 deletions frontend/src/components/v2/CopyButton/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { CopyButtonProps } from "./CopyButton";
export { CopyButton } from "./CopyButton";
6 changes: 5 additions & 1 deletion frontend/src/components/v2/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type TooltipProps = Omit<TooltipPrimitive.TooltipContentProps, "open" | "
position?: "top" | "bottom" | "left" | "right";
isDisabled?: boolean;
center?: boolean;
size?: "sm" | "md";
};

export const Tooltip = ({
Expand All @@ -26,6 +27,7 @@ export const Tooltip = ({
asChild = true,
isDisabled,
position = "top",
size = "md",
...props
}: TooltipProps) =>
// just render children if tooltip content is empty
Expand All @@ -43,14 +45,16 @@ export const Tooltip = ({
sideOffset={5}
{...props}
className={twMerge(
`z-50 max-w-[15rem] select-none rounded-md border border-mineshaft-600 bg-mineshaft-800 py-2 px-4 text-sm font-light text-bunker-200 shadow-md
`z-50 max-w-[15rem] select-none border border-mineshaft-600 bg-mineshaft-800 font-light text-bunker-200 shadow-md
data-[state=delayed-open]:data-[side=top]:animate-slideDownAndFade
data-[state=delayed-open]:data-[side=right]:animate-slideLeftAndFade
data-[state=delayed-open]:data-[side=left]:animate-slideRightAndFade
data-[state=delayed-open]:data-[side=bottom]:animate-slideUpAndFade
`,
isDisabled && "!hidden",
center && "text-center",
size === "sm" && "rounded-sm py-1 px-2 text-xs",
size === "md" && "rounded-md py-2 px-4 text-sm",
className
)}
>
Expand Down
42 changes: 36 additions & 6 deletions frontend/src/hooks/api/dashboard/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,21 @@ export const useGetProjectSecretsOverview = (
}),
onError: (error) => {
if (axios.isAxiosError(error)) {
const serverResponse = error.response?.data as { message: string };
const { message, requestId } = error.response?.data as {
message: string;
requestId: string;
};
createNotification({
title: "Error fetching secret details",
type: "error",
text: serverResponse.message
text: message,
copyActions: [
{
value: requestId,
name: "Request ID",
label: `Request ID: ${requestId}`
}
]
});
}
},
Expand Down Expand Up @@ -270,11 +280,21 @@ export const useGetProjectSecretsDetails = (
}),
onError: (error) => {
if (axios.isAxiosError(error)) {
const serverResponse = error.response?.data as { message: string };
const { message, requestId } = error.response?.data as {
message: string;
requestId: string;
};
createNotification({
title: "Error fetching secret details",
type: "error",
text: serverResponse.message
text: message,
copyActions: [
{
value: requestId,
name: "Request ID",
label: `Request ID: ${requestId}`
}
]
});
}
},
Expand Down Expand Up @@ -355,11 +375,21 @@ export const useGetProjectSecretsQuickSearch = (
}),
onError: (error) => {
if (axios.isAxiosError(error)) {
const serverResponse = error.response?.data as { message: string };
const { message, requestId } = error.response?.data as {
message: string;
requestId: string;
};
createNotification({
title: "Error fetching secrets deep search",
type: "error",
text: serverResponse.message
text: message,
copyActions: [
{
value: requestId,
name: "Request ID",
label: `Request ID: ${requestId}`
}
]
});
}
},
Expand Down
31 changes: 25 additions & 6 deletions frontend/src/hooks/api/secrets/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,21 @@ export const useGetProjectSecrets = ({
queryFn: () => fetchProjectSecrets({ workspaceId, environment, secretPath }),
onError: (error) => {
if (axios.isAxiosError(error)) {
const serverResponse = error.response?.data as { message: string };
const { message, requestId } = error.response?.data as {
message: string;
requestId: string;
};
createNotification({
title: "Error fetching secrets",
type: "error",
text: serverResponse.message
text: message,
copyActions: [
{
value: requestId,
name: "Request ID",
label: `Request ID: ${requestId}`
}
]
});
}
},
Expand All @@ -148,15 +158,24 @@ export const useGetProjectSecretsAllEnv = ({
enabled: Boolean(workspaceId && environment),
onError: (error: unknown) => {
if (axios.isAxiosError(error) && !isErrorHandled) {
const serverResponse = error.response?.data as { message: string };
if (serverResponse.message !== ERROR_NOT_ALLOWED_READ_SECRETS) {
const { message, requestId } = error.response?.data as {
message: string;
requestId: string;
};
if (message !== ERROR_NOT_ALLOWED_READ_SECRETS) {
createNotification({
title: "Error fetching secrets",
type: "error",
text: serverResponse.message
text: message,
copyActions: [
{
value: requestId,
name: "Request ID",
label: `Request ID: ${requestId}`
}
]
});
}

setIsErrorHandled.on();
}
},
Expand Down
41 changes: 28 additions & 13 deletions frontend/src/reactQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ export const queryClient = new QueryClient({
{
title: "Validation Error",
type: "error",
text: (
<div>
<p>Please check the input and try again.</p>
<p className="mt-2 text-xs">Request ID: {serverResponse.requestId}</p>
</div>
),
children: (
text: "Please check the input and try again.",
cta: (
<Modal>
<ModalTrigger>
<Button variant="outline_bg" size="xs">
Expand Down Expand Up @@ -66,7 +61,14 @@ export const queryClient = new QueryClient({
</TableContainer>
</ModalContent>
</Modal>
)
),
copyActions: [
{
value: serverResponse.requestId,
name: "Request ID",
label: `Request ID: ${serverResponse.requestId}`
}
]
},
{ closeOnClick: false }
);
Expand All @@ -77,9 +79,8 @@ export const queryClient = new QueryClient({
{
title: "Forbidden Access",
type: "error",

text: `${serverResponse.message} [requestId=${serverResponse.requestId}]`,
children: serverResponse?.details?.length ? (
text: `${serverResponse.message}.`,
cta: serverResponse?.details?.length ? (
<Modal>
<ModalTrigger>
<Button variant="outline_bg" size="xs">
Expand Down Expand Up @@ -165,7 +166,14 @@ export const queryClient = new QueryClient({
</div>
</ModalContent>
</Modal>
) : undefined
) : undefined,
copyActions: [
{
value: serverResponse.requestId,
name: "Request ID",
label: `Request ID: ${serverResponse.requestId}`
}
]
},
{ closeOnClick: false }
);
Expand All @@ -174,7 +182,14 @@ export const queryClient = new QueryClient({
createNotification({
title: "Bad Request",
type: "error",
text: `${serverResponse.message} [requestId=${serverResponse.requestId}]`
text: `${serverResponse.message}.`,
copyActions: [
{
value: serverResponse.requestId,
name: "Request ID",
label: `Request ID: ${serverResponse.requestId}`
}
]
});
}
}
Expand Down
Loading