Skip to content

Commit

Permalink
Prevent null selectedDraft from breaking render (#219)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
b-camphart authored Jan 19, 2024
1 parent 85a2789 commit 8f45706
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/view/compile/CompileView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Expand Down Expand Up @@ -329,7 +327,7 @@
>
<CompileStepView
ordinal={item.index + 1}
bind:step={$workflows[currentWorkflowName].steps[item.index]}
step={$workflows[currentWorkflowName].steps[item.index]}
on:removeStep={() => {
const newWorkflow = {
...$currentWorkflow,
Expand Down
13 changes: 7 additions & 6 deletions src/view/explorer/SceneList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -231,6 +229,7 @@
}
function doWithUnknown(fileName: string, action: "add" | "ignore") {
if (!$selectedDraft) return;
if (action === "add") {
addScene(fileName);
} else {
Expand All @@ -239,6 +238,7 @@
}
function doWithAll(action: "add" | "ignore") {
if (!$selectedDraft) return;
if (action === "add") {
addAll();
} else {
Expand Down Expand Up @@ -270,6 +270,7 @@
if (
oldIndex !== undoIndex &&
newValue &&
currentDraftIndex >= 0 &&
newValue.draftVaultPath === $drafts[currentDraftIndex].vaultPath &&
$drafts[currentDraftIndex].format === "scenes"
) {
Expand Down Expand Up @@ -361,7 +362,7 @@
</div>
</SortableList>
</div>
{#if $selectedDraft.format === "scenes" && $selectedDraft.unknownFiles.length > 0}
{#if $selectedDraft && $selectedDraft.format === "scenes" && $selectedDraft.unknownFiles.length > 0}
<div id="longform-unknown-files-wizard">
<div class="longform-unknown-inner">
<p class="longform-unknown-explanation">
Expand Down
15 changes: 15 additions & 0 deletions src/view/explorer/scene-menu-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 8f45706

Please sign in to comment.