Skip to content

Commit 745aaf7

Browse files
committed
Code improvements found by Cursor
1 parent 1df81a9 commit 745aaf7

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

jgclark.Dashboard/src/perspectiveHelpers.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// @flow
33
//-----------------------------------------------------------------------------
44
// Dashboard plugin helper functions for Perspectives
5-
// Last updated 2025-10-05 for v2.3.0
5+
// Last updated 2025-11-28 for v2.3.0.b16
66
//-----------------------------------------------------------------------------
77

88
import pluginJson from '../plugin.json'
@@ -184,6 +184,7 @@ function ensureDefaultPerspectiveExists(perspectiveSettings: Array<TPerspectiveD
184184
* @param {boolean?} logAllKeys? whether to log every setting key:value or just the key ones (default: false)
185185
* @returns {Array<TPerspectiveDef>} all perspective settings
186186
*/
187+
// FIXME: Fix this parameter
187188
export async function getPerspectiveSettings(logAllKeys: boolean = false): Promise<Array<TPerspectiveDef>> {
188189
try {
189190
// Note: we think following (newer API call) is unreliable.
@@ -203,11 +204,13 @@ export async function getPerspectiveSettings(logAllKeys: boolean = false): Promi
203204
perspectiveSettings = await getPerspectiveSettingDefaults()
204205
const defaultPersp = getPerspectiveNamed('-', perspectiveSettings)
205206
if (!defaultPersp) {
206-
logError('getPerspectiveSettings', `getDefaultPerspectiveDef failed`)
207+
logError('getPerspectiveSettings', `Couldn't get default perspective - from getPerspectiveSettingDefaults()`)
207208
return []
208209
}
209210
const dashboardSettings = await getDashboardSettings()
210-
defaultPersp.dashboardSettings = { ...defaultPersp.dashboardSettings, ...cleanDashboardSettingsInAPerspective(dashboardSettings) }
211+
// $FlowFixMe[prop-missing]
212+
// $FlowFixMe[incompatible-call]
213+
defaultPersp.dashboardSettings = { ...defaultPersp.dashboardSettings, ...cleanDashboardSettingsInAPerspective(dashboardSettings, true) }
211214
perspectiveSettings = replacePerspectiveDef(perspectiveSettings, defaultPersp)
212215
logPerspectives(perspectiveSettings)
213216
}
@@ -546,7 +549,6 @@ export async function updateCurrentPerspectiveDef(): Promise<boolean> {
546549
return false
547550
}
548551
activeDef.isModified = false
549-
// $FlowIgnore // doesn't like the partial settings
550552
const dSet: Partial<TDashboardSettings> = await getDashboardSettings()
551553
activeDef.dashboardSettings = dSet
552554
const newDefs = replacePerspectiveDef(allDefs, activeDef)
@@ -612,7 +614,7 @@ export function cleanDashboardSettingsInAPerspective(settingsIn: TDashboardPlugi
612614

613615
const settingsOut = Object.keys(perspSettingsWithoutIrrelevantTags).reduce((acc: Partial<TDashboardSettings>, key) => {
614616
if (!shouldRemoveKey(key)) {
615-
acc[key] = settingsIn[key]
617+
acc[key] = perspSettingsWithoutIrrelevantTags[key] // TEST: Cursor is suggesting that this should be acc[key] = perspSettingsWithoutIrrelevantTags[key] not = settingsIn[key]
616618
} else {
617619
logDebug('cleanDashboardSettingsInAPerspective', `- Removing key '${key}'`)
618620
}
@@ -633,20 +635,26 @@ export function cleanDashboardSettingsInAPerspective(settingsIn: TDashboardPlugi
633635
* @returns {TDashboardSettings} - settings without irrelevant tag sections
634636
*/
635637
export function removeInvalidTagSections(settingsIn: TDashboardSettings): TDashboardSettings {
636-
// aka validateTagSections validateTags limitTagsToShow
637-
const tagSectionDetails = getTagSectionDetails(settingsIn)
638-
const showTagSectionKeysToRemove = Object.keys(settingsIn).filter(
639-
(key) => key.startsWith('showTagSection_') && !tagSectionDetails.some((detail) => detail.showSettingName === key),
640-
)
641-
642-
// Remove the keys only if they exist and are defined
643-
showTagSectionKeysToRemove.forEach((key) => {
644-
if (settingsIn[key] !== undefined && typeof settingsIn[key] === 'boolean') {
638+
try {
639+
const result = { ...settingsIn }
640+
// aka validateTagSections validateTags limitTagsToShow
641+
const tagSectionDetails = getTagSectionDetails(result)
642+
const showTagSectionKeysToRemove = Object.keys(result).filter(
643+
(key) => key.startsWith('showTagSection_') && !tagSectionDetails.some((detail) => detail.showSettingName === key),
644+
)
645+
646+
// Remove the keys only if they exist and are defined
647+
showTagSectionKeysToRemove.forEach((key) => {
648+
if (result[key] !== undefined && typeof result[key] === 'boolean') {
645649
// $FlowIgnore[incompatible-type]
646-
delete settingsIn[key]
647-
}
648-
})
649-
return settingsIn
650+
delete result[key]
651+
}
652+
})
653+
return result
654+
} catch (error) {
655+
logError('removeInvalidTagSections', `Error: ${error.message}. Returning original settings.`)
656+
return settingsIn
657+
}
650658
}
651659

652660
/**
@@ -663,7 +671,7 @@ export async function addNewPerspective(nameArg?: string): Promise<void> {
663671
name = nameArg
664672
} else {
665673
const res = await getInputTrimmed('Enter name of new Perspective:', 'OK', 'Add Perspective', 'Test')
666-
if (typeof name === 'boolean') {
674+
if (typeof res === 'boolean' && !res) {
667675
logWarn('addPerspectiveSetting', `Cancelled adding new Perspective`)
668676
return
669677
}
@@ -719,7 +727,7 @@ export async function addNewPerspective(nameArg?: string): Promise<void> {
719727
* Delete all Perspective settings, other than default
720728
*/
721729
export async function deleteAllNamedPerspectiveSettings(): Promise<void> {
722-
logDebug('deleteAllNamedPerspectiveSettings', `Attempting to delete all Perspective settings (other than edfault) ...`)
730+
logDebug('deleteAllNamedPerspectiveSettings', `Attempting to delete all Perspective settings (other than default) ...`)
723731
// v1
724732
// const pluginSettings = DataStore.settings
725733
// pluginSettings.perspectiveSettings = "[]"

0 commit comments

Comments
 (0)