2023-10-26 09:32:44 +00:00
|
|
|
|
import styled from '@emotion/styled';
|
2024-03-09 09:48:19 +00:00
|
|
|
|
import { isNonEmptyString } from '@sniptt/guards';
|
2023-11-16 17:14:04 +00:00
|
|
|
|
import { DateTime } from 'luxon';
|
2024-08-02 11:31:06 +00:00
|
|
|
|
import { useState } from 'react';
|
2025-01-18 12:58:12 +00:00
|
|
|
|
import { useParams } from 'react-router-dom';
|
2025-01-21 13:18:22 +00:00
|
|
|
|
import { useRecoilCallback, useRecoilValue } from 'recoil';
|
2024-10-28 12:08:55 +00:00
|
|
|
|
import { Button, H2Title, IconRepeat, IconTrash, Section } from 'twenty-ui';
|
2023-10-24 14:14:54 +00:00
|
|
|
|
|
2023-12-31 09:41:53 +00:00
|
|
|
|
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
2023-11-29 12:45:57 +00:00
|
|
|
|
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
|
|
|
|
|
|
import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord';
|
|
|
|
|
|
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
|
2023-10-24 14:14:54 +00:00
|
|
|
|
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
|
|
|
|
|
|
import { ApiKeyInput } from '@/settings/developers/components/ApiKeyInput';
|
2024-08-02 11:31:06 +00:00
|
|
|
|
import { ApiKeyNameInput } from '@/settings/developers/components/ApiKeyNameInput';
|
2025-01-21 13:18:22 +00:00
|
|
|
|
import { apiKeyTokenFamilyState } from '@/settings/developers/states/apiKeyTokenFamilyState';
|
2024-02-08 12:02:37 +00:00
|
|
|
|
import { ApiKey } from '@/settings/developers/types/api-key/ApiKey';
|
2024-10-20 18:20:19 +00:00
|
|
|
|
import { computeNewExpirationDate } from '@/settings/developers/utils/computeNewExpirationDate';
|
|
|
|
|
|
import { formatExpiration } from '@/settings/developers/utils/formatExpiration';
|
2024-09-17 16:29:00 +00:00
|
|
|
|
import { SettingsPath } from '@/types/SettingsPath';
|
2024-09-18 08:48:49 +00:00
|
|
|
|
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
|
|
|
|
|
|
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
2023-10-24 14:14:54 +00:00
|
|
|
|
import { TextInput } from '@/ui/input/components/TextInput';
|
2024-02-21 17:39:37 +00:00
|
|
|
|
import { ConfirmationModal } from '@/ui/layout/modal/components/ConfirmationModal';
|
2024-10-20 18:20:19 +00:00
|
|
|
|
import { SubMenuTopBarContainer } from '@/ui/layout/page/components/SubMenuTopBarContainer';
|
2025-01-19 12:29:19 +00:00
|
|
|
|
import { Trans, useLingui } from '@lingui/react/macro';
|
2023-11-19 17:25:47 +00:00
|
|
|
|
import { useGenerateApiKeyTokenMutation } from '~/generated/graphql';
|
2025-01-18 12:58:12 +00:00
|
|
|
|
import { useNavigateSettings } from '~/hooks/useNavigateSettings';
|
|
|
|
|
|
import { getSettingsPath } from '~/utils/navigation/getSettingsPath';
|
2023-10-24 14:14:54 +00:00
|
|
|
|
|
2023-10-26 09:32:44 +00:00
|
|
|
|
const StyledInfo = styled.span`
|
|
|
|
|
|
color: ${({ theme }) => theme.font.color.light};
|
|
|
|
|
|
font-size: ${({ theme }) => theme.font.size.sm};
|
|
|
|
|
|
font-weight: ${({ theme }) => theme.font.weight.regular};
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const StyledInputContainer = styled.div`
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
|
gap: ${({ theme }) => theme.spacing(2)};
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
2023-10-24 14:14:54 +00:00
|
|
|
|
export const SettingsDevelopersApiKeyDetail = () => {
|
2025-01-19 12:29:19 +00:00
|
|
|
|
const { t } = useLingui();
|
2024-09-18 08:48:49 +00:00
|
|
|
|
const { enqueueSnackBar } = useSnackBar();
|
2024-02-21 17:39:37 +00:00
|
|
|
|
const [isRegenerateKeyModalOpen, setIsRegenerateKeyModalOpen] =
|
|
|
|
|
|
useState(false);
|
|
|
|
|
|
const [isDeleteApiKeyModalOpen, setIsDeleteApiKeyModalOpen] = useState(false);
|
2024-09-18 08:48:49 +00:00
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
2024-02-21 17:39:37 +00:00
|
|
|
|
|
2025-01-18 12:58:12 +00:00
|
|
|
|
const navigate = useNavigateSettings();
|
2023-10-24 14:14:54 +00:00
|
|
|
|
const { apiKeyId = '' } = useParams();
|
2023-10-26 09:32:44 +00:00
|
|
|
|
|
2025-01-21 13:18:22 +00:00
|
|
|
|
const apiKeyToken = useRecoilValue(apiKeyTokenFamilyState(apiKeyId));
|
|
|
|
|
|
|
|
|
|
|
|
const setApiKeyTokenCallback = useRecoilCallback(
|
|
|
|
|
|
({ set }) =>
|
|
|
|
|
|
(apiKeyId: string, token: string) => {
|
|
|
|
|
|
set(apiKeyTokenFamilyState(apiKeyId), token);
|
|
|
|
|
|
},
|
|
|
|
|
|
[],
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2023-11-19 17:25:47 +00:00
|
|
|
|
const [generateOneApiKeyToken] = useGenerateApiKeyTokenMutation();
|
2023-11-29 12:45:57 +00:00
|
|
|
|
const { createOneRecord: createOneApiKey } = useCreateOneRecord<ApiKey>({
|
2023-12-29 14:11:30 +00:00
|
|
|
|
objectNameSingular: CoreObjectNameSingular.ApiKey,
|
2023-11-29 12:45:57 +00:00
|
|
|
|
});
|
|
|
|
|
|
const { updateOneRecord: updateApiKey } = useUpdateOneRecord<ApiKey>({
|
2023-12-29 14:11:30 +00:00
|
|
|
|
objectNameSingular: CoreObjectNameSingular.ApiKey,
|
2023-11-16 17:14:04 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-08-02 11:31:06 +00:00
|
|
|
|
const [apiKeyName, setApiKeyName] = useState('');
|
|
|
|
|
|
|
|
|
|
|
|
const { record: apiKeyData, loading } = useFindOneRecord({
|
2023-12-29 14:11:30 +00:00
|
|
|
|
objectNameSingular: CoreObjectNameSingular.ApiKey,
|
2023-11-17 08:45:48 +00:00
|
|
|
|
objectRecordId: apiKeyId,
|
2024-08-02 11:31:06 +00:00
|
|
|
|
onCompleted: (record) => {
|
|
|
|
|
|
setApiKeyName(record.name);
|
|
|
|
|
|
},
|
2023-11-16 17:14:04 +00:00
|
|
|
|
});
|
2023-10-26 09:32:44 +00:00
|
|
|
|
|
|
|
|
|
|
const deleteIntegration = async (redirect = true) => {
|
2024-09-18 08:48:49 +00:00
|
|
|
|
setIsLoading(true);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
await updateApiKey?.({
|
|
|
|
|
|
idToUpdate: apiKeyId,
|
|
|
|
|
|
updateOneRecordInput: { revokedAt: DateTime.now().toString() },
|
|
|
|
|
|
});
|
|
|
|
|
|
if (redirect) {
|
2025-03-07 17:03:57 +00:00
|
|
|
|
navigate(SettingsPath.APIs);
|
2024-09-18 08:48:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
2025-01-19 12:29:19 +00:00
|
|
|
|
enqueueSnackBar(t`Error deleting api key: ${err}`, {
|
2024-09-18 08:48:49 +00:00
|
|
|
|
variant: SnackBarVariant.Error,
|
|
|
|
|
|
});
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsLoading(false);
|
2023-10-26 09:32:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-10-30 16:48:34 +00:00
|
|
|
|
const createIntegration = async (
|
|
|
|
|
|
name: string,
|
|
|
|
|
|
newExpiresAt: string | null,
|
|
|
|
|
|
) => {
|
2023-11-16 17:14:04 +00:00
|
|
|
|
const newApiKey = await createOneApiKey?.({
|
|
|
|
|
|
name: name,
|
2024-01-23 17:13:00 +00:00
|
|
|
|
expiresAt: newExpiresAt ?? '',
|
2023-11-16 17:14:04 +00:00
|
|
|
|
});
|
2023-11-17 15:24:58 +00:00
|
|
|
|
|
|
|
|
|
|
if (!newApiKey) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-16 17:14:04 +00:00
|
|
|
|
const tokenData = await generateOneApiKeyToken({
|
2023-10-30 16:48:34 +00:00
|
|
|
|
variables: {
|
2023-11-19 17:25:47 +00:00
|
|
|
|
apiKeyId: newApiKey.id,
|
|
|
|
|
|
expiresAt: newApiKey?.expiresAt,
|
2023-10-30 16:48:34 +00:00
|
|
|
|
},
|
|
|
|
|
|
});
|
2023-11-16 17:14:04 +00:00
|
|
|
|
return {
|
2023-11-17 15:24:58 +00:00
|
|
|
|
id: newApiKey.id,
|
2023-11-19 17:25:47 +00:00
|
|
|
|
token: tokenData.data?.generateApiKeyToken.token,
|
2023-11-16 17:14:04 +00:00
|
|
|
|
};
|
2023-10-30 16:48:34 +00:00
|
|
|
|
};
|
2025-01-18 12:58:12 +00:00
|
|
|
|
|
2023-10-26 09:32:44 +00:00
|
|
|
|
const regenerateApiKey = async () => {
|
2024-09-18 08:48:49 +00:00
|
|
|
|
setIsLoading(true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (isNonEmptyString(apiKeyData?.name)) {
|
|
|
|
|
|
const newExpiresAt = computeNewExpirationDate(
|
|
|
|
|
|
apiKeyData?.expiresAt,
|
|
|
|
|
|
apiKeyData?.createdAt,
|
|
|
|
|
|
);
|
|
|
|
|
|
const apiKey = await createIntegration(apiKeyData?.name, newExpiresAt);
|
|
|
|
|
|
await deleteIntegration(false);
|
2023-11-17 15:24:58 +00:00
|
|
|
|
|
2024-09-18 08:48:49 +00:00
|
|
|
|
if (isNonEmptyString(apiKey?.token)) {
|
2025-01-21 13:18:22 +00:00
|
|
|
|
setApiKeyTokenCallback(apiKey.id, apiKey.token);
|
2025-01-18 12:58:12 +00:00
|
|
|
|
navigate(SettingsPath.DevelopersApiKeyDetail, {
|
|
|
|
|
|
apiKeyId: apiKey.id,
|
|
|
|
|
|
});
|
2024-09-18 08:48:49 +00:00
|
|
|
|
}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
}
|
2024-09-18 08:48:49 +00:00
|
|
|
|
} catch (err) {
|
2025-01-19 12:29:19 +00:00
|
|
|
|
enqueueSnackBar(t`Error regenerating api key: ${err}`, {
|
2024-09-18 08:48:49 +00:00
|
|
|
|
variant: SnackBarVariant.Error,
|
|
|
|
|
|
});
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsLoading(false);
|
2023-10-26 09:32:44 +00:00
|
|
|
|
}
|
2023-10-24 14:14:54 +00:00
|
|
|
|
};
|
2023-10-26 09:32:44 +00:00
|
|
|
|
|
2025-01-19 12:29:19 +00:00
|
|
|
|
const confirmationValue = t`yes`;
|
|
|
|
|
|
|
2023-10-24 14:14:54 +00:00
|
|
|
|
return (
|
2023-10-26 09:32:44 +00:00
|
|
|
|
<>
|
|
|
|
|
|
{apiKeyData?.name && (
|
2024-08-16 19:20:02 +00:00
|
|
|
|
<SubMenuTopBarContainer
|
2024-09-17 16:29:00 +00:00
|
|
|
|
title={apiKeyData?.name}
|
|
|
|
|
|
links={[
|
|
|
|
|
|
{
|
2025-01-19 12:29:19 +00:00
|
|
|
|
children: t`Workspace`,
|
2025-01-18 12:58:12 +00:00
|
|
|
|
href: getSettingsPath(SettingsPath.Workspace),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
2025-03-07 17:03:57 +00:00
|
|
|
|
children: t`APIs`,
|
|
|
|
|
|
href: getSettingsPath(SettingsPath.APIs),
|
2024-09-17 16:29:00 +00:00
|
|
|
|
},
|
2025-01-19 12:29:19 +00:00
|
|
|
|
{ children: t`${apiKeyName} API Key` },
|
2024-09-17 16:29:00 +00:00
|
|
|
|
]}
|
2024-08-16 19:20:02 +00:00
|
|
|
|
>
|
2023-10-26 09:32:44 +00:00
|
|
|
|
<SettingsPageContainer>
|
|
|
|
|
|
<Section>
|
2024-08-02 11:31:06 +00:00
|
|
|
|
{apiKeyToken ? (
|
2023-10-26 09:32:44 +00:00
|
|
|
|
<>
|
|
|
|
|
|
<H2Title
|
2025-01-19 12:29:19 +00:00
|
|
|
|
title={t`API Key`}
|
2025-01-21 13:18:22 +00:00
|
|
|
|
description={t`Copy this key as it will not be visible again`}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
/>
|
2024-08-02 11:31:06 +00:00
|
|
|
|
<ApiKeyInput apiKey={apiKeyToken} />
|
2023-10-26 09:32:44 +00:00
|
|
|
|
</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<H2Title
|
2025-01-19 12:29:19 +00:00
|
|
|
|
title={t`API Key`}
|
|
|
|
|
|
description={t`Regenerate an API key`}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
/>
|
|
|
|
|
|
<StyledInputContainer>
|
|
|
|
|
|
<Button
|
2025-01-19 12:29:19 +00:00
|
|
|
|
title={t`Regenerate Key`}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
Icon={IconRepeat}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
onClick={() => setIsRegenerateKeyModalOpen(true)}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
/>
|
|
|
|
|
|
<StyledInfo>
|
|
|
|
|
|
{formatExpiration(
|
|
|
|
|
|
apiKeyData?.expiresAt || '',
|
|
|
|
|
|
true,
|
|
|
|
|
|
false,
|
|
|
|
|
|
)}
|
|
|
|
|
|
</StyledInfo>
|
|
|
|
|
|
</StyledInputContainer>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</Section>
|
|
|
|
|
|
<Section>
|
2025-01-19 12:29:19 +00:00
|
|
|
|
<H2Title title={t`Name`} description={t`Name of your API key`} />
|
2024-08-02 11:31:06 +00:00
|
|
|
|
<ApiKeyNameInput
|
|
|
|
|
|
apiKeyName={apiKeyName}
|
|
|
|
|
|
apiKeyId={apiKeyData?.id}
|
|
|
|
|
|
disabled={loading}
|
|
|
|
|
|
onNameUpdate={setApiKeyName}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Section>
|
|
|
|
|
|
<Section>
|
|
|
|
|
|
<H2Title
|
2025-01-19 12:29:19 +00:00
|
|
|
|
title={t`Expiration`}
|
|
|
|
|
|
description={t`When the key will be disabled`}
|
2024-08-02 11:31:06 +00:00
|
|
|
|
/>
|
2023-10-26 09:32:44 +00:00
|
|
|
|
<TextInput
|
2025-01-19 12:29:19 +00:00
|
|
|
|
placeholder={t`E.g. backoffice integration`}
|
2024-08-02 11:31:06 +00:00
|
|
|
|
value={formatExpiration(
|
|
|
|
|
|
apiKeyData?.expiresAt || '',
|
|
|
|
|
|
true,
|
|
|
|
|
|
false,
|
|
|
|
|
|
)}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
disabled
|
|
|
|
|
|
fullWidth
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Section>
|
|
|
|
|
|
<Section>
|
|
|
|
|
|
<H2Title
|
2025-01-19 12:29:19 +00:00
|
|
|
|
title={t`Danger zone`}
|
|
|
|
|
|
description={t`Delete this integration`}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
/>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
accent="danger"
|
|
|
|
|
|
variant="secondary"
|
2025-01-19 12:29:19 +00:00
|
|
|
|
title={t`Delete`}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
Icon={IconTrash}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
onClick={() => setIsDeleteApiKeyModalOpen(true)}
|
2023-10-26 09:32:44 +00:00
|
|
|
|
/>
|
|
|
|
|
|
</Section>
|
|
|
|
|
|
</SettingsPageContainer>
|
|
|
|
|
|
</SubMenuTopBarContainer>
|
|
|
|
|
|
)}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
<ConfirmationModal
|
2025-01-19 12:29:19 +00:00
|
|
|
|
confirmationPlaceholder={confirmationValue}
|
|
|
|
|
|
confirmationValue={confirmationValue}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
isOpen={isDeleteApiKeyModalOpen}
|
|
|
|
|
|
setIsOpen={setIsDeleteApiKeyModalOpen}
|
2025-01-19 12:29:19 +00:00
|
|
|
|
title={t`Delete API key`}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
subtitle={
|
2025-01-19 12:29:19 +00:00
|
|
|
|
<Trans>
|
|
|
|
|
|
Please type {`"${confirmationValue}"`} to confirm you want to delete
|
|
|
|
|
|
this API Key. Be aware that any script using this key will stop
|
|
|
|
|
|
working.
|
|
|
|
|
|
</Trans>
|
2024-02-21 17:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
onConfirmClick={deleteIntegration}
|
2025-03-04 16:27:14 +00:00
|
|
|
|
confirmButtonText={t`Delete`}
|
2024-09-18 08:48:49 +00:00
|
|
|
|
loading={isLoading}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
/>
|
|
|
|
|
|
<ConfirmationModal
|
2025-01-19 12:29:19 +00:00
|
|
|
|
confirmationPlaceholder={confirmationValue}
|
|
|
|
|
|
confirmationValue={confirmationValue}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
isOpen={isRegenerateKeyModalOpen}
|
|
|
|
|
|
setIsOpen={setIsRegenerateKeyModalOpen}
|
2025-01-19 12:29:19 +00:00
|
|
|
|
title={t`Regenerate an API key`}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
subtitle={
|
2025-01-19 12:29:19 +00:00
|
|
|
|
<Trans>
|
2024-02-21 17:39:37 +00:00
|
|
|
|
If you’ve lost this key, you can regenerate it, but be aware that
|
2025-01-19 12:29:19 +00:00
|
|
|
|
any script using this key will need to be updated. Please type
|
|
|
|
|
|
{`"${confirmationValue}"`} to confirm.
|
|
|
|
|
|
</Trans>
|
2024-02-21 17:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
onConfirmClick={regenerateApiKey}
|
2025-03-04 16:27:14 +00:00
|
|
|
|
confirmButtonText={t`Regenerate key`}
|
2024-09-18 08:48:49 +00:00
|
|
|
|
loading={isLoading}
|
2024-02-21 17:39:37 +00:00
|
|
|
|
/>
|
2023-10-26 09:32:44 +00:00
|
|
|
|
</>
|
2023-10-24 14:14:54 +00:00
|
|
|
|
);
|
|
|
|
|
|
};
|