Skip to content

Commit

Permalink
better cache selector
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarner7 committed Oct 31, 2023
1 parent 28c24d3 commit f7626b9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
21 changes: 21 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
BrowserWindowConstructorOptions,
protocol,
net,
dialog,
} from 'electron';
import electronLocalShortcut from 'electron-localshortcut';
import log from 'electron-log';
Expand Down Expand Up @@ -663,6 +664,26 @@ ipcMain.on(
},
);

ipcMain.handle('cache-open-dialog', async (): Promise<string | null> => {
const window = getMainWindow();
if (window) {
const response = await dialog.showOpenDialog(window, {
defaultPath: app.getPath('userData'),
properties: ['openDirectory', 'showHiddenFiles'],
});
if (response.canceled) {
return null;
}

return response.filePaths[0];
}
return null;
});

ipcMain.handle('cache-open-path', async (_event, path: string) => {
return shell.openPath(path);
});

app.on('before-quit', () => {
getMpvInstance()?.stop();
getMpvInstance()?.quit();
Expand Down
11 changes: 11 additions & 0 deletions src/main/preload/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { access, lstat, writeFile } from 'fs/promises';
import { join } from 'path';
import { Song } from '/@/renderer/api/types';
import { localSettings } from '/@/main/preload/local-settings';
import { ipcRenderer } from 'electron';

let cachePath: string;

Expand Down Expand Up @@ -42,10 +43,20 @@ export const setCachePath = (path: string): void => {
localSettings.set('cache.path', path);
};

export const openCacheDialog = (): Promise<string | null> => {
return ipcRenderer.invoke('cache-open-dialog');
};

export const openCachePath = (path: string) => {
ipcRenderer.invoke('cache-open-path', path);
};

export const cache = {
cacheFile,
getPath,
isValidDirectory,
openCacheDialog,
openCachePath,
setCachePath,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Switch, TextInput, toast } from '/@/renderer/components';
import { Stack } from '@mantine/core';
import { Button, Switch } from '/@/renderer/components';
import {
SettingOption,
SettingsSection,
} from '/@/renderer/features/settings/components/settings-section';
import { useCacheSettings, useSettingsStoreActions } from '/@/renderer/store';
import { RiExternalLinkLine } from 'react-icons/ri';

const cache = window.electron.cache;
const localSettings = window.electron.localSettings;
Expand Down Expand Up @@ -35,27 +37,33 @@ export const CacheSettings = () => {
},
{
control: (
<TextInput
defaultValue={settings.path}
w={300}
onChange={async (e) => {
const path = e.currentTarget.value;

if (await cache.isValidDirectory(path)) {
setSettings({
cache: {
...settings,
path,
},
});
} else {
toast.error({
message: `${path} is not a directory or does not exist`,
title: 'Invalid path',
});
}
}}
/>
<Stack>
<Button
variant="filled"
onClick={async () => {
const path = await cache.openCacheDialog();
if (path) {
setSettings({
cache: {
...settings,
path,
},
});
}
}}
>
Set cache path
</Button>
<Button
rightIcon={<RiExternalLinkLine />}
variant="default"
onClick={() => {
cache.openCachePath(settings.path);
}}
>
{settings.path}
</Button>
</Stack>
),
description: 'Path to store tracks',
title: 'Cache location',
Expand Down

0 comments on commit f7626b9

Please sign in to comment.