Skip to content

Commit 7eb3cdf

Browse files
committed
add all newly added keys/Sections to DS and PS
1 parent 3b7a7f1 commit 7eb3cdf

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

jgclark.Dashboard/src/backupSettings.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ import { showMessage } from '@helpers/userInput'
1010

1111
//-----------------------------------------------------------------
1212

13-
1413
/**
1514
* Backup Dashboard settings.json file to a dated version in the plugin data folder.
15+
* @param {boolean} runSilently - If true, don't show any messages.
1616
*/
17-
export async function backupSettings() {
17+
export async function backupSettings(runSilently: boolean = false) {
1818
try {
1919
const pluginID = pluginJson['plugin.id']
2020
const pluginSettings = await DataStore.loadJSON(`../${pluginID}/settings.json`)
2121
const backupFilename = `settings_backup_${moment().format('YYYYMMDDHHmmss')}.json`
2222
const backupPath = `../${pluginID}/${backupFilename}`
2323
await DataStore.saveJSON(pluginSettings, backupPath)
24-
await showMessage(`Backup of Dashboard settings saved to ${backupPath}`, 'OK', 'Dashboard Settings Backup')
24+
if (!runSilently) {
25+
await showMessage(`Backup of Dashboard settings saved to ${backupPath}`, 'OK', 'Dashboard Settings Backup')
26+
}
2527
logInfo('backupSettings', `Backup of Dashboard settings saved to ${backupPath}`)
2628
} catch (error) {
2729
await showMessage(`Error trying to Backup Dashboard settings. Please see Plugin Console log for details.`, 'OK', 'Dashboard Settings Backup')

jgclark.Dashboard/src/dashboardHelpers.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ export function getDashboardSettingsDefaults(): TDashboardSettings {
10181018
}
10191019
return acc
10201020
}, {})
1021+
clo(dashboardSettingsDefaults, `dashboardSettingsDefaults:`)
10211022
// $FlowIgnore[prop-missing]
10221023
return dashboardSettingsDefaults
10231024
}
@@ -1028,13 +1029,14 @@ export function getDashboardSettingsDefaults(): TDashboardSettings {
10281029
* @param {TDashboardSettings} dashboardSettings - The dashboard settings to update.
10291030
* @returns {TDashboardSettings} The default values for the dashboard settings, with all sections set to false.
10301031
*/
1031-
export function getDashboardSettingsDefaultsWithSectionsAlsoSetToFalse(): TDashboardSettings {
1032+
export function getDashboardSettingsDefaultsWithSectionsSetToFalse(): TDashboardSettings {
10321033
const dashboardSettingsDefaults = getDashboardSettingsDefaults()
1033-
const sectionList = allSectionDetails.map((s) => s.showSettingName)
1034+
const sectionList = allSectionDetails.map((s) => s.showSettingName).filter((s) => s !== '' && s !== undefined)
10341035
const sectionsSetToFalse = sectionList.reduce((acc: TAnyObject, curr: string) => {
10351036
acc[curr] = false
10361037
return acc
10371038
}, {})
1039+
clo(sectionsSetToFalse, `sectionsSetToFalse:`)
10381040
// $FlowIgnore[prop-missing]
10391041
// $FlowIgnore[cannot-spread-indexer]
10401042
return { ...dashboardSettingsDefaults, ...sectionsSetToFalse }

jgclark.Dashboard/src/index.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*/
1111
import pluginJson from '../plugin.json'
1212
import { generateTagMentionCache } from './tagMentionCache'
13+
import { getDashboardSettingsDefaultsWithSectionsSetToFalse } from './dashboardHelpers'
14+
import { showDashboardReact } from './reactMain'
15+
import { backupSettings } from './backupSettings'
1316
import { renameKeys } from '@helpers/dataManipulation'
1417
import { clo, compareObjects, JSP, logDebug, logError, logInfo, logWarn } from '@helpers/dev'
1518
import { pluginUpdated, saveSettings } from '@helpers/NPConfiguration'
@@ -73,7 +76,7 @@ export { externallyStartSearch } from './dataGenerationSearch.js'
7376
export async function onUpdateOrInstall(): Promise<void> {
7477
try {
7578
logInfo(pluginJson, `onUpdateOrInstall() starting ...`)
76-
const initialSettings = await DataStore.loadJSON(`../${pluginID}/settings.json`)
79+
const initialSettings = (await DataStore.loadJSON(`../${pluginID}/settings.json`)) || DataStore.settings
7780
// clo(initialSettings, `onUpdateOrInstall - initialSettings:`)
7881
// Note: this is deceptive because dashboardSettings is one single JSON stringified key inside initialSettings
7982

@@ -87,35 +90,47 @@ export async function onUpdateOrInstall(): Promise<void> {
8790
includeTaskContext: 'showTaskContext',
8891
}
8992
const initialDashboardSettings = JSON.parse(initialSettings.dashboardSettings)
90-
const migratedDashboardSettings = renameKeys(initialDashboardSettings, keysToChange)
93+
const defaults = getDashboardSettingsDefaultsWithSectionsSetToFalse()
94+
const migratedDashboardSettings = { ...defaults, ...renameKeys(initialDashboardSettings, keysToChange) }
9195

9296
// Add any new settings for 2.3.0
93-
logInfo(pluginJson, `- adding new keys for 2.3.0 ...`)
94-
migratedDashboardSettings.includeFutureTagMentions = false
95-
migratedDashboardSettings.showProgressInSections = 'number closed'
97+
// TODO: (@jgclark): Hard-coding this should not be necessary anymore, because if they are in the defaults of dashboardSettings, they will be added to the perspectives.
98+
// logInfo(pluginJson, `- adding new keys for 2.3.0 ...`)
99+
// migratedDashboardSettings.includeFutureTagMentions = false
100+
// migratedDashboardSettings.showProgressInSections = 'number closed'
96101

97102
// Note: Workaround for number types getting changed to strings at some point in our Settings system.
98103
migratedDashboardSettings.newTaskSectionHeadingLevel = parseInt(migratedDashboardSettings.newTaskSectionHeadingLevel || 2)
99104
migratedDashboardSettings.maxItemsToShowInSection = parseInt(migratedDashboardSettings.maxItemsToShowInSection || 24)
100105
migratedDashboardSettings.lookBackDaysForOverdue = parseInt(migratedDashboardSettings.lookBackDaysForOverdue || 7)
101106
migratedDashboardSettings.autoUpdateAfterIdleTime = parseInt(migratedDashboardSettings.autoUpdateAfterIdleTime || 10)
102107

103-
// clo(migratedDashboardSettings, `onUpdateOrInstall - migratedDashboardSettings:`)
104-
105-
// Save the settings back to the DataStore
106-
if (compareObjects(migratedDashboardSettings, initialDashboardSettings, [], true) != null) {
107-
const migratedSettings = { ...initialSettings, dashboardSettings: JSON.stringify(migratedDashboardSettings) }
108-
const result = await saveSettings(pluginID, migratedSettings)
109-
logInfo(`onUpdateOrInstall`, `- Changes detected. Saved settings with result: ${JSP(result)}`)
108+
clo(migratedDashboardSettings, `onUpdateOrInstall - migratedDashboardSettings:`)
109+
110+
clo(initialSettings, `onUpdateOrInstall - initialSettings:`)
111+
const perspectiveSettings = await JSON.parse(initialSettings.perspectiveSettings)
112+
const newPerspectives = perspectiveSettings.map((p) => ({ ...p, dashboardSettings: { ...defaults, ...p.dashboardSettings } }))
113+
const migratedSettings = { ...initialSettings, dashboardSettings: JSON.stringify(migratedDashboardSettings), perspectiveSettings: JSON.stringify(newPerspectives) }
114+
115+
const diff = compareObjects(migratedDashboardSettings, initialDashboardSettings, [], true)
116+
if (diff != null) {
117+
// Save the settings back to the DataStore
118+
clo(diff, `Dashboard: onUpdateOrInstall - changes detected; diff:`)
119+
await backupSettings(true)
120+
await saveSettings(pluginID, migratedSettings)
121+
await showDashboardReact() // force a refresh of the dashboard with the new settings.
122+
// throw new Error('Stop because changes detected (this is not actually an ERROR but we are using a throw statement to stop execution)') // updates were required to be made to the settings.
123+
} else {
124+
logInfo(`onUpdateOrInstall`, `- no changes detected to settings.`)
110125
}
111-
126+
logInfo(`onUpdateOrInstall`, `- finished.`)
127+
pluginUpdated(pluginJson, { code: 1, message: `Plugin Installed or Updated.` })
112128
// Now get the tagMentionCache up to date.
113129
// Note: Deliberately don't await this, because it can take 15+ seconds.
114-
const _cachePromise = generateTagMentionCache(true)
115-
116-
await pluginUpdated(pluginJson, { code: 1, message: `Plugin Installed or Updated.` })
117-
118-
logInfo(`onUpdateOrInstall`, `- finished.`)
130+
// const _cachePromise = generateTagMentionCache(true)
131+
// TODO: (@jgclark): If we don't await this, doesn't it stop running at the end of the function? I think NotePlan will kill the thread.
132+
// dbw is changing it to await for now so it will run and finish. Should be invisible to the user.
133+
await generateTagMentionCache(true)
119134
} catch (err) {
120135
logError(pluginJson, `onUpdateOrInstall() error: ${err.message}`)
121136
}

0 commit comments

Comments
 (0)