-
Notifications
You must be signed in to change notification settings - Fork 69
refactor: Refactor shift selection ♻️ #3554
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
base: master
Are you sure you want to change the base?
Conversation
BundleMonFiles updated (2)
Unchanged files (19)
Total files change +162B 0% Groups updated (1)
Unchanged groups (2)
Final result: ✅ View report in BundleMon website ➡️ |
6e220e3
to
c640640
Compare
newSelected[targetItem._id] = targetItem | ||
} else { | ||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete newSelected[targetItem._id] |
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.
Please don't use delete, look here
7502202
to
2c4808e
Compare
2c4808e
to
1aa9225
Compare
* Clamps an index value to be within valid array bounds. | ||
* | ||
* @param maxLength - The maximum length of the array | ||
* @param index - The index to clamp |
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 a human generated JSDoc .
param {type} name description
1aa9225
to
b2a9217
Compare
b2a9217
to
a5960c1
Compare
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.
Testing locally, the shift select is working fine with double click, kudos for that 🎉
Thanks for the type definitions and the JSDoc attempt
But I have some remarks to point out:
- Making more functions does not mean that it will be easier to understand; having more than enough functions will split the mind of the person reading the code.
- Let's not abuse useMemo for things that are not necessary
- Try to avoid useEffect as much as possible; we already have enough.
- In the code, we are mixing responsibilities in functions. You have functions that are one-line return something, and functions that do more than they should do like
toggleSelection
- keyboard listener is bloated
- The state is overcomplicated
- isSelectAll state can be computed instead of computing it in an useEffect
- not all functions have JSDoc ( or a proper one with types )
- The useshiftselect hook should be tested
}, [lastInteractedItem, items]) | ||
|
||
const selectItemByIds: SelectedItems = useMemo(() => { | ||
return selectedItems.reduce<SelectedItems>( |
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.
Are you sure selectedItems is an array?
it is an object in the selection provider
const [selectedItems, setSelectedItems] = useState({})
isSelectionBarVisible: boolean | ||
|
||
/** List of selected items as an array */ | ||
selectedItems: IOCozyFile[] |
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 not an array it's an object
const viewTypeRef = useRef<ViewType>() | ||
viewTypeRef.current = useMemo(() => viewType, [viewType]) | ||
|
||
const itemsRef = useRef<IOCozyFile[]>() | ||
itemsRef.current = useMemo(() => items, [items]) |
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 is unnecessary and double-storing the value
useEffect(() => { | ||
setIsSelectAll(selectedItems.length === itemsRef.current?.length) | ||
}, [setIsSelectAll, selectedItems]) |
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.
Please avoid useEffect
> | ||
<GridFile | ||
const onToggleSelect = (fileId, event) => { | ||
event.stopPropagation() |
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.
why do you need to do it here outside the onSelect?
...row, | ||
index | ||
})) | ||
return rows |
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.
does this change have other impacts on virtualized table? @JF-Cozy
|
||
useShiftArrowsSelection({ items: sortedRow, viewType: 'list' }, tableRef) | ||
const handleRowSelect = (row, event) => { | ||
event.stopPropagation() |
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.
same comment
|
||
const onToggleSelect = (fileId, e) => { | ||
setLastInteractedItem(fileId) | ||
if (e.shiftKey) { |
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.
we shouldn't check the keyboard event key in each place we use the onShiftClick
const viewTypeRef = useRef<ViewType>() | ||
viewTypeRef.current = useMemo(() => viewType, [viewType]) | ||
|
||
const itemsRef = useRef<IOCozyFile[]>() |
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.
const itemsRef = useRef<IOCozyFile[]>() | |
const itemsRef = useRef<IOCozyFile[]>([]) |
|
||
/** | ||
* Returns a new SelectedItems object with the specified key removed. | ||
* This is a safe way to remove items from selection without using delete operator. |
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.
AI comment slipped in.
onShiftClick: (clickedItemId: string) => void | ||
} | ||
|
||
const useShiftSelection = ( |
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.
JSDoc
Change:
SelectionProvider
.useLongPress
to be available with shift selection.