From c9a5a943b6ac384f1f3a1247ca3804ab1cb8f376 Mon Sep 17 00:00:00 2001 From: Boris Goldowsky Date: Thu, 19 Sep 2024 16:23:02 -0400 Subject: [PATCH] Don't do init steps on all dataset changes. Take more care with undefined vs. falsy --- src/plugins/data-card/data-card-content.ts | 19 +++++++++++-------- src/plugins/data-card/data-card-tile.tsx | 5 +++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/plugins/data-card/data-card-content.ts b/src/plugins/data-card/data-card-content.ts index bdc1a3891..8e3054335 100644 --- a/src/plugins/data-card/data-card-content.ts +++ b/src/plugins/data-card/data-card-content.ts @@ -135,7 +135,7 @@ export const DataCardContentModel = TileContentModel })) .views(self => ({ get caseId() { - return self.caseIndex ? self.dataSet.caseIDFromIndex(self.caseIndex) : undefined; + return self.caseIndex !== undefined ? self.dataSet.caseIDFromIndex(self.caseIndex) : undefined; } })) .views(self => ({ @@ -260,13 +260,16 @@ export const DataCardContentModel = TileContentModel updateAfterSharedModelChanges(sharedModel?: SharedModelType) { const dataSet = self.dataSet; if (!dataSet) return; - // Select the card of the first selected case - const selectedCaseId = dataSet.firstSelectedCaseId - ? dataSet.firstSelectedCaseId : dataSet.firstSelectedCell?.caseId; - if (selectedCaseId && dataSet.caseIndexFromID(selectedCaseId) !== self.caseIndex) { - self.setCaseIndex(dataSet.caseIndexFromID(selectedCaseId)); - } else if (self.caseIndex === undefined) { - self.setCaseIndex(0); + // At initialization, caseIndex will be undefined. + // Set it to the first selected case, or the first card if there's no selection. + if (self.caseIndex === undefined) { + const selectedCaseId = dataSet.firstSelectedCaseId !== undefined + ? dataSet.firstSelectedCaseId : dataSet.firstSelectedCell?.caseId; + if (selectedCaseId !== undefined) { + self.setCaseIndex(dataSet.caseIndexFromID(selectedCaseId)); + } else { + self.setCaseIndex(0); + } } else if (self.caseIndex >= self.totalCases && self.totalCases > 0) { // Make sure case index is in range if number of cases has changed self.setCaseIndex(self.totalCases - 1); diff --git a/src/plugins/data-card/data-card-tile.tsx b/src/plugins/data-card/data-card-tile.tsx index 12f058307..9d63c3cc3 100644 --- a/src/plugins/data-card/data-card-tile.tsx +++ b/src/plugins/data-card/data-card-tile.tsx @@ -50,10 +50,11 @@ export const DataCardToolComponent: React.FC = observer(function Dat const cardOf = `Card ${content.caseIndexNumber + 1 } of `; // When a highlighted case or cell is set, show it - const selectedCaseId = dataSet.firstSelectedCaseId ? dataSet.firstSelectedCaseId : dataSet.firstSelectedCell?.caseId; + const selectedCaseId = dataSet.firstSelectedCaseId !== undefined + ? dataSet.firstSelectedCaseId : dataSet.firstSelectedCell?.caseId; useEffect(() => { if (content.caseIndex === undefined) return; - if (selectedCaseId && dataSet.caseIndexFromID(selectedCaseId) !== content.caseIndex) { + if (selectedCaseId !== undefined && dataSet.caseIndexFromID(selectedCaseId) !== content.caseIndex) { content.setCaseIndex(dataSet.caseIndexFromID(selectedCaseId)); } }, [content, dataSet, selectedCaseId]);