From a2183f5d9da05953e4610cf421c69f5535390523 Mon Sep 17 00:00:00 2001 From: thsparks Date: Tue, 7 May 2024 12:54:19 -0700 Subject: [PATCH] Persist all tags collapsed --- teachertool/src/components/CatalogOverlay.tsx | 9 +++---- teachertool/src/services/storageService.ts | 27 ++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/teachertool/src/components/CatalogOverlay.tsx b/teachertool/src/components/CatalogOverlay.tsx index 81624ca4627..ac5331133eb 100644 --- a/teachertool/src/components/CatalogOverlay.tsx +++ b/teachertool/src/components/CatalogOverlay.tsx @@ -140,12 +140,11 @@ const CatalogList: React.FC = () => { return null; } - const expandedTags = getExpandedCatalogTags(); - - // If no tags are expanded, expand the first one. - if (expandedTags.length === 0) { + let expandedTags = getExpandedCatalogTags(); + if (!expandedTags) { + // If we haven't saved an expanded set, default expand the first one. addExandedCatalogTagAsync(tags[0]); - expandedTags.push(tags[0]); + expandedTags = [tags[0]]; } const expandedIds = expandedTags.map(t => getItemIdForTag(t)); diff --git a/teachertool/src/services/storageService.ts b/teachertool/src/services/storageService.ts index d7567f851ce..31c49986e05 100644 --- a/teachertool/src/services/storageService.ts +++ b/teachertool/src/services/storageService.ts @@ -194,13 +194,15 @@ export async function deleteChecklistAsync(name: string) { } } -export function getExpandedCatalogTags(): string[] { +// Returns undefined if it has not been set or if there was an issue. +// Empty list means it was explicitly set to empty. +export function getExpandedCatalogTags(): string[] | undefined { try { const tags = getValue(EXPANDED_CATALOG_TAGS_KEY); - return tags ? JSON.parse(tags) : []; + return tags ? JSON.parse(tags) : undefined; } catch (e) { logError(ErrorCode.localStorageReadError, e); - return []; + return undefined; } } @@ -213,17 +215,24 @@ export async function setExpandedCatalogTags(tags: string[]) { } export async function addExandedCatalogTagAsync(tag: string) { - const expandedTags = getExpandedCatalogTags(); + let expandedTags = getExpandedCatalogTags(); + if (!expandedTags) { + expandedTags = []; + } expandedTags.push(tag); await setExpandedCatalogTags(expandedTags); } export async function removeExpandedCatalogTagAsync(tag: string) { - const expandedTags = getExpandedCatalogTags(); - const index = expandedTags.indexOf(tag); - if (index !== -1) { - expandedTags.splice(index, 1); - await setExpandedCatalogTags(expandedTags); + let expandedTags = getExpandedCatalogTags(); + if (!expandedTags) { + await setExpandedCatalogTags([]); + } else { + const index = expandedTags.indexOf(tag); + if (index !== -1) { + expandedTags.splice(index, 1); + await setExpandedCatalogTags(expandedTags); + } } }