Skip to content

Commit

Permalink
start registreting theme in config
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Feb 20, 2024
1 parent 2403416 commit 5d6ade5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 25 deletions.
7 changes: 7 additions & 0 deletions src/component/Connection/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type ConnectionObject = {
name: string;
host: string;
port: number;
user: string;
password: string;
};
2 changes: 1 addition & 1 deletion src/component/ThemeSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { THEME_LIST } from '../../src/theme';
import { useTheme } from '..//Contexts';
import { useTheme } from '../Contexts';

export default function ThemeSelector() {
const { themeName, changeTheme } = useTheme();
Expand Down
38 changes: 35 additions & 3 deletions src/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { afterEach, describe, expect, test, vi } from 'vitest';
import { existsSync, mkdirSync, readFileSync, writeFile } from 'node:fs';
import envPaths from 'env-paths';
import { addConnectionToConfig, readConfigurationFile } from './configuration';
import {
addConnectionToConfig,
readConfigurationFile,
changeTheme,
} from './configuration';

vi.mock('env-paths', () => ({
default: () => ({ config: 'config' }),
Expand Down Expand Up @@ -115,7 +119,7 @@ describe('add connection to config', () => {

test('empty file', async () => {
mockExistsSync.mockReturnValue(false);
await addConnectionToConfig({} as any, 'local', {
await addConnectionToConfig('local', {
name: 'local',
host: 'localhost',
port: 3306,
Expand All @@ -128,6 +132,7 @@ describe('add connection to config', () => {
JSON.stringify(
{
version: 1,
theme: 'Dracula',
connections: {
local: {
name: 'local',
Expand Down Expand Up @@ -168,7 +173,7 @@ describe('add connection to config', () => {
mockExistsSync.mockReturnValue(true);
mockReadFileSync.mockReturnValue(JSON.stringify(config));

await addConnectionToConfig({} as any, 'test', {
await addConnectionToConfig('test', {
name: 'test',
host: 'test',
port: 3306,
Expand Down Expand Up @@ -211,3 +216,30 @@ describe('add connection to config', () => {
);
});
});

describe('set theme', () => {
afterEach(() => {
vi.resetModules();
});

test('set theme', async () => {
mockExistsSync.mockReturnValue(false);

await changeTheme('test');

expect(mockWriteFile).toHaveBeenCalledWith(
'config/config.json',
JSON.stringify(
{
version: 1,
theme: 'test',
connections: {},
},
null,
2
),
'utf-8',
expect.any(Function)
);
});
});
52 changes: 33 additions & 19 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { resolve } from 'node:path';
import { existsSync, mkdirSync, readFileSync, writeFile } from 'node:fs';
import envPaths from 'env-paths';
import { ConnectionObject } from './component/Connection/types';
import { DEFAULT_THEME } from './theme';

export type Configuration = {
version: 1;
theme: string;
connections: Record<string, EncryptedConnectionObject>;
};

Expand All @@ -20,6 +22,14 @@ type EncryptedConfiguration = {
const envPath = envPaths('TianaTables', { suffix: '' });
const dataFilePath = resolve(envPath.config, 'config.json');

function getBaseConfig(): Configuration {
return {
version: 1,
theme: DEFAULT_THEME.name,
connections: {},
};
}

function encryptConnection(
connection: ConnectionObject
): EncryptedConnectionObject {
Expand Down Expand Up @@ -63,26 +73,8 @@ export function readConfigurationFile(): null | Configuration {
),
};
}
export function addConnectionToConfig(
event: Electron.IpcMainInvokeEvent,
name: string,
connection: ConnectionObject
): void {
let config = readConfigurationFile();

if (!config) {
config = {
version: 1,
connections: {},
};
}

if (!config.connections) {
config.connections = {};
}

config.connections[name] = connection;

function writeConfiguration(config: Configuration): void {
const encryptedConfig = {
...config,
connections: Object.fromEntries(
Expand All @@ -105,3 +97,25 @@ export function addConnectionToConfig(
}
);
}

export function addConnectionToConfig(
name: string,
connection: ConnectionObject
): void {
const config = readConfigurationFile() ?? getBaseConfig();

if (!config.connections) {
config.connections = {};
}

config.connections[name] = connection;

writeConfiguration(config);
}

export function changeTheme(theme: string): void {
const config = readConfigurationFile() ?? getBaseConfig();
config.theme = theme;

writeConfiguration(config);
}
16 changes: 14 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import installExtension, {
REACT_DEVELOPER_TOOLS,
} from 'electron-devtools-installer';
import connectionStackInstance from './sql';
import { readConfigurationFile, addConnectionToConfig } from './configuration';
import {
readConfigurationFile,
addConnectionToConfig,
changeTheme,
} from './configuration';
import { ConnectionObject } from './component/Connection';

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
Expand Down Expand Up @@ -45,7 +50,14 @@ app.whenReady().then(() => {
.catch((err) => console.log('An error occurred: ', err));

ipcMain.handle('config:read', readConfigurationFile);
ipcMain.handle('config:connection:add', addConnectionToConfig);
ipcMain.handle(
'config:connection:add',
(event: unknown, name: string, connection: ConnectionObject) =>
addConnectionToConfig(name, connection)
);
ipcMain.handle('config:theme:change', (event: unknown, name: string) =>
changeTheme(name)
);

connectionStackInstance.bindIpcMain(ipcMain);

Expand Down
4 changes: 4 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ interface Config {
name: string,
connection: ConnectionObject
): Promise<void>;

changeTheme(theme: string): void;
}

const config: Config = {
readConfigurationFile: () => ipcRenderer.invoke('config:read'),
addConnectionToConfig: (name: string, connection: ConnectionObject) =>
ipcRenderer.invoke('config:connection:add', name, connection),
changeTheme: (theme: string) =>
ipcRenderer.invoke('config:theme:change', theme),
};

contextBridge.exposeInMainWorld('config', config);
Expand Down

0 comments on commit 5d6ade5

Please sign in to comment.