1010 */
1111import pluginJson from '../plugin.json'
1212import { generateTagMentionCache } from './tagMentionCache'
13+ import { getDashboardSettingsDefaultsWithSectionsSetToFalse } from './dashboardHelpers'
14+ import { showDashboardReact } from './reactMain'
15+ import { backupSettings } from './backupSettings'
1316import { renameKeys } from '@helpers/dataManipulation'
1417import { clo , compareObjects , JSP , logDebug , logError , logInfo , logWarn } from '@helpers/dev'
1518import { pluginUpdated , saveSettings } from '@helpers/NPConfiguration'
@@ -73,7 +76,7 @@ export { externallyStartSearch } from './dataGenerationSearch.js'
7376export 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