Skip to content

Commit ffab2f8

Browse files
committed
Greatly improve perf of dragging list around sidebar
- If you dragged a list rapidly around the sidebar fast enough, the actions would form a queue and sequentially happen with a noticeable delay. This greatly improves that. - Main change is moving the setTimeout(mutation, 0) to the top level and not performing the mutation if nothing gets changed
1 parent dca0060 commit ffab2f8

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

src/dashboard-refactor/index.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,12 +812,26 @@ export class DashboardContainer extends StatefulUIElement<
812812
onListDragEnd={(listId) => (e) =>
813813
this.processEvent('dropList', { listId })}
814814
initDropReceivingState={(listId) => ({
815-
onDragEnter: () =>
816-
this.processEvent('setDragOverListId', { listId }),
817-
onDragLeave: () =>
815+
onDragEnter: (e) => {
816+
e.preventDefault()
817+
e.stopPropagation()
818+
// Needed to push this op back on the event queue, so it fires after the previous
819+
//  list item's `onDropLeave` event
820+
setTimeout(
821+
() =>
822+
this.processEvent('setDragOverListId', {
823+
listId,
824+
}),
825+
0,
826+
)
827+
},
828+
onDragLeave: (e) => {
829+
e.preventDefault()
830+
e.stopPropagation()
818831
this.processEvent('setDragOverListId', {
819832
listId: undefined,
820-
}),
833+
})
834+
},
821835
onDrop: (dataTransfer) =>
822836
this.processEvent('dropOnListItem', {
823837
listId,

src/dashboard-refactor/lists-sidebar/components/sidebar-item.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React from 'react'
22
import { fonts } from 'src/dashboard-refactor/styles'
33
import styled, { css } from 'styled-components'
4-
import Icon from '@worldbrain/memex-common/lib/common-ui/components/icon'
5-
import { TooltipBox } from '@worldbrain/memex-common/lib/common-ui/components/tooltip-box'
64
import type { DropReceivingState } from 'src/dashboard-refactor/types'
75

86
export interface Props {
@@ -38,14 +36,6 @@ export default class ListsSidebarItem extends React.PureComponent<
3836
> {
3937
state: State = { isHovering: false, canDisableHover: false }
4038

41-
private handleDragEnter: React.DragEventHandler = (e) => {
42-
e.preventDefault()
43-
e.stopPropagation()
44-
// Needed to push this op back on the event queue, so it fires after the previous
45-
//  list item's `onDropLeave` event
46-
setTimeout(() => this.props.dropReceivingState?.onDragEnter(), 0)
47-
}
48-
4939
private handleDrop: React.DragEventHandler = (e) => {
5040
e.preventDefault()
5141
if (!this.props.dropReceivingState?.canReceiveDroppedItems) {
@@ -75,7 +65,7 @@ export default class ListsSidebarItem extends React.PureComponent<
7565
})
7666
}}
7767
spaceSidebarWidth={this.props.spaceSidebarWidth}
78-
onDragEnter={this.handleDragEnter}
68+
onDragEnter={this.props.dropReceivingState?.onDragEnter}
7969
onDragLeave={this.props.dropReceivingState?.onDragLeave}
8070
onDragOver={(e) => {
8171
e.preventDefault()

src/dashboard-refactor/lists-sidebar/index.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,7 @@ export default class ListsSidebar extends PureComponent<ListsSidebarProps> {
116116
<ReorderLine
117117
isActive={this.props.someListIsDragging}
118118
isVisible={reorderLineDropReceivingState.isDraggedOver}
119-
onDragEnter={(e: React.DragEvent) => {
120-
e.preventDefault()
121-
e.stopPropagation()
122-
// Needed to push this op back on the event queue, so it fires after the previous
123-
//  list item's `onDropLeave` event
124-
setTimeout(
125-
() => reorderLineDropReceivingState.onDragEnter(),
126-
0,
127-
)
128-
}}
119+
onDragEnter={reorderLineDropReceivingState.onDragEnter}
129120
onDragLeave={reorderLineDropReceivingState.onDragLeave}
130121
onDragOver={(e: React.DragEvent) => {
131122
e.preventDefault()

src/dashboard-refactor/logic.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4382,9 +4382,13 @@ export class DashboardLogic extends UILogic<State, Events> {
43824382
})
43834383
}
43844384

4385-
setDragOverListId: EventHandler<'setDragOverListId'> = async ({
4385+
setDragOverListId: EventHandler<'setDragOverListId'> = ({
43864386
event,
4387+
previousState,
43874388
}) => {
4389+
if (event.listId === previousState.listsSidebar.dragOverListId) {
4390+
return
4391+
}
43884392
this.emitMutation({
43894393
listsSidebar: { dragOverListId: { $set: event.listId } },
43904394
})

src/dashboard-refactor/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import type { SpaceSearchSuggestion } from '@worldbrain/memex-common/lib/editor'
5252
import type { HighlightColor } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/types'
5353
import type { BulkEditCollection } from 'src/bulk-edit/types'
5454
import type { PersonalCloudRemoteInterface } from 'src/personal-cloud/background/types'
55+
import type { DragEventHandler } from 'react'
5556

5657
export interface RootState {
5758
loadState: TaskState
@@ -152,8 +153,8 @@ export interface DropReceivingState {
152153
wasPageDropped?: boolean
153154
canReceiveDroppedItems?: boolean
154155
onDrop(dataTransfer: DataTransfer): void
155-
onDragEnter(): void
156-
onDragLeave(): void
156+
onDragEnter: DragEventHandler
157+
onDragLeave: DragEventHandler
157158
}
158159

159160
export interface SearchResultTextPart {

0 commit comments

Comments
 (0)