diff --git a/locales/en.ts b/locales/en.ts index 194d624..b2b7d63 100644 --- a/locales/en.ts +++ b/locales/en.ts @@ -12,7 +12,6 @@ export default { 'connection.form.port.label': 'Port', 'connection.form.user.label': 'User', 'connection.form.password.label': 'Password', - 'connection.form.saveConnection': 'Save connection', 'connection.form.action.connect': 'Connect', 'connection.form.action.saveAndConnect': 'Save and connect', 'table.rows.loadMore': 'Load more…', diff --git a/locales/fr.ts b/locales/fr.ts index de6be1a..4364148 100644 --- a/locales/fr.ts +++ b/locales/fr.ts @@ -14,7 +14,6 @@ const fr: Translation = { 'connection.form.port.label': 'Port', 'connection.form.user.label': 'Utilisateur', 'connection.form.password.label': 'Mot de passe', - 'connection.form.saveConnection': 'Enregistrer la connexion', 'connection.form.action.connect': 'Connecter', 'connection.form.action.saveAndConnect': 'Enregistrer et connecter', 'table.rows.loadMore': 'Charger plus…', diff --git a/src/configuration/index.test.ts b/src/configuration/index.test.ts index 5d8b6a5..b537c14 100644 --- a/src/configuration/index.test.ts +++ b/src/configuration/index.test.ts @@ -11,7 +11,7 @@ import { updateConnectionState, } from '.'; -const { getBaseConfig } = testables; +const { getBaseConfig, resetConfiguration } = testables; vi.mock('env-paths', () => ({ default: () => ({ config: 'config' }), @@ -37,6 +37,8 @@ function resetAllMocks(): void { mockExistsSync.mockReset(); mockReadFileSync.mockReset(); mockWriteFile.mockReset(); + + resetConfiguration(); } vi.mock('electron', () => ({ @@ -81,6 +83,7 @@ describe('read configuration from file', () => { mockExistsSync.mockReturnValue(false); expect(getConfiguration()).toStrictEqual(getBaseConfig()); + expect(mockReadFileSync).not.toHaveBeenCalled(); }); test('existing file but empty', () => { @@ -88,6 +91,8 @@ describe('read configuration from file', () => { mockReadFileSync.mockReturnValue(''); expect(getConfiguration()).toStrictEqual(getBaseConfig()); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); test('existing file without connexion key', () => { @@ -97,6 +102,8 @@ describe('read configuration from file', () => { expect(getConfiguration()).toStrictEqual({ connections: {}, }); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); test('existing file without connexion', () => { @@ -107,6 +114,8 @@ describe('read configuration from file', () => { version: 1, connections: {}, }); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); test('existing file with connexions', () => { @@ -237,6 +246,8 @@ describe('add connection to config', () => { 'utf-8', expect.any(Function) ); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); }); @@ -313,6 +324,8 @@ describe('set theme', () => { 'utf-8', expect.any(Function) ); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); }); @@ -331,6 +344,8 @@ describe('set connection appState', async () => { await updateConnectionState('inexistant', 'isActive', true); expect(mockWriteFile).not.toHaveBeenCalled(); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); test('existing file, set active', async () => { @@ -372,6 +387,8 @@ describe('set connection appState', async () => { 'utf-8', expect.any(Function) ); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); test('existing file, set activeDatabase', async () => { @@ -437,6 +454,8 @@ describe('set connection appState', async () => { 'utf-8', expect.any(Function) ); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); }); @@ -483,6 +502,8 @@ describe('edit', () => { 'utf-8', expect.any(Function) ); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); test('rename connexion', () => { @@ -528,5 +549,7 @@ describe('edit', () => { 'utf-8', expect.any(Function) ); + + expect(mockReadFileSync).toHaveBeenCalledOnce(); }); }); diff --git a/src/configuration/index.ts b/src/configuration/index.ts index 5920231..6a316df 100644 --- a/src/configuration/index.ts +++ b/src/configuration/index.ts @@ -46,7 +46,17 @@ function decryptConnection( }; } +let configuration: Configuration | null = null; + export function getConfiguration(): Configuration { + if (!configuration) { + configuration = loadConfiguration(); + } + + return configuration; +} + +function loadConfiguration(): Configuration { if (!existsSync(dataFilePath)) { return getBaseConfig(); } @@ -183,4 +193,9 @@ export function bindIpcMain(ipcMain: Electron.IpcMain): void { } } -export const testables = { getBaseConfig }; +export const testables = { + getBaseConfig, + resetConfiguration: () => { + configuration = null; + }, +}; diff --git a/src/contexts/ConnectionContext.tsx b/src/contexts/ConnectionContext.tsx index 4897f04..952bbce 100644 --- a/src/contexts/ConnectionContext.tsx +++ b/src/contexts/ConnectionContext.tsx @@ -8,7 +8,6 @@ interface ConnexionContextProps { currentConnectionName: string | null; connectionNameList: string[]; connectTo: ConnectToFunc; - setCurrentConnectionName: (connectionName: string) => void; } export const ConnectionContext = createContext({ @@ -16,8 +15,6 @@ export const ConnectionContext = createContext({ connectionNameList: [], // eslint-disable-next-line @typescript-eslint/no-empty-function connectTo: () => Promise.resolve(), - // eslint-disable-next-line @typescript-eslint/no-empty-function - setCurrentConnectionName: () => {}, }); ConnectionContext.displayName = 'ConnectionContext'; diff --git a/src/renderer/component/Cell.stories.tsx b/src/renderer/component/Cell.stories.tsx index bc27353..2652921 100644 --- a/src/renderer/component/Cell.stories.tsx +++ b/src/renderer/component/Cell.stories.tsx @@ -13,8 +13,6 @@ const meta: Meta = { value={{ currentConnectionName: 'test', connectionNameList: ['test'], - // eslint-disable-next-line @typescript-eslint/no-empty-function - setCurrentConnectionName: () => {}, connectTo: async (connection) => { action('connectTo')(connection); }, diff --git a/src/renderer/component/Connection/ConnectionForm.stories.tsx b/src/renderer/component/Connection/ConnectionForm.stories.tsx index d01af64..72feecb 100644 --- a/src/renderer/component/Connection/ConnectionForm.stories.tsx +++ b/src/renderer/component/Connection/ConnectionForm.stories.tsx @@ -11,8 +11,6 @@ const meta: Meta = { value={{ currentConnectionName: 'test', connectionNameList: ['test'], - // eslint-disable-next-line @typescript-eslint/no-empty-function - setCurrentConnectionName: () => {}, connectTo: async (connection) => { action('connectTo')(connection); }, diff --git a/src/renderer/component/Connection/ConnectionForm.tsx b/src/renderer/component/Connection/ConnectionForm.tsx index 48e4a1a..32ea4ed 100644 --- a/src/renderer/component/Connection/ConnectionForm.tsx +++ b/src/renderer/component/Connection/ConnectionForm.tsx @@ -1,5 +1,4 @@ -import { useState } from 'react'; -import { Button, Checkbox, Form, Input } from 'antd'; +import { Button, Form, Input } from 'antd'; import type { TFunction } from 'i18next'; import { useNavigate } from 'react-router'; import { useConfiguration } from '../../../contexts/ConfigurationContext'; @@ -7,34 +6,26 @@ import { useConnectionContext } from '../../../contexts/ConnectionContext'; import { useTranslation } from '../../../i18n'; import type { ConnectionObject } from '../../../sql/types'; -type ConnectionFormType = ConnectionObject & { - save?: boolean; -}; - type Props = { connection?: ConnectionObject }; function getSubmitButtonLabel( t: TFunction, - connection: ConnectionObject | undefined, - isSaveChecked: boolean | undefined + connection: ConnectionObject | undefined ): string { if (connection) { return t('save'); } - return isSaveChecked - ? t('connection.form.action.saveAndConnect') - : t('connection.form.action.connect'); + return t('connection.form.action.saveAndConnect'); } function ConnectionForm({ connection }: Props) { - const initialValues: ConnectionFormType = connection ?? { + const initialValues: ConnectionObject = connection ?? { name: '', host: 'localhost', port: 3306, user: 'root', password: '', - save: true, }; const { t } = useTranslation(); @@ -42,31 +33,23 @@ function ConnectionForm({ connection }: Props) { const { addConnectionToConfig, editConnection } = useConfiguration(); const navigate = useNavigate(); const [form] = Form.useForm(); - const [submitButtonLabel, setSubmitButtonLabel] = useState( - getSubmitButtonLabel(t, connection, initialValues.save) - ); - - const handleSubmit = (formData: ConnectionFormType): void => { - const { save, ...connectionFormData } = formData; + const handleSubmit = (formData: ConnectionObject): void => { if (connection) { // edit connection - editConnection(connection.name, connectionFormData); + editConnection(connection.name, formData); navigate('/connect'); return; } - if (save) { - addConnectionToConfig(connectionFormData); - } - - connectTo(connectionFormData); + addConnectionToConfig(formData); + connectTo(formData); }; return (
- + name="name" label={t('connection.form.name.label')} rules={[{ required: true }]} @@ -74,7 +57,7 @@ function ConnectionForm({ connection }: Props) { - + name="host" label={t('connection.form.host.label')} rules={[{ required: true }]} @@ -82,7 +65,7 @@ function ConnectionForm({ connection }: Props) { - + name="port" label={t('connection.form.port.label')} rules={[{ required: true }]} @@ -90,7 +73,7 @@ function ConnectionForm({ connection }: Props) { - + name="user" label={t('connection.form.user.label')} rules={[{ required: true }]} @@ -98,28 +81,14 @@ function ConnectionForm({ connection }: Props) { - + name="password" label={t('connection.form.password.label')} > - {!connection && ( - name="save" valuePropName="checked"> - { - setSubmitButtonLabel( - getSubmitButtonLabel(t, connection, form.getFieldValue('save')) - ); - }} - > - {t('connection.form.saveConnection')} - - - )} - - shouldUpdate> + shouldUpdate> diff --git a/src/renderer/component/Connection/ConnectionStack.tsx b/src/renderer/component/Connection/ConnectionStack.tsx index ea2cb51..dbf6859 100644 --- a/src/renderer/component/Connection/ConnectionStack.tsx +++ b/src/renderer/component/Connection/ConnectionStack.tsx @@ -37,13 +37,6 @@ function ConnectionStack({ children }: Props) { [navigate, setConnectionNameList] ); - const handleSetConnection = useCallback( - (connectionName: string) => { - navigate(`/connections/${connectionName}`); - }, - [navigate] - ); - const handleSetDatabase = useCallback( (database: string) => { invariant(currentConnectionName, 'Connection name is required'); @@ -59,7 +52,6 @@ function ConnectionStack({ children }: Props) { connectionNameList, currentConnectionName: currentConnectionName ?? null, connectTo: handleConnectTo, - setCurrentConnectionName: handleSetConnection, }} > = { value={{ currentConnectionName: 'production', connectionNameList: ['test', 'production', 'staging', 'development'], - // eslint-disable-next-line @typescript-eslint/no-empty-function - setCurrentConnectionName: (connection) => { - action('setCurrentConnectionName')(connection); - }, connectTo: async () => {}, }} > diff --git a/src/renderer/component/TableList.stories.tsx b/src/renderer/component/TableList.stories.tsx index ea987f1..b12f4f8 100644 --- a/src/renderer/component/TableList.stories.tsx +++ b/src/renderer/component/TableList.stories.tsx @@ -12,8 +12,6 @@ const meta: Meta = { value={{ currentConnectionName: 'test', connectionNameList: ['test'], - // eslint-disable-next-line @typescript-eslint/no-empty-function - setCurrentConnectionName: () => {}, connectTo: async (connection) => { action('connectTo')(connection); },