From 01b8c52b5002599308877df9ce363780c8fa2dbb Mon Sep 17 00:00:00 2001 From: Esen Date: Fri, 3 Jan 2025 17:40:31 +0600 Subject: [PATCH] address feedback --- src/actions/formatLetterCase.ts | 4 +--- src/hooks/useDragAndDropSubThought.ts | 19 +++++++++++-------- src/initialize.ts | 2 +- src/selectors/prevSibling.ts | 7 ++++--- src/test-helpers/contextToThought.ts | 4 ++-- src/util/importJSON.ts | 2 +- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/actions/formatLetterCase.ts b/src/actions/formatLetterCase.ts index 120aa8f749..053633e387 100644 --- a/src/actions/formatLetterCase.ts +++ b/src/actions/formatLetterCase.ts @@ -16,9 +16,7 @@ export const formatLetterCaseActionCreator = const cursor = state.cursor if (!cursor) return - const thought = pathToThought(state, cursor) - const originalThoughtValue = thought?.value - + const originalThoughtValue = pathToThought(state, cursor)?.value if (originalThoughtValue === undefined) return const updatedThoughtValue = applyLetterCase(command, originalThoughtValue) diff --git a/src/hooks/useDragAndDropSubThought.ts b/src/hooks/useDragAndDropSubThought.ts index 044ec414bf..5074aac0ce 100644 --- a/src/hooks/useDragAndDropSubThought.ts +++ b/src/hooks/useDragAndDropSubThought.ts @@ -125,16 +125,19 @@ const drop = (props: DroppableSubthoughts, monitor: DropTargetMonitor) => { return } - if (thoughtTo) { - store.dispatch( - moveThought({ - oldPath: thoughtsFrom, - newPath: pathTo, - newRank: (dropTop ? getPrevRank : getNextRank)(state, thoughtTo.id), - }), - ) + if (!thoughtTo) { + console.warn(`Cannot drop ${thoughtFrom} on itself. Aborting drop.`) + return } + store.dispatch( + moveThought({ + oldPath: thoughtsFrom, + newPath: pathTo, + newRank: (dropTop ? getPrevRank : getNextRank)(state, thoughtTo.id), + }), + ) + // alert user of move to another context if (!sameContext && thoughtTo && thoughtFrom) { // wait until after MultiGesture has cleared the error so this alert does no get cleared diff --git a/src/initialize.ts b/src/initialize.ts index 99e232c49a..e48d670a59 100644 --- a/src/initialize.ts +++ b/src/initialize.ts @@ -208,7 +208,7 @@ const windowEm = { return contexts .map(id => getThoughtById(state, id)) .filter(Boolean) - .map(context => context.parentId) + .map(thought => thoughtToContext(state, thought.parentId)) }), getAllChildrenByContext: withState((state: State, context: Context) => getAllChildren(state, contextToThoughtId(state, context) || null), diff --git a/src/selectors/prevSibling.ts b/src/selectors/prevSibling.ts index 0c701c68b5..526c4049eb 100644 --- a/src/selectors/prevSibling.ts +++ b/src/selectors/prevSibling.ts @@ -31,10 +31,11 @@ export const prevSibling = ( const showContexts = showContextsForced ?? isContextViewActive(state, parentPath) // siblings, including the current thought - const siblings = - showContexts && parent + const siblings = showContexts + ? parent ? getContextsSortedAndRanked(state, parent.value) - : getChildrenSorted(state, thought.parentId) + : [] + : getChildrenSorted(state, thought.parentId) // in context view, we need to match the context's parentId, since all context's ids refer to lexeme instances const index = siblings.findIndex(child => (showContexts ? child.parentId : child.id) === id) diff --git a/src/test-helpers/contextToThought.ts b/src/test-helpers/contextToThought.ts index 55bbaec1f3..d32398617e 100644 --- a/src/test-helpers/contextToThought.ts +++ b/src/test-helpers/contextToThought.ts @@ -7,9 +7,9 @@ import getThoughtById from '../selectors/getThoughtById' /** * Converts a Context to a Thought. If more than one thought has the same value in the same context, traveerses the first. */ -const contextToThought = (state: State, context: Context): Thought | null => { +const contextToThought = (state: State, context: Context): Thought | undefined => { const id = contextToThoughtId(state, context) - return id ? (getThoughtById(state, id) ?? null) : null + return id ? getThoughtById(state, id) : undefined } export default contextToThought diff --git a/src/util/importJSON.ts b/src/util/importJSON.ts index 9b0248b9df..11bb0f3021 100644 --- a/src/util/importJSON.ts +++ b/src/util/importJSON.ts @@ -63,7 +63,7 @@ const insertThought = ( }, ) => { const thoughtOld = getThoughtById(state, id) - if (!thoughtOld) return + if (!thoughtOld) return null const childLastUpdated = block.children[0]?.lastUpdated const childCreated = block.children[0]?.created const lastUpdatedInherited =