From 8f457069eebef388268f4981f65fa54433eb4b6f Mon Sep 17 00:00:00 2001 From: "B. Camphart" Date: Fri, 19 Jan 2024 13:23:28 -0800 Subject: [PATCH] Prevent null selectedDraft from breaking render (#219) * Prevent null selectedDraft from breaking render * Remove unnecessary (triple) check * Early return if selectedDraft is null * Provide default value of `currentDraftIndex` Also adds checks for it being greater than `0` where needed. * Remove unnecessary binding, which caused error For some reason, this binding sometimes would have an `undefined` value, which was then set in the steps of the current workflow (possibly due to refresh from an unpopulated compile step). * Fix style issues - Use `!draft` instead of `draft == null` - Always include curly braces for `if` statements --- src/view/compile/CompileView.svelte | 6 ++---- src/view/explorer/SceneList.svelte | 13 +++++++------ src/view/explorer/scene-menu-items.ts | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/view/compile/CompileView.svelte b/src/view/compile/CompileView.svelte index e5b0412..6782d87 100644 --- a/src/view/compile/CompileView.svelte +++ b/src/view/compile/CompileView.svelte @@ -63,9 +63,7 @@ allWorkflowNames.length > 0 ) { // shadowed here to prevent circular reference - const _currentDraftIndex = $drafts.findIndex( - (d) => d.vaultPath === $selectedDraft.vaultPath - ); + const _currentDraftIndex = $drafts.findIndex((d) => d.vaultPath === $selectedDraft.vaultPath); $drafts[_currentDraftIndex].workflow = allWorkflowNames[0]; } } @@ -329,7 +327,7 @@ > { const newWorkflow = { ...$currentWorkflow, diff --git a/src/view/explorer/SceneList.svelte b/src/view/explorer/SceneList.svelte index 43ff69e..8757eb4 100644 --- a/src/view/explorer/SceneList.svelte +++ b/src/view/explorer/SceneList.svelte @@ -19,11 +19,9 @@ import { selectElementContents } from "../utils"; import { addAll, addScene, ignoreAll, ignoreScene } from "./scene-menu-items"; - let currentDraftIndex: number; - $: { - currentDraftIndex = $drafts.findIndex( - (d) => d.vaultPath === $selectedDraft.vaultPath - ); + let currentDraftIndex: number = -1; + $: if($selectedDraft) { + currentDraftIndex = $drafts.findIndex((d) => d.vaultPath === $selectedDraft.vaultPath); } // Function to make paths from scene names @@ -231,6 +229,7 @@ } function doWithUnknown(fileName: string, action: "add" | "ignore") { + if (!$selectedDraft) return; if (action === "add") { addScene(fileName); } else { @@ -239,6 +238,7 @@ } function doWithAll(action: "add" | "ignore") { + if (!$selectedDraft) return; if (action === "add") { addAll(); } else { @@ -270,6 +270,7 @@ if ( oldIndex !== undoIndex && newValue && + currentDraftIndex >= 0 && newValue.draftVaultPath === $drafts[currentDraftIndex].vaultPath && $drafts[currentDraftIndex].format === "scenes" ) { @@ -361,7 +362,7 @@ - {#if $selectedDraft.format === "scenes" && $selectedDraft.unknownFiles.length > 0} + {#if $selectedDraft && $selectedDraft.format === "scenes" && $selectedDraft.unknownFiles.length > 0}

diff --git a/src/view/explorer/scene-menu-items.ts b/src/view/explorer/scene-menu-items.ts index d1eb920..d22f302 100644 --- a/src/view/explorer/scene-menu-items.ts +++ b/src/view/explorer/scene-menu-items.ts @@ -5,6 +5,9 @@ import { get } from "svelte/store"; const getSelectedDraftWithIndex = () => { const draft = get(selectedDraft) as MultipleSceneDraft; + if (!draft) { + return { index: -1, draft } + } const index = get(drafts).findIndex( (d) => d.vaultPath === draft.vaultPath ); @@ -13,6 +16,9 @@ const getSelectedDraftWithIndex = () => { export const addScene = (fileName: string) => { const { index, draft } = getSelectedDraftWithIndex() + if (!draft) { + return; + } if (index >= 0 && draft.format === "scenes") { drafts.update((d) => { const targetDraft = d[index] as MultipleSceneDraft; @@ -29,6 +35,9 @@ export const addScene = (fileName: string) => { export const ignoreScene = (fileName: string) => { const { index, draft } = getSelectedDraftWithIndex() + if (!draft) { + return; + } if (index >= 0 && draft.format === "scenes") { drafts.update((d) => { const targetDraft = d[index] as MultipleSceneDraft; @@ -46,6 +55,9 @@ export const ignoreScene = (fileName: string) => { export const addAll = () => { const { index, draft } = getSelectedDraftWithIndex() + if (!draft) { + return; + } if (index >= 0 && draft.format === "scenes") { drafts.update((d) => { const targetDraft = d[index] as MultipleSceneDraft; @@ -61,6 +73,9 @@ export const addAll = () => { export const ignoreAll = () => { const { index, draft } = getSelectedDraftWithIndex() + if (!draft) { + return; + } if (index >= 0 && draft.format === "scenes") { drafts.update((d) => { const targetDraft = d[index] as MultipleSceneDraft;