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,43 +108,27 @@ 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 });
}); if (cb) cb();
if (cb) cb(); createNotification({
createNotification({ 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,27 +39,19 @@ 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({
projectId: currentProject.id, projectId: currentProject.id,
id id
}); });
createNotification({ createNotification({
text: "Successfully deleted environment", text: "Successfully deleted environment",
type: "success" type: "success"
}); });
handlePopUpClose("deleteEnv"); handlePopUpClose("deleteEnv");
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete environment",
type: "error"
});
}
}; };
return ( return (
@@ -46,26 +46,18 @@ 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({
projectId: currentProject.id, projectId: currentProject.id,
id, id,
position position
}); });
createNotification({ createNotification({
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,22 +55,15 @@ 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) });
});
createNotification({ createNotification({
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,83 +59,51 @@ 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 });
}); handlePopUpClose("addWebhook");
handlePopUpClose("addWebhook"); createNotification({
createNotification({ 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, isDisabled
isDisabled });
}); createNotification({
createNotification({ 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, projectId
projectId });
}); handlePopUpClose("deleteWebhook");
handlePopUpClose("deleteWebhook"); createNotification({
createNotification({ 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 });
}); createNotification({
createNotification({ 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,37 +130,30 @@ export const MicrosoftTeamsIntegrationForm = ({ onClose }: Props) => {
}); });
const handleIntegrationSave = async (data: TMicrosoftTeamsConfigForm) => { const handleIntegrationSave = async (data: TMicrosoftTeamsConfigForm) => {
try { if (!currentProject) {
if (!currentProject) { return;
return;
}
await updateProjectMicrosoftTeamsConfig({
projectId: currentProject.id,
isAccessRequestNotificationEnabled: data.isAccessRequestNotificationEnabled,
isSecretRequestNotificationEnabled: data.isSecretRequestNotificationEnabled,
...(data.isAccessRequestNotificationEnabled && {
accessRequestChannels: data.accessRequestChannels
}),
...(data.isSecretRequestNotificationEnabled && {
secretRequestChannels: data.secretRequestChannels
}),
integration: WorkflowIntegrationPlatform.MICROSOFT_TEAMS,
integrationId: data.microsoftTeamsIntegrationId
});
createNotification({
type: "success",
text: "Successfully created microsoft teams integration"
});
onClose();
} catch {
createNotification({
type: "error",
text: "Failed to create microsoft teams integration"
});
} }
await updateProjectMicrosoftTeamsConfig({
projectId: currentProject.id,
isAccessRequestNotificationEnabled: data.isAccessRequestNotificationEnabled,
isSecretRequestNotificationEnabled: data.isSecretRequestNotificationEnabled,
...(data.isAccessRequestNotificationEnabled && {
accessRequestChannels: data.accessRequestChannels
}),
...(data.isSecretRequestNotificationEnabled && {
secretRequestChannels: data.secretRequestChannels
}),
integration: WorkflowIntegrationPlatform.MICROSOFT_TEAMS,
integrationId: data.microsoftTeamsIntegrationId
});
createNotification({
type: "success",
text: "Successfully created microsoft teams integration"
});
onClose();
}; };
const selectedAccessRequestTeamId = watch("accessRequestChannels.teamId"); const selectedAccessRequestTeamId = watch("accessRequestChannels.teamId");
@@ -22,23 +22,16 @@ 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, projectId: dataSource.projectId
projectId: dataSource.projectId });
});
createNotification({ createNotification({
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,44 +184,30 @@ 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, isAutoScanEnabled,
isAutoScanEnabled, projectId: dataSource.projectId
projectId: dataSource.projectId });
});
createNotification({ createNotification({
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, projectId: dataSource.projectId
projectId: dataSource.projectId });
});
createNotification({ createNotification({
text: "Successfully triggered scan", text: "Successfully triggered scan",
type: "success" type: "success"
}); });
} catch {
createNotification({
text: "Failed to trigger scan",
type: "error"
});
}
}; };
return ( return (
@@ -47,24 +47,16 @@ 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, defaultHostSshCaId: defaultHostSshCaId || undefined
defaultHostSshCaId: defaultHostSshCaId || undefined });
});
createNotification({ createNotification({
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,30 +43,22 @@ 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 });
createNotification({ createNotification({
text: "Successfully deleted SSH CA", text: "Successfully deleted SSH CA",
type: "success" type: "success"
}); });
handlePopUpClose("deleteSshCa"); handlePopUpClose("deleteSshCa");
navigate({ navigate({
to: "/projects/ssh/$projectId/overview", to: "/projects/ssh/$projectId/overview",
params: { params: {
projectId projectId
} }
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete SSH CA",
type: "error"
});
}
}; };
return ( return (
@@ -122,65 +122,57 @@ 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;
switch (operation) { switch (operation) {
case SshCertificateOperation.SIGN_SSH_KEY: { case SshCertificateOperation.SIGN_SSH_KEY: {
const { serialNumber, signedKey } = await signSshKey({ const { serialNumber, signedKey } = await signSshKey({
projectId, projectId,
certificateTemplateId: templateData.id, certificateTemplateId: templateData.id,
publicKey: existingPublicKey, publicKey: existingPublicKey,
certType, certType,
principals: principals.split(",").map((user) => user.trim()), principals: principals.split(",").map((user) => user.trim()),
ttl, ttl,
keyId keyId
}); });
setCertificateDetails({ setCertificateDetails({
serialNumber, serialNumber,
signedKey signedKey
}); });
break; break;
}
case SshCertificateOperation.ISSUE_SSH_CREDS: {
const { serialNumber, publicKey, privateKey, signedKey } = await issueSshCreds({
projectId,
certificateTemplateId: templateData.id,
keyAlgorithm,
certType,
principals: principals.split(",").map((user) => user.trim()),
ttl,
keyId
});
setCertificateDetails({
serialNumber,
privateKey,
publicKey,
signedKey
});
break;
}
default: {
break;
}
} }
case SshCertificateOperation.ISSUE_SSH_CREDS: {
const { serialNumber, publicKey, privateKey, signedKey } = await issueSshCreds({
projectId,
certificateTemplateId: templateData.id,
keyAlgorithm,
certType,
principals: principals.split(",").map((user) => user.trim()),
ttl,
keyId
});
reset(); setCertificateDetails({
serialNumber,
createNotification({ privateKey,
text: "Successfully created SSH certificate", publicKey,
type: "success" signedKey
}); });
} catch (err) { break;
console.error(err); }
createNotification({ default: {
text: "Failed to create SSH certificate", break;
type: "error" }
});
} }
reset();
createNotification({
text: "Successfully created SSH certificate",
type: "success"
});
}; };
return ( return (
@@ -138,52 +138,44 @@ 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, name,
name, ttl,
ttl, maxTTL,
maxTTL, allowedUsers: allowedUsers ? allowedUsers.split(",").map((user) => user.trim()) : [],
allowedUsers: allowedUsers ? allowedUsers.split(",").map((user) => user.trim()) : [], allowedHosts: allowedHosts ? allowedHosts.split(",").map((host) => host.trim()) : [],
allowedHosts: allowedHosts ? allowedHosts.split(",").map((host) => host.trim()) : [], allowUserCertificates,
allowUserCertificates, allowHostCertificates,
allowHostCertificates, allowCustomKeyIds
allowCustomKeyIds });
});
createNotification({
text: "Successfully updated SSH certificate template",
type: "success"
});
} else {
await createSshCertTemplate({
sshCaId,
name,
ttl,
maxTTL,
allowedUsers: allowedUsers ? allowedUsers.split(",").map((user) => user.trim()) : [],
allowedHosts: allowedHosts ? allowedHosts.split(",").map((host) => host.trim()) : [],
allowUserCertificates,
allowHostCertificates,
allowCustomKeyIds
});
createNotification({
text: "Successfully created SSH certificate template",
type: "success"
});
}
reset();
handlePopUpToggle("sshCertificateTemplate", false);
} catch (err) {
console.error(err);
createNotification({ createNotification({
text: "Failed to save changes", text: "Successfully updated SSH certificate template",
type: "error" type: "success"
});
} else {
await createSshCertTemplate({
sshCaId,
name,
ttl,
maxTTL,
allowedUsers: allowedUsers ? allowedUsers.split(",").map((user) => user.trim()) : [],
allowedHosts: allowedHosts ? allowedHosts.split(",").map((host) => host.trim()) : [],
allowUserCertificates,
allowHostCertificates,
allowCustomKeyIds
});
createNotification({
text: "Successfully created SSH certificate template",
type: "success"
}); });
} }
reset();
handlePopUpToggle("sshCertificateTemplate", false);
}; };
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,26 +52,16 @@ 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`,
type: "success" type: "success"
}); });
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,47 +106,39 @@ export const SshCaModal = ({ popUp, handlePopUpToggle }: Props) => {
publicKey, publicKey,
privateKey privateKey
}: FormData) => { }: FormData) => {
try { if (!projectId) return;
if (!projectId) return;
if (ca) { if (ca) {
await updateMutateAsync({ await updateMutateAsync({
caId: ca.id, caId: ca.id,
friendlyName friendlyName
});
} else {
const { id: newCaId } = await createMutateAsync({
projectId,
friendlyName,
keySource,
keyAlgorithm,
publicKey,
privateKey
});
navigate({
to: "/projects/ssh/$projectId/ca/$caId",
params: {
projectId,
caId: newCaId
}
});
}
reset();
handlePopUpToggle("sshCa", false);
createNotification({
text: `Successfully ${ca ? "updated" : "created"} SSH CA`,
type: "success"
}); });
} catch (err) { } else {
console.error(err); const { id: newCaId } = await createMutateAsync({
createNotification({ projectId,
text: `Failed to ${ca ? "update" : "create"} SSH CA`, friendlyName,
type: "error" keySource,
keyAlgorithm,
publicKey,
privateKey
});
navigate({
to: "/projects/ssh/$projectId/ca/$caId",
params: {
projectId,
caId: newCaId
}
}); });
} }
reset();
handlePopUpToggle("sshCa", false);
createNotification({
text: `Successfully ${ca ? "updated" : "created"} SSH CA`,
type: "success"
});
}; };
return ( return (
@@ -23,41 +23,25 @@ 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({
text: "Successfully deleted SSH CA", text: "Successfully deleted SSH CA",
type: "success" type: "success"
}); });
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({
text: `Successfully ${status === SshCaStatus.ACTIVE ? "enabled" : "disabled"} SSH CA`, text: `Successfully ${status === SshCaStatus.ACTIVE ? "enabled" : "disabled"} SSH CA`,
type: "success" type: "success"
}); });
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,30 +44,22 @@ 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 });
createNotification({ createNotification({
text: "Successfully deleted SSH group", text: "Successfully deleted SSH group",
type: "success" type: "success"
}); });
handlePopUpClose("deleteSshHostGroup"); handlePopUpClose("deleteSshHostGroup");
navigate({ navigate({
to: "/projects/ssh/$projectId/overview", to: "/projects/ssh/$projectId/overview",
params: { params: {
projectId projectId
} }
}); });
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete SSH group",
type: "error"
});
}
}; };
return ( return (
@@ -42,30 +42,23 @@ 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({
text: "Some data is missing, please refresh the page and try again",
type: "error"
});
return;
}
await addHostToSshHostGroup({
sshHostGroupId: popUpData.sshHostGroupId,
sshHostId
});
createNotification({ createNotification({
text: "Successfully added host to the group", text: "Some data is missing, please refresh the page and try again",
type: "success"
});
} catch {
createNotification({
text: "Failed to add host to the group",
type: "error" type: "error"
}); });
return;
} }
await addHostToSshHostGroup({
sshHostGroupId: popUpData.sshHostGroupId,
sshHostId
});
createNotification({
text: "Successfully added host to the group",
type: "success"
});
}; };
return ( return (
@@ -121,67 +121,59 @@ 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
const existingNames = const existingNames =
sshHostGroups?.filter((h) => h.id !== sshHostGroup?.id).map((h) => h.name) || []; sshHostGroups?.filter((h) => h.id !== sshHostGroup?.id).map((h) => h.name) || [];
if (existingNames.includes(name.trim())) {
createNotification({
text: "A host group with this name already exists.",
type: "error"
});
return;
}
const transformedLoginMappings = loginMappings.map(({ loginUser, allowedPrincipals }) => {
const usernames = allowedPrincipals
.filter((p) => p.type === "user" && p.value)
.map((p) => p.value);
const groupNames = allowedPrincipals
.filter((p) => p.type === "group" && p.value)
.map((p) => p.value);
return {
loginUser,
allowedPrincipals: {
usernames,
groups: groupNames
}
};
});
if (sshHostGroup) {
await updateMutateAsync({
sshHostGroupId: sshHostGroup.id,
name,
loginMappings: transformedLoginMappings
});
} else {
await createMutateAsync({
projectId,
name,
loginMappings: transformedLoginMappings
});
}
reset();
handlePopUpToggle("sshHostGroup", false);
if (existingNames.includes(name.trim())) {
createNotification({ createNotification({
text: `Successfully ${sshHostGroup ? "updated" : "created"} SSH host group`, text: "A host group with this name already exists.",
type: "success"
});
} catch (err) {
console.error(err);
createNotification({
text: `Failed to ${sshHostGroup ? "update" : "create"} SSH host group`,
type: "error" type: "error"
}); });
return;
} }
const transformedLoginMappings = loginMappings.map(({ loginUser, allowedPrincipals }) => {
const usernames = allowedPrincipals
.filter((p) => p.type === "user" && p.value)
.map((p) => p.value);
const groupNames = allowedPrincipals
.filter((p) => p.type === "group" && p.value)
.map((p) => p.value);
return {
loginUser,
allowedPrincipals: {
usernames,
groups: groupNames
}
};
});
if (sshHostGroup) {
await updateMutateAsync({
sshHostGroupId: sshHostGroup.id,
name,
loginMappings: transformedLoginMappings
});
} else {
await createMutateAsync({
projectId,
name,
loginMappings: transformedLoginMappings
});
}
reset();
handlePopUpToggle("sshHostGroup", false);
createNotification({
text: `Successfully ${sshHostGroup ? "updated" : "created"} SSH host group`,
type: "success"
});
}; };
const toggleMapping = (index: number) => { const toggleMapping = (index: number) => {
@@ -20,22 +20,14 @@ 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({
text: `Successfully deleted SSH host: ${host.hostname}`, text: `Successfully deleted SSH host: ${host.hostname}`,
type: "success" type: "success"
}); });
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,27 +76,19 @@ 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] });
});
setNewAPIKey(apiKey); setNewAPIKey(apiKey);
createNotification({ createNotification({
text: "Successfully created API key", text: "Successfully created API key",
type: "success" type: "success"
}); });
reset(); reset();
} catch (err) {
console.error(err);
createNotification({
text: "Failed to create API key",
type: "error"
});
}
}; };
const hasAPIKey = Boolean(newAPIKey); const hasAPIKey = Boolean(newAPIKey);