fix: copy secret audit log

This commit is contained in:
Piyush Gupta
2025-11-25 00:27:59 +05:30
parent e719de8710
commit 3acbae2c1d
2 changed files with 28 additions and 70 deletions
@@ -10,7 +10,6 @@ import {
faXmark faXmark
} from "@fortawesome/free-solid-svg-icons"; } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useQueryClient } from "@tanstack/react-query";
import { twMerge } from "tailwind-merge"; import { twMerge } from "tailwind-merge";
import { createNotification } from "@app/components/notifications"; import { createNotification } from "@app/components/notifications";
@@ -33,11 +32,7 @@ import {
} from "@app/context"; } from "@app/context";
import { ProjectPermissionSecretActions } from "@app/context/ProjectPermissionContext/types"; import { ProjectPermissionSecretActions } from "@app/context/ProjectPermissionContext/types";
import { usePopUp, useToggle } from "@app/hooks"; import { usePopUp, useToggle } from "@app/hooks";
import { import { useGetSecretValue } from "@app/hooks/api/dashboard/queries";
dashboardKeys,
fetchSecretValue,
useGetSecretValue
} from "@app/hooks/api/dashboard/queries";
import { ProjectEnv, SecretType, SecretV3RawSanitized } from "@app/hooks/api/types"; import { ProjectEnv, SecretType, SecretV3RawSanitized } from "@app/hooks/api/types";
import { hasSecretReadValueOrDescribePermission } from "@app/lib/fn/permission"; import { hasSecretReadValueOrDescribePermission } from "@app/lib/fn/permission";
import { CollapsibleSecretImports } from "@app/pages/secret-manager/SecretDashboardPage/components/SecretListView/CollapsibleSecretImports"; import { CollapsibleSecretImports } from "@app/pages/secret-manager/SecretDashboardPage/components/SecretListView/CollapsibleSecretImports";
@@ -109,8 +104,6 @@ export const SecretEditRow = ({
"editSecret" "editSecret"
] as const); ] as const);
const queryClient = useQueryClient();
const { currentProject } = useProject(); const { currentProject } = useProject();
const [isFieldFocused, setIsFieldFocused] = useToggle(); const [isFieldFocused, setIsFieldFocused] = useToggle();
@@ -137,19 +130,18 @@ export const SecretEditRow = ({
const { const {
data: secretValueData, data: secretValueData,
isPending: isPendingSecretValueData, isPending: isPendingSecretValueData,
isError: isErrorFetchingSecretValue isError: isErrorFetchingSecretValue,
refetch: refetchSecretValue
} = useGetSecretValue(fetchSecretValueParams, { } = useGetSecretValue(fetchSecretValueParams, {
enabled: canFetchValue && (isVisible || isFieldFocused) enabled: canFetchValue && (isVisible || isFieldFocused)
}); });
const isFetchingSecretValue = canFetchValue && isPendingSecretValueData; const isFetchingSecretValue = canFetchValue && isPendingSecretValueData;
const isSecretValueFetched = Boolean(secretValueData);
const { const {
handleSubmit, handleSubmit,
control, control,
reset, reset,
getValues,
setValue, setValue,
formState: { isDirty, isSubmitting } formState: { isDirty, isSubmitting }
} = useForm({ } = useForm({
@@ -178,34 +170,17 @@ export const SecretEditRow = ({
}; };
const handleCopySecretToClipboard = async () => { const handleCopySecretToClipboard = async () => {
if (!isSecretValueFetched && !isDirty) { try {
try { const { data } = await refetchSecretValue();
const data = await fetchSecretValue(fetchSecretValueParams);
queryClient.setQueryData(dashboardKeys.getSecretValue(fetchSecretValueParams), data); await window.navigator.clipboard.writeText(data?.valueOverride ?? data?.value ?? "");
createNotification({ type: "success", text: "Copied secret to clipboard" });
await window.navigator.clipboard.writeText(data.valueOverride ?? data.value); } catch (e) {
createNotification({ type: "success", text: "Copied secret to clipboard" }); console.error(e);
return; createNotification({
} catch (e) { type: "error",
console.error(e); text: "Failed to fetch secret value."
createNotification({ });
type: "error",
text: "Failed to fetch secret value."
});
return;
}
}
const { value } = getValues();
if (value) {
try {
await window.navigator.clipboard.writeText(value);
createNotification({ type: "success", text: "Copied secret to clipboard" });
} catch (error) {
console.log(error);
createNotification({ type: "error", text: "Failed to copy secret to clipboard" });
}
} }
}; };
@@ -47,13 +47,9 @@ import { faEyeSlash, faKey, faRotate, faWarning } from "@fortawesome/free-solid-
import { PendingAction } from "@app/hooks/api/secretFolders/types"; import { PendingAction } from "@app/hooks/api/secretFolders/types";
import { format } from "date-fns"; import { format } from "date-fns";
import { CreateReminderForm } from "@app/pages/secret-manager/SecretDashboardPage/components/SecretListView/CreateReminderForm"; import { CreateReminderForm } from "@app/pages/secret-manager/SecretDashboardPage/components/SecretListView/CreateReminderForm";
import { import { useGetSecretValue } from "@app/hooks/api/dashboard/queries";
dashboardKeys,
fetchSecretValue,
useGetSecretValue
} from "@app/hooks/api/dashboard/queries";
import { createNotification } from "@app/components/notifications"; import { createNotification } from "@app/components/notifications";
import { useQueryClient } from "@tanstack/react-query"; import { DashboardSecretValue } from "@app/hooks/api/dashboard/types";
import { import {
FontAwesomeSpriteName, FontAwesomeSpriteName,
formSchema, formSchema,
@@ -124,7 +120,6 @@ export const SecretItem = memo(
const { removePendingChange } = useBatchModeActions(); const { removePendingChange } = useBatchModeActions();
const [isFieldFocused, setIsFieldFocused] = useToggle(); const [isFieldFocused, setIsFieldFocused] = useToggle();
const queryClient = useQueryClient();
const canFetchSecretValue = const canFetchSecretValue =
!originalSecret.secretValueHidden && !originalSecret.secretValueHidden &&
@@ -142,7 +137,8 @@ export const SecretItem = memo(
const { const {
data: secretValueData, data: secretValueData,
isPending: isPendingSecretValueData, isPending: isPendingSecretValueData,
isError: isErrorFetchingSecretValue isError: isErrorFetchingSecretValue,
refetch: refetchSecretValueData
} = useGetSecretValue(fetchSecretValueParams, { } = useGetSecretValue(fetchSecretValueParams, {
enabled: canFetchSecretValue && (isVisible || isFieldFocused) enabled: canFetchSecretValue && (isVisible || isFieldFocused)
}); });
@@ -210,7 +206,6 @@ export const SecretItem = memo(
watch, watch,
setValue, setValue,
reset, reset,
getValues,
trigger, trigger,
formState: { isDirty, isSubmitting, errors }, formState: { isDirty, isSubmitting, errors },
getFieldState getFieldState
@@ -386,37 +381,25 @@ export const SecretItem = memo(
} }
}; };
const fetchValue = async () => { const fetchValue = async (): Promise<DashboardSecretValue | undefined> => {
if (secretValueData) return secretValueData; const { data, isRefetchError } = await refetchSecretValueData();
if (isRefetchError) {
try {
const data = await fetchSecretValue(fetchSecretValueParams);
queryClient.setQueryData(dashboardKeys.getSecretValue(fetchSecretValueParams), data);
return data;
} catch (e) {
console.error(e);
createNotification({ createNotification({
type: "error", type: "error",
text: "Failed to fetch secret value" text: "Failed to fetch secret value"
}); });
throw e;
} }
if (!data) return undefined;
return data;
}; };
const copyTokenToClipboard = async () => { const copyTokenToClipboard = async () => {
if (hasFetchedSecretValue) { const data = await fetchValue();
const [overrideValue, value] = getValues(["value", "valueOverride"]); if (!data) return;
if (isOverridden) {
navigator.clipboard.writeText(value as string); navigator.clipboard.writeText((data.valueOverride ?? data.value) as string);
} else {
navigator.clipboard.writeText(overrideValue as string);
}
} else {
const data = await fetchValue();
navigator.clipboard.writeText((data.valueOverride ?? data.value) as string);
}
setIsSecValueCopied.on(); setIsSecValueCopied.on();
}; };