Skip to content
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

Merged
merged 6 commits into from
Jan 3, 2025

Conversation

snqb
Copy link
Collaborator

@snqb snqb commented Dec 28, 2024

@snqb snqb marked this pull request as draft December 28, 2024 23:55
@@ -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
Copy link
Collaborator Author

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 ?? '')
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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}`)
Copy link
Collaborator Author

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

@@ -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
Copy link
Collaborator Author

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?

Copy link
Contributor

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.

Copy link
Collaborator Author

@snqb snqb Jan 3, 2025

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)

@snqb snqb marked this pull request as ready for review December 29, 2024 00:12
Copy link
Contributor

@raineorshine raineorshine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment on lines 20 to 22
const originalThoughtValue = thought.value
const originalThoughtValue = thought?.value

if (originalThoughtValue === undefined) return
Copy link
Contributor

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

Copy link
Collaborator Author

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

Copy link
Contributor

@raineorshine raineorshine Jan 3, 2025

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) {
Copy link
Contributor

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.

Comment on lines 208 to 211
return contexts.map(id => thoughtToContext(state, getThoughtById(state, id)?.parentId))
return contexts
.map(id => getThoughtById(state, id))
.filter(Boolean)
.map(context => context.parentId)
Copy link
Contributor

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.

Comment on lines 33 to 37
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)
Copy link
Contributor

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.

@@ -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
Copy link
Contributor

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.

@@ -63,6 +63,7 @@ const insertThought = (
},
) => {
const thoughtOld = getThoughtById(state, id)
if (!thoughtOld) return
Copy link
Contributor

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.

@snqb snqb requested a review from raineorshine January 3, 2025 11:41
@trevinhofmann trevinhofmann assigned trevinhofmann and unassigned snqb Jan 3, 2025
@trevinhofmann trevinhofmann self-requested a review January 3, 2025 20:12
Comment on lines 128 to 131
if (!thoughtTo) {
console.warn(`Cannot drop ${thoughtFrom} on itself. Aborting drop.`)
return
}
Copy link
Contributor

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) {
Copy link
Contributor

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.

@raineorshine raineorshine merged commit 0b0697d into cybersemics:main Jan 3, 2025
3 checks passed
@trevinhofmann trevinhofmann removed their assignment Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants