A local-first personal finance app
// @ts-strict-ignore
import React, { useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { ButtonWithLoading } from '@actual-app/components/button';
import { Input } from '@actual-app/components/input';
import { Text } from '@actual-app/components/text';
import { Toggle } from '@actual-app/components/toggle';
import { View } from '@actual-app/components/view';
import { send } from '@actual-app/core/platform/client/connection';
import { Error } from '#components/alerts';
import { Link } from '#components/common/Link';
import {
Modal,
ModalButtons,
ModalCloseButton,
ModalHeader,
} from '#components/common/Modal';
import { FormField, FormLabel } from '#components/forms';
import { useCurrentAccess } from '#hooks/useCurrentAccess';
import type { Modal as ModalType } from '#modals/modalsSlice';
import { getSecretsError } from '#util/error';
type SimpleFinInitialiseModalProps = Extract<
ModalType,
{ name: 'simplefin-init' }
>['options'];
export const SimpleFinInitialiseModal = ({
onSuccess,
credentialSource,
}: SimpleFinInitialiseModalProps) => {
const { t } = useTranslation();
const { cloudFileId, isAdmin } = useCurrentAccess();
const [perBudgetFile, setPerBudgetFile] = useState(
credentialSource === 'per-budget-file' || !isAdmin,
);
const [token, setToken] = useState('');
const [isValid, setIsValid] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(t('It is required to provide a token.'));
const onSubmit = async (close: () => void) => {
if (!token) {
setIsValid(false);
return;
}
const fileId = perBudgetFile ? cloudFileId : null;
if (perBudgetFile && !fileId) {
setIsValid(false);
setError(t('Budget file ID is required.'));
return;
}
setIsLoading(true);
let { error, reason } =
(await send('secret-set', {
name: 'simplefin_token',
value: token,
fileId,
})) || {};
if (!error) {
({ error, reason } =
(await send('secret-set', {
name: 'simplefin_accessKey',
value: null,
fileId,
})) || {});
}
if (
!error &&
!perBudgetFile &&
credentialSource === 'per-budget-file' &&
cloudFileId
) {
for (const name of ['simplefin_token', 'simplefin_accessKey']) {
({ error, reason } =
(await send('secret-set', {
name,
value: null,
fileId: cloudFileId,
})) || {});
if (error) {
break;
}
}
}
setIsLoading(false);
if (error) {
setIsValid(false);
setError(getSecretsError(error, reason));
return;
}
onSuccess(perBudgetFile);
close();
};
return (
<Modal name="simplefin-init" containerProps={{ style: { width: 300 } }}>
{({ state }) => (
<>
<ModalHeader
title={t('Set-up SimpleFIN')}
rightContent={<ModalCloseButton onPress={() => state.close()} />}
/>
<View style={{ display: 'flex', gap: 10 }}>
<Text>
<Trans>
In order to enable bank sync via SimpleFIN (only for North
American banks), you will need to create a token. This can be
done by creating an account with{' '}
<Link
variant="external"
to="https://bridge.simplefin.org/"
linkColor="purple"
>
SimpleFIN
</Link>
.
</Trans>
</Text>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
gap: 12,
}}
>
<Text>
<Trans>For this budget only</Trans>
</Text>
<Toggle
id="simplefin-per-budget-file"
aria-label={t('For this budget only')}
isOn={perBudgetFile}
isDisabled={!isAdmin}
onToggle={setPerBudgetFile}
/>
</View>
<FormField>
<FormLabel title={t('Token:')} htmlFor="token-field" />
<Input
id="token-field"
type="password"
value={token}
onChangeValue={value => {
setToken(value);
setIsValid(true);
}}
/>
</FormField>
{!isValid && <Error>{error}</Error>}
</View>
<ModalButtons>
<ButtonWithLoading
variant="primary"
autoFocus
isLoading={isLoading}
onPress={() => {
void onSubmit(() => state.close());
}}
>
<Trans>Save and continue</Trans>
</ButtonWithLoading>
</ModalButtons>
</>
)}
</Modal>
);
};