Skip to content

Commit

Permalink
Settings Bug Fix: On init, only replace default values with actual va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
digitalmaster committed Nov 18, 2022
1 parent 5e3d420 commit 2b59b9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const defaultSettings = {
// context to access it anywhere)
const useSettings = () => {
const [settings, setSettings] = React.useState(defaultSettings);
console.log('DEBUG:: ~ file: useSettings.ts ~ line 15 ~ settings', settings);

React.useEffect(() => {
if (!settings.tagsListString.trim()) {
Expand All @@ -24,13 +23,23 @@ const useSettings = () => {

React.useEffect(() => {
// Init config panel
window.roamMemo.extensionAPI.settings.panel.create(settingsPanelConfig({ setSettings }));
window.roamMemo.extensionAPI.settings.panel.create(
settingsPanelConfig({ settings, setSettings })
);
}, [setSettings, settings.dataPageTitle]);

React.useEffect(() => {
const allSettings = window.roamMemo.extensionAPI.settings.getAll() || {};

setSettings((currentSettings) => ({ ...currentSettings, ...allSettings }));
// Filterout out any settings that are falsey
const filteredSettings = Object.keys(allSettings).reduce((acc, key) => {
if (allSettings[key]) {
acc[key] = allSettings[key];
}
return acc;
}, {});

setSettings((currentSettings) => ({ ...currentSettings, ...filteredSettings }));
}, [setSettings]);

return settings;
Expand Down
11 changes: 5 additions & 6 deletions src/settingsPanelConfig.ts → src/settingsPanelConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as asyncUtils from '~/utils/async';
import { importRoamSrOldData } from '~/utils/migration';
import RoamSrImportData from '~/components/RoamSRImport';
import RoamSrImportPanel from '~/components/RoamSrImportPanel';
import { defaultSettings } from './hooks/useSettings';

const settingsPanelConfig = ({ setSettings }) => {
const settingsPanelConfig = ({ settings, setSettings }) => {
const syncFn = async ({ key, value }: { key: string; value: any }) => {
window.roamMemo.extensionAPI.settings.set(key, value);
setSettings((currentSettings) => {
Expand All @@ -17,9 +16,9 @@ const settingsPanelConfig = ({ setSettings }) => {
settings: [
{
id: 'tagsListString',
name: 'Tag Pages',
name: 'Tag Pages (aka Decks)',
description:
'Separate multiple with commas. First one is the default page. Example: "memo, sr, 🐘, french words, fun facts"',
'Separate multiple decks with commas. Example: "memo, sr, 🐘, french exam, fun facts"',
action: {
type: 'input',
placeholder: defaultSettings.tagsListString,
Expand All @@ -35,7 +34,7 @@ const settingsPanelConfig = ({ setSettings }) => {
description: 'Import Roam Sr Old data',
action: {
type: 'reactComponent',
component: RoamSrImportData,
component: () => <RoamSrImportPanel dataPageTitle={settings.dataPageTitle} />,
},
},
{
Expand Down

0 comments on commit 2b59b9b

Please sign in to comment.