refactor: streamline async logic and notification handling across various settings and integration components

This commit is contained in:
Victor Santos
2025-11-03 15:43:08 -03:00
parent c005f8a0fe
commit f2f50f739e
22 changed files with 383 additions and 609 deletions
@@ -108,27 +108,18 @@ export const NativeIntegrationsTab = () => {
shouldDeleteIntegrationSecrets: boolean, shouldDeleteIntegrationSecrets: boolean,
cb: () => void cb: () => void
) => { ) => {
try {
await deleteIntegration({ id: integrationId, workspaceId, shouldDeleteIntegrationSecrets }); await deleteIntegration({ id: integrationId, workspaceId, shouldDeleteIntegrationSecrets });
if (cb) cb(); if (cb) cb();
createNotification({ createNotification({
type: "success", type: "success",
text: "Deleted integration" text: "Deleted integration"
}); });
} catch (err) {
console.log(err);
createNotification({
type: "error",
text: "Failed to delete integration"
});
}
}; };
const handleIntegrationAuthRevoke = async (provider: string, cb?: () => void) => { const handleIntegrationAuthRevoke = async (provider: string, cb?: () => void) => {
const integrationAuthForProvider = integrationAuths?.[provider]; const integrationAuthForProvider = integrationAuths?.[provider];
if (!integrationAuthForProvider) return; if (!integrationAuthForProvider) return;
try {
await deleteIntegrationAuths({ await deleteIntegrationAuths({
integration: provider, integration: provider,
workspaceId workspaceId
@@ -138,13 +129,6 @@ export const NativeIntegrationsTab = () => {
type: "success", type: "success",
text: "Revoked provider authentication" text: "Revoked provider authentication"
}); });
} catch (err) {
console.error(err);
createNotification({
type: "error",
text: "Failed to revoke provider authentication"
});
}
}; };
const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([ const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([
@@ -39,7 +39,6 @@ export const EnvironmentSection = () => {
] as const); ] as const);
const onEnvDeleteSubmit = async (id: string) => { const onEnvDeleteSubmit = async (id: string) => {
try {
if (!currentProject?.id) return; if (!currentProject?.id) return;
await deleteWsEnvironment.mutateAsync({ await deleteWsEnvironment.mutateAsync({
@@ -53,13 +52,6 @@ export const EnvironmentSection = () => {
}); });
handlePopUpClose("deleteEnv"); handlePopUpClose("deleteEnv");
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete environment",
type: "error"
});
}
}; };
return ( return (
@@ -46,7 +46,6 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {
const updateEnvironment = useUpdateWsEnvironment(); const updateEnvironment = useUpdateWsEnvironment();
const handleReorderEnv = async (id: string, position: number) => { const handleReorderEnv = async (id: string, position: number) => {
try {
if (!currentProject?.id) return; if (!currentProject?.id) return;
await updateEnvironment.mutateAsync({ await updateEnvironment.mutateAsync({
@@ -59,13 +58,6 @@ export const EnvironmentTable = ({ handlePopUpOpen }: Props) => {
text: "Successfully re-ordered environments", text: "Successfully re-ordered environments",
type: "success" type: "success"
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to re-order environments",
type: "error"
});
}
}; };
const isMoreEnvironmentsAllowed = const isMoreEnvironmentsAllowed =
@@ -55,7 +55,6 @@ export const SecretDetectionIgnoreValuesSection = () => {
}, [currentProject?.secretDetectionIgnoreValues, reset]); }, [currentProject?.secretDetectionIgnoreValues, reset]);
const handleIgnoreValuesSubmit = async ({ ignoreValues }: TForm) => { const handleIgnoreValuesSubmit = async ({ ignoreValues }: TForm) => {
try {
await updateProject({ await updateProject({
projectId: currentProject.id, projectId: currentProject.id,
secretDetectionIgnoreValues: ignoreValues.map((item) => item.value) secretDetectionIgnoreValues: ignoreValues.map((item) => item.value)
@@ -65,12 +64,6 @@ export const SecretDetectionIgnoreValuesSection = () => {
text: "Successfully updated secret detection ignore values", text: "Successfully updated secret detection ignore values",
type: "success" type: "success"
}); });
} catch {
createNotification({
text: "Failed updating secret detection ignore values",
type: "error"
});
}
}; };
const isAdmin = hasProjectRole(ProjectMembershipRole.Admin); const isAdmin = hasProjectRole(ProjectMembershipRole.Admin);
@@ -30,12 +30,6 @@ export const SecretSnapshotsLegacySection = () => {
text: `Successfully ${state ? "enabled" : "disabled"} secret snapshots legacy for this project`, text: `Successfully ${state ? "enabled" : "disabled"} secret snapshots legacy for this project`,
type: "success" type: "success"
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to update secret snapshots legacy for this project",
type: "error"
});
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }
@@ -59,7 +59,6 @@ export const WebhooksTab = withProjectPermission(
const { mutateAsync: deleteWebhook } = useDeleteWebhook(); const { mutateAsync: deleteWebhook } = useDeleteWebhook();
const handleWebhookCreate = async (data: TFormSchema) => { const handleWebhookCreate = async (data: TFormSchema) => {
try {
await createWebhook({ await createWebhook({
...data, ...data,
projectId projectId
@@ -69,17 +68,9 @@ export const WebhooksTab = withProjectPermission(
type: "success", type: "success",
text: "Successfully created webhook" text: "Successfully created webhook"
}); });
} catch (err) {
console.log(err);
createNotification({
type: "error",
text: "Failed to create webhook"
});
}
}; };
const handleWebhookDisable = async (webhookId: string, isDisabled: boolean) => { const handleWebhookDisable = async (webhookId: string, isDisabled: boolean) => {
try {
await updateWebhook({ await updateWebhook({
webhookId, webhookId,
projectId, projectId,
@@ -89,17 +80,9 @@ export const WebhooksTab = withProjectPermission(
type: "success", type: "success",
text: "Successfully updated webhook" text: "Successfully updated webhook"
}); });
} catch (err) {
console.log(err);
createNotification({
type: "error",
text: "Failed to update webhook"
});
}
}; };
const handleWebhookDelete = async () => { const handleWebhookDelete = async () => {
try {
const webhookId = popUp?.deleteWebhook?.data as string; const webhookId = popUp?.deleteWebhook?.data as string;
await deleteWebhook({ await deleteWebhook({
webhookId, webhookId,
@@ -110,17 +93,9 @@ export const WebhooksTab = withProjectPermission(
type: "success", type: "success",
text: "Successfully deleted webhook" text: "Successfully deleted webhook"
}); });
} catch (err) {
console.log(err);
createNotification({
type: "error",
text: "Failed to delete webhook"
});
}
}; };
const handleWebhookTest = async (webhookId: string) => { const handleWebhookTest = async (webhookId: string) => {
try {
await testWebhook({ await testWebhook({
webhookId, webhookId,
projectId projectId
@@ -129,13 +104,6 @@ export const WebhooksTab = withProjectPermission(
type: "success", type: "success",
text: "Successfully triggered webhook" text: "Successfully triggered webhook"
}); });
} catch (err) {
console.log(err);
createNotification({
type: "error",
text: "Failed to trigger webhook"
});
}
}; };
return ( return (
@@ -130,7 +130,6 @@ export const MicrosoftTeamsIntegrationForm = ({ onClose }: Props) => {
}); });
const handleIntegrationSave = async (data: TMicrosoftTeamsConfigForm) => { const handleIntegrationSave = async (data: TMicrosoftTeamsConfigForm) => {
try {
if (!currentProject) { if (!currentProject) {
return; return;
} }
@@ -155,12 +154,6 @@ export const MicrosoftTeamsIntegrationForm = ({ onClose }: Props) => {
}); });
onClose(); onClose();
} catch {
createNotification({
type: "error",
text: "Failed to create microsoft teams integration"
});
}
}; };
const selectedAccessRequestTeamId = watch("accessRequestChannels.teamId"); const selectedAccessRequestTeamId = watch("accessRequestChannels.teamId");
@@ -22,7 +22,6 @@ export const SecretScanningResourceSection = ({ dataSource }: Props) => {
const triggerDataSourceScan = useTriggerSecretScanningDataSource(); const triggerDataSourceScan = useTriggerSecretScanningDataSource();
const handleTriggerScan = async () => { const handleTriggerScan = async () => {
try {
await triggerDataSourceScan.mutateAsync({ await triggerDataSourceScan.mutateAsync({
dataSourceId: dataSource.id, dataSourceId: dataSource.id,
type: dataSource.type, type: dataSource.type,
@@ -33,12 +32,6 @@ export const SecretScanningResourceSection = ({ dataSource }: Props) => {
text: `Successfully triggered scan for ${dataSource.name}`, text: `Successfully triggered scan for ${dataSource.name}`,
type: "success" type: "success"
}); });
} catch {
createNotification({
text: `Failed to trigger scan for ${dataSource.name}`,
type: "error"
});
}
}; };
const resourceDetails = RESOURCE_DESCRIPTION_HELPER[dataSource.type]; const resourceDetails = RESOURCE_DESCRIPTION_HELPER[dataSource.type];
@@ -184,7 +184,6 @@ export const SecretScanningDataSourcesTable = ({ dataSources }: Props) => {
const isAutoScanEnabled = !dataSource.isAutoScanEnabled; const isAutoScanEnabled = !dataSource.isAutoScanEnabled;
try {
await updateDataSource.mutateAsync({ await updateDataSource.mutateAsync({
dataSourceId: dataSource.id, dataSourceId: dataSource.id,
type: dataSource.type, type: dataSource.type,
@@ -196,16 +195,9 @@ export const SecretScanningDataSourcesTable = ({ dataSources }: Props) => {
text: `Successfully ${isAutoScanEnabled ? "enabled" : "disabled"} auto-scan for ${destinationName} Data Source`, text: `Successfully ${isAutoScanEnabled ? "enabled" : "disabled"} auto-scan for ${destinationName} Data Source`,
type: "success" type: "success"
}); });
} catch {
createNotification({
text: `Failed to ${isAutoScanEnabled ? "enable" : "disable"} auto-scan for ${destinationName} Data Source`,
type: "error"
});
}
}; };
const handleTriggerScan = async (dataSource: TSecretScanningDataSource) => { const handleTriggerScan = async (dataSource: TSecretScanningDataSource) => {
try {
await triggerDataSourceScan.mutateAsync({ await triggerDataSourceScan.mutateAsync({
dataSourceId: dataSource.id, dataSourceId: dataSource.id,
type: dataSource.type, type: dataSource.type,
@@ -216,12 +208,6 @@ export const SecretScanningDataSourcesTable = ({ dataSources }: Props) => {
text: "Successfully triggered scan", text: "Successfully triggered scan",
type: "success" type: "success"
}); });
} catch {
createNotification({
text: "Failed to trigger scan",
type: "error"
});
}
}; };
return ( return (
@@ -47,7 +47,6 @@ export const ProjectSshConfigCasSection = () => {
}, [sshConfig]); }, [sshConfig]);
const onFormSubmit = async ({ defaultUserSshCaId, defaultHostSshCaId }: FormData) => { const onFormSubmit = async ({ defaultUserSshCaId, defaultHostSshCaId }: FormData) => {
try {
await updateProjectSshConfig({ await updateProjectSshConfig({
projectId: currentProject.id, projectId: currentProject.id,
defaultUserSshCaId: defaultUserSshCaId || undefined, defaultUserSshCaId: defaultUserSshCaId || undefined,
@@ -58,13 +57,6 @@ export const ProjectSshConfigCasSection = () => {
text: "Successfully updated SSH project settings", text: "Successfully updated SSH project settings",
type: "success" type: "success"
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to update SSH project settings",
type: "error"
});
}
}; };
return ( return (
@@ -43,7 +43,6 @@ const Page = () => {
] as const); ] as const);
const onRemoveCaSubmit = async (caIdToDelete: string) => { const onRemoveCaSubmit = async (caIdToDelete: string) => {
try {
if (!projectId) return; if (!projectId) return;
await deleteSshCa({ caId: caIdToDelete }); await deleteSshCa({ caId: caIdToDelete });
@@ -60,13 +59,6 @@ const Page = () => {
projectId projectId
} }
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete SSH CA",
type: "error"
});
}
}; };
return ( return (
@@ -122,7 +122,6 @@ export const SshCertificateModal = ({ popUp, handlePopUpToggle }: Props) => {
ttl, ttl,
keyId keyId
}: FormData) => { }: FormData) => {
try {
if (!templateData) return; if (!templateData) return;
if (!projectId) return; if (!projectId) return;
@@ -174,13 +173,6 @@ export const SshCertificateModal = ({ popUp, handlePopUpToggle }: Props) => {
text: "Successfully created SSH certificate", text: "Successfully created SSH certificate",
type: "success" type: "success"
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to create SSH certificate",
type: "error"
});
}
}; };
return ( return (
@@ -138,7 +138,6 @@ export const SshCertificateTemplateModal = ({ popUp, handlePopUpToggle, sshCaId
allowedHosts, allowedHosts,
allowCustomKeyIds allowCustomKeyIds
}: FormData) => { }: FormData) => {
try {
if (certTemplate) { if (certTemplate) {
await updateSshCertTemplate({ await updateSshCertTemplate({
id: certTemplate.id, id: certTemplate.id,
@@ -177,13 +176,6 @@ export const SshCertificateTemplateModal = ({ popUp, handlePopUpToggle, sshCaId
reset(); reset();
handlePopUpToggle("sshCertificateTemplate", false); handlePopUpToggle("sshCertificateTemplate", false);
} catch (err) {
console.error(err);
createNotification({
text: "Failed to save changes",
type: "error"
});
}
}; };
return ( return (
@@ -33,24 +33,16 @@ export const SshCertificateTemplatesSection = ({ caId }: Props) => {
const { mutateAsync: updateSshCertTemplate } = useUpdateSshCertTemplate(); const { mutateAsync: updateSshCertTemplate } = useUpdateSshCertTemplate();
const onRemoveSshCertificateTemplateSubmit = async (id: string) => { const onRemoveSshCertificateTemplateSubmit = async (id: string) => {
try {
await deleteSshCertTemplate({ await deleteSshCertTemplate({
id id
}); });
await createNotification({ createNotification({
text: "Successfully deleted SSH certificate template", text: "Successfully deleted SSH certificate template",
type: "success" type: "success"
}); });
handlePopUpClose("deleteSshCertificateTemplate"); handlePopUpClose("deleteSshCertificateTemplate");
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete SSH certificate template",
type: "error"
});
}
}; };
const onUpdateSshCaStatus = async ({ const onUpdateSshCaStatus = async ({
@@ -60,10 +52,9 @@ export const SshCertificateTemplatesSection = ({ caId }: Props) => {
templateId: string; templateId: string;
status: SshCertTemplateStatus; status: SshCertTemplateStatus;
}) => { }) => {
try {
await updateSshCertTemplate({ id: templateId, status }); await updateSshCertTemplate({ id: templateId, status });
await createNotification({ createNotification({
text: `Successfully ${ text: `Successfully ${
status === SshCertTemplateStatus.ACTIVE ? "enabled" : "disabled" status === SshCertTemplateStatus.ACTIVE ? "enabled" : "disabled"
} SSH certificate template`, } SSH certificate template`,
@@ -71,15 +62,6 @@ export const SshCertificateTemplatesSection = ({ caId }: Props) => {
}); });
handlePopUpClose("sshCertificateTemplateStatus"); handlePopUpClose("sshCertificateTemplateStatus");
} catch (err) {
console.error(err);
createNotification({
text: `Failed to ${
status === SshCertTemplateStatus.ACTIVE ? "enabled" : "disabled"
} SSH certificate template`,
type: "error"
});
}
}; };
return ( return (
@@ -106,7 +106,6 @@ export const SshCaModal = ({ popUp, handlePopUpToggle }: Props) => {
publicKey, publicKey,
privateKey privateKey
}: FormData) => { }: FormData) => {
try {
if (!projectId) return; if (!projectId) return;
if (ca) { if (ca) {
@@ -140,13 +139,6 @@ export const SshCaModal = ({ popUp, handlePopUpToggle }: Props) => {
text: `Successfully ${ca ? "updated" : "created"} SSH CA`, text: `Successfully ${ca ? "updated" : "created"} SSH CA`,
type: "success" type: "success"
}); });
} catch (err) {
console.error(err);
createNotification({
text: `Failed to ${ca ? "update" : "create"} SSH CA`,
type: "error"
});
}
}; };
return ( return (
@@ -23,7 +23,6 @@ export const SshCaSection = () => {
] as const); ] as const);
const onRemoveSshCaSubmit = async (caId: string) => { const onRemoveSshCaSubmit = async (caId: string) => {
try {
await deleteSshCa({ caId }); await deleteSshCa({ caId });
createNotification({ createNotification({
@@ -32,17 +31,9 @@ export const SshCaSection = () => {
}); });
handlePopUpClose("deleteSshCa"); handlePopUpClose("deleteSshCa");
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete SSH CA",
type: "error"
});
}
}; };
const onUpdateSshCaStatus = async ({ caId, status }: { caId: string; status: SshCaStatus }) => { const onUpdateSshCaStatus = async ({ caId, status }: { caId: string; status: SshCaStatus }) => {
try {
await updateSshCa({ caId, status }); await updateSshCa({ caId, status });
createNotification({ createNotification({
@@ -51,13 +42,6 @@ export const SshCaSection = () => {
}); });
handlePopUpClose("sshCaStatus"); handlePopUpClose("sshCaStatus");
} catch (err) {
console.error(err);
createNotification({
text: `Failed to ${status === SshCaStatus.ACTIVE ? "enabled" : "disabled"} SSH CA`,
type: "error"
});
}
}; };
return ( return (
@@ -44,7 +44,6 @@ const Page = () => {
] as const); ] as const);
const onRemoveSshGroupSubmit = async (groupIdToDelete: string) => { const onRemoveSshGroupSubmit = async (groupIdToDelete: string) => {
try {
if (!projectId) return; if (!projectId) return;
await deleteSshHostGroup({ sshHostGroupId: groupIdToDelete }); await deleteSshHostGroup({ sshHostGroupId: groupIdToDelete });
@@ -61,13 +60,6 @@ const Page = () => {
projectId projectId
} }
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete SSH group",
type: "error"
});
}
}; };
return ( return (
@@ -42,7 +42,6 @@ export const AddHostGroupMemberModal = ({ popUp, handlePopUpToggle }: Props) =>
useAddHostToSshHostGroup(); useAddHostToSshHostGroup();
const handleAddHost = async (sshHostId: string) => { const handleAddHost = async (sshHostId: string) => {
try {
if (!popUpData?.sshHostGroupId) { if (!popUpData?.sshHostGroupId) {
createNotification({ createNotification({
text: "Some data is missing, please refresh the page and try again", text: "Some data is missing, please refresh the page and try again",
@@ -60,12 +59,6 @@ export const AddHostGroupMemberModal = ({ popUp, handlePopUpToggle }: Props) =>
text: "Successfully added host to the group", text: "Successfully added host to the group",
type: "success" type: "success"
}); });
} catch {
createNotification({
text: "Failed to add host to the group",
type: "error"
});
}
}; };
return ( return (
@@ -121,7 +121,6 @@ export const SshHostGroupModal = ({ popUp, handlePopUpToggle }: Props) => {
}, [sshHostGroup]); }, [sshHostGroup]);
const onFormSubmit = async ({ name, loginMappings }: FormData) => { const onFormSubmit = async ({ name, loginMappings }: FormData) => {
try {
if (!projectId) return; if (!projectId) return;
// check if there is already a different host group with the same name // check if there is already a different host group with the same name
@@ -175,13 +174,6 @@ export const SshHostGroupModal = ({ popUp, handlePopUpToggle }: Props) => {
text: `Successfully ${sshHostGroup ? "updated" : "created"} SSH host group`, text: `Successfully ${sshHostGroup ? "updated" : "created"} SSH host group`,
type: "success" type: "success"
}); });
} catch (err) {
console.error(err);
createNotification({
text: `Failed to ${sshHostGroup ? "update" : "create"} SSH host group`,
type: "error"
});
}
}; };
const toggleMapping = (index: number) => { const toggleMapping = (index: number) => {
@@ -20,7 +20,6 @@ export const SshHostsSection = () => {
] as const); ] as const);
const onRemoveSshHostSubmit = async (sshHostId: string) => { const onRemoveSshHostSubmit = async (sshHostId: string) => {
try {
const host = await deleteSshHost({ sshHostId }); const host = await deleteSshHost({ sshHostId });
createNotification({ createNotification({
@@ -29,13 +28,6 @@ export const SshHostsSection = () => {
}); });
handlePopUpClose("deleteSshHost"); handlePopUpClose("deleteSshHost");
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete SSH host",
type: "error"
});
}
}; };
return ( return (
@@ -22,19 +22,11 @@ export const APIKeyTable = () => {
const { mutateAsync } = useDeleteAPIKey(); const { mutateAsync } = useDeleteAPIKey();
const handleDeleteAPIKeyDataClick = async (apiKeyDataId: string) => { const handleDeleteAPIKeyDataClick = async (apiKeyDataId: string) => {
try {
await mutateAsync(apiKeyDataId); await mutateAsync(apiKeyDataId);
createNotification({ createNotification({
text: "Successfully deleted API key", text: "Successfully deleted API key",
type: "success" type: "success"
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete API key",
type: "error"
});
}
}; };
return ( return (
@@ -76,7 +76,6 @@ export const AddAPIKeyModal = ({ popUp, handlePopUpToggle }: Props) => {
}; };
const onFormSubmit = async ({ name, expiresIn }: FormData) => { const onFormSubmit = async ({ name, expiresIn }: FormData) => {
try {
const { apiKey } = await mutateAsync({ const { apiKey } = await mutateAsync({
name, name,
expiresIn: expirationMapping[expiresIn] expiresIn: expirationMapping[expiresIn]
@@ -90,13 +89,6 @@ export const AddAPIKeyModal = ({ popUp, handlePopUpToggle }: Props) => {
}); });
reset(); reset();
} catch (err) {
console.error(err);
createNotification({
text: "Failed to create API key",
type: "error"
});
}
}; };
const hasAPIKey = Boolean(newAPIKey); const hasAPIKey = Boolean(newAPIKey);