Skip to content

Commit

Permalink
Add custom equality function to useSelector in useDividerData.
Browse files Browse the repository at this point in the history
  • Loading branch information
ohrytskov committed Jan 19, 2025
1 parent 7df6c65 commit 8270579
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/components/Divider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,43 @@ const useDividerData = (path: Path) => {
const grandParentPath = parentOf(parentPath)
const grandParentId = head(grandParentPath)

return useSelector((state: State) => {
const children = parentId ? getAllChildrenAsThoughts(state, parentId) : []
const childrenWithoutDividers = children.filter(child => !isDivider(child.value))
const isOnlyChild = childrenWithoutDividers.length === 0
const isTableView =
(parentId && attributeEquals(state, parentId, '=view', 'Table')) ||
(grandParentId && attributeEquals(state, grandParentId, '=view', 'Table'))

let thoughtsAtSameDepth: { id: ThoughtId; value: string }[] = []

if (isTableView && isOnlyChild && grandParentId) {
const parentSiblings = getAllChildrenAsThoughts(state, grandParentId)
thoughtsAtSameDepth = parentSiblings.flatMap(parent => {
const childrenOfParent = parent.id ? getAllChildrenAsThoughts(state, parent.id) : []
return childrenOfParent.filter(child => !isDivider(child.value))
})
}
return useSelector(
(state: State) => {
const children = parentId ? getAllChildrenAsThoughts(state, parentId) : []
const childrenWithoutDividers = children.filter(child => !isDivider(child.value))
const isOnlyChild = childrenWithoutDividers.length === 0
const isTableView =
(parentId && attributeEquals(state, parentId, '=view', 'Table')) ||
(grandParentId && attributeEquals(state, grandParentId, '=view', 'Table'))

let thoughtsAtSameDepth: { id: ThoughtId; value: string }[] = []

if (isTableView && isOnlyChild && grandParentId) {
const parentSiblings = getAllChildrenAsThoughts(state, grandParentId)
thoughtsAtSameDepth = parentSiblings.flatMap(parent => {
const childrenOfParent = parent.id ? getAllChildrenAsThoughts(state, parent.id) : []
return childrenOfParent.filter(child => !isDivider(child.value))
})
}

return {
dividerId,
parentId,
isOnlyChild,
isTableView,
children,
thoughtsAtSameDepth,
}
})
return {
dividerId,
parentId,
isOnlyChild,
isTableView,
children,
thoughtsAtSameDepth,
}
},
(prev, next) => {
return (
prev.dividerId === next.dividerId &&
prev.parentId === next.parentId &&
prev.isOnlyChild === next.isOnlyChild &&
prev.isTableView === next.isTableView
)
},
)
}

const canvas = document.createElement('canvas')
Expand Down

0 comments on commit 8270579

Please sign in to comment.