-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Third bunch of possible undefined thoughts #2744
Conversation
@@ -8,7 +8,7 @@ const parentOfThought = (state: State, thoughtId: ThoughtId): Thought | null => | |||
const thought = getThoughtById(state, thoughtId) | |||
if (!thought) return null | |||
const parentThought = getThoughtById(state, thought.parentId) | |||
return parentThought | |||
return parentThought ?? null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preserve fn signature
@@ -36,7 +36,7 @@ export const updateCommandState = () => { | |||
const action = | |||
selection.isActive() && selection.isOnThought() | |||
? getCommandState(selection.html() ?? '') | |||
: getCommandState(pathToThought(state, state.cursor).value) | |||
: getCommandState(pathToThought(state, state.cursor)?.value ?? '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on that end we have a path.length === 0
default case, so fallback is fine here
const rankStart = destEmpty ? destThought.rank : getNextRank(state, head(simplePath)) | ||
const rankIncrement = getRankIncrement(state, blocks, destThought, rankStart) | ||
const rankStart = destEmpty ? destThought?.rank : getNextRank(state, head(simplePath)) | ||
const rankIncrement = destEmpty ? getRankIncrement(state, blocks, destThought, rankStart) : 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 is a fallback in getRankIncrement too, so that's my judgment
@@ -19,6 +19,8 @@ const getThoughtAndParentPath = (state: State, at: string[]): [Thought, Path] => | |||
|
|||
const thought = pathToThought(state, path) | |||
|
|||
if (!thought) throw new Error(`Thought not found for path: ${path}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a test file, and we throw an error for empty path above, so thought why not
src/test-helpers/contextToThought.ts
Outdated
@@ -9,7 +9,7 @@ import getThoughtById from '../selectors/getThoughtById' | |||
*/ | |||
const contextToThought = (state: State, context: Context): Thought | null => { | |||
const id = contextToThoughtId(state, context) | |||
return id ? getThoughtById(state, id) : null | |||
return id ? (getThoughtById(state, id) ?? null) : null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to preserve type for now
but there's an itch to make it Thought | null | undefined
wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would do Thought | null
or Thought | undefined
since we don't need to distinguish between null
and undefined
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I made it Thought | undefined
, and it broke nothing
(it broke one test :D)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
src/actions/formatLetterCase.ts
Outdated
const originalThoughtValue = thought.value | ||
const originalThoughtValue = thought?.value | ||
|
||
if (originalThoughtValue === undefined) return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be a bit more direct here (above the originalThoughtValue
declaration):
if (!thought) return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I got you right, but here's what I've ended up with
const cursor = state.cursor
if (!cursor) return
const originalThoughtValue = pathToThought(state, cursor)?.value
if (originalThoughtValue === undefined) return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
originalThoughtValue
can only be undefined due to the optional chaining operator. It doesn't really make sense to use it here in order to check for undefined, when what you really want to check is if the thought is undefined. thought.value
is always a string. There's no reason to get it mixed up with the undefined thought check.
const thought = pathToThought(state, cursor)
if (!thought) return
newRank: (dropTop ? getPrevRank : getNextRank)(state, thoughtTo.id), | ||
}), | ||
) | ||
if (thoughtTo) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if (!thoughtTo) return
or combining it with the "cannot drop on itself" short circuit.
src/initialize.ts
Outdated
return contexts.map(id => thoughtToContext(state, getThoughtById(state, id)?.parentId)) | ||
return contexts | ||
.map(id => getThoughtById(state, id)) | ||
.filter(Boolean) | ||
.map(context => context.parentId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think thoughtToContext
got lost here, altering the previous behavior.
src/selectors/prevSibling.ts
Outdated
const siblings = showContexts | ||
? getContextsSortedAndRanked(state, getThoughtById(state, head(parentPath)).value) | ||
: getChildrenSorted(state, thought.parentId) | ||
const siblings = | ||
showContexts && parent | ||
? getContextsSortedAndRanked(state, parent.value) | ||
: getChildrenSorted(state, thought.parentId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will incorrectly return the siblings from the normal view if parent
is missing. It would be better to return []
when showContexts
is true and the parent is missing.
src/test-helpers/contextToThought.ts
Outdated
@@ -9,7 +9,7 @@ import getThoughtById from '../selectors/getThoughtById' | |||
*/ | |||
const contextToThought = (state: State, context: Context): Thought | null => { | |||
const id = contextToThoughtId(state, context) | |||
return id ? getThoughtById(state, id) : null | |||
return id ? (getThoughtById(state, id) ?? null) : null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would do Thought | null
or Thought | undefined
since we don't need to distinguish between null
and undefined
here.
src/util/importJSON.ts
Outdated
@@ -63,6 +63,7 @@ const insertThought = ( | |||
}, | |||
) => { | |||
const thoughtOld = getThoughtById(state, id) | |||
if (!thoughtOld) return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's return null
here since insertUpdates
is nullable below.
if (!thoughtTo) { | ||
console.warn(`Cannot drop ${thoughtFrom} on itself. Aborting drop.`) | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This console.warn
is not accurate, as dropping a thought on itself is already detected and short circuited using the equalPath
check above. Please update the message or remove the console.warn
completely, combining it with the first return statement.
@@ -134,7 +139,7 @@ const drop = (props: DroppableSubthoughts, monitor: DropTargetMonitor) => { | |||
) | |||
|
|||
// alert user of move to another context | |||
if (!sameContext) { | |||
if (!sameContext && thoughtTo && thoughtFrom) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't necessary due to the short circuit above.
#2711