Skip to content

Commit

Permalink
feat(renderer): clear setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Feb 2, 2025
1 parent 897ae89 commit c2670ce
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
8 changes: 6 additions & 2 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { electronApp, optimizer } from '@electron-toolkit/utils';
import { app, globalShortcut, ipcMain } from 'electron';
import squirrelStartup from 'electron-squirrel-startup';
import ElectronStore from 'electron-store';
import { updateElectronApp, UpdateSourceType } from 'update-electron-app';
import { UpdateSourceType, updateElectronApp } from 'update-electron-app';
import { mainZustandBridge } from 'zutron/main';

import * as env from '@main/env';
Expand Down Expand Up @@ -194,6 +194,10 @@ const registerIPCHandlers = () => {
SettingStore.resetPreset();
return SettingStore.getStore();
});

ipcMain.handle('setting:clear', async () => {
SettingStore.clear();
});
};

/**
Expand Down Expand Up @@ -228,4 +232,4 @@ app
})
.catch(console.log);

// ... 保留其他代码 ...
// ... 保留其他代码 ...
3 changes: 3 additions & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const electronHandler = {
ipcRenderer.invoke('utio:updatePresetFromRemote'),
resetPreset: () => ipcRenderer.invoke('utio:resetPreset'),
},
setting: {
clear: () => ipcRenderer.invoke('setting:clear'),
},
};

// Initialize Zutron bridge
Expand Down
75 changes: 56 additions & 19 deletions src/renderer/src/pages/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '@chakra-ui/react';
import { Field, Form, Formik } from 'formik';
import { useLayoutEffect, useState } from 'react';
import { IoAdd, IoInformationCircle } from 'react-icons/io5';
import { IoAdd, IoInformationCircle, IoTrash } from 'react-icons/io5';
import { useDispatch } from 'zutron';

import { VlmProvider } from '@main/store/types';
Expand Down Expand Up @@ -101,6 +101,29 @@ export default function Settings() {
}
};

const handleClearSettings = async () => {
try {
await window.electron.setting.clear();
// 刷新设置
dispatch({
type: 'GET_SETTINGS',
payload: null,
});
toast({
title: 'All settings cleared successfully',
status: 'success',
duration: 2000,
});
} catch (error) {
toast({
title: 'Failed to clear settings',
description: error.message,
status: 'error',
duration: 3000,
});
}
};

return (
<Box h="100vh" overflow="hidden" px={6} pt={6} pb={0}>
<Tabs display="flex" flexDirection="column" h="full" pt={4}>
Expand Down Expand Up @@ -231,7 +254,11 @@ export default function Settings() {
)}

{settings ? (
<Formik initialValues={settings} onSubmit={handleSubmit}>
<Formik
initialValues={settings}
onSubmit={handleSubmit}
enableReinitialize
>
{({ values = {}, setFieldValue }) => {
const isRemotePreset =
settings?.presetSource?.type === 'remote';
Expand Down Expand Up @@ -411,24 +438,34 @@ export default function Settings() {
zIndex={1}
flexShrink={0}
>
<HStack spacing={4} justify="flex-start">
<Button
form="settings-form"
as="button"
type="submit"
rounded="base"
variant="tars-ghost"
>
Save
</Button>
<Button
onClick={handleCancel}
rounded="base"
<HStack spacing={4} justify="space-between">
<HStack spacing={4}>
<Button
form="settings-form"
as="button"
type="submit"
rounded="base"
variant="tars-ghost"
>
Save
</Button>
<Button
onClick={handleCancel}
rounded="base"
variant="ghost"
fontWeight="normal"
>
Cancel
</Button>
</HStack>

<IconButton
aria-label="Clear all settings"
icon={<IoTrash />}
variant="ghost"
fontWeight="normal"
>
Cancel
</Button>
colorScheme="red"
onClick={handleClearSettings}
/>
</HStack>
</Box>
</Tabs>
Expand Down

0 comments on commit c2670ce

Please sign in to comment.