Skip to content

Commit

Permalink
Greatly improve perf of dragging list around sidebar
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
poltak committed Jun 19, 2024
1 parent dca0060 commit ffab2f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
22 changes: 18 additions & 4 deletions src/dashboard-refactor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -812,12 +812,26 @@ export class DashboardContainer extends StatefulUIElement<
onListDragEnd={(listId) => (e) =>
this.processEvent('dropList', { listId })}
initDropReceivingState={(listId) => ({
onDragEnter: () =>
this.processEvent('setDragOverListId', { listId }),
onDragLeave: () =>
onDragEnter: (e) => {
e.preventDefault()
e.stopPropagation()
// Needed to push this op back on the event queue, so it fires after the previous
//  list item's `onDropLeave` event
setTimeout(
() =>
this.processEvent('setDragOverListId', {
listId,
}),
0,
)
},
onDragLeave: (e) => {
e.preventDefault()
e.stopPropagation()
this.processEvent('setDragOverListId', {
listId: undefined,
}),
})
},
onDrop: (dataTransfer) =>
this.processEvent('dropOnListItem', {
listId,
Expand Down
12 changes: 1 addition & 11 deletions src/dashboard-refactor/lists-sidebar/components/sidebar-item.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from 'react'
import { fonts } from 'src/dashboard-refactor/styles'
import styled, { css } from 'styled-components'
import Icon from '@worldbrain/memex-common/lib/common-ui/components/icon'
import { TooltipBox } from '@worldbrain/memex-common/lib/common-ui/components/tooltip-box'
import type { DropReceivingState } from 'src/dashboard-refactor/types'

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

private handleDragEnter: React.DragEventHandler = (e) => {
e.preventDefault()
e.stopPropagation()
// Needed to push this op back on the event queue, so it fires after the previous
//  list item's `onDropLeave` event
setTimeout(() => this.props.dropReceivingState?.onDragEnter(), 0)
}

private handleDrop: React.DragEventHandler = (e) => {
e.preventDefault()
if (!this.props.dropReceivingState?.canReceiveDroppedItems) {
Expand Down Expand Up @@ -75,7 +65,7 @@ export default class ListsSidebarItem extends React.PureComponent<
})
}}
spaceSidebarWidth={this.props.spaceSidebarWidth}
onDragEnter={this.handleDragEnter}
onDragEnter={this.props.dropReceivingState?.onDragEnter}
onDragLeave={this.props.dropReceivingState?.onDragLeave}
onDragOver={(e) => {
e.preventDefault()
Expand Down
11 changes: 1 addition & 10 deletions src/dashboard-refactor/lists-sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,7 @@ export default class ListsSidebar extends PureComponent<ListsSidebarProps> {
<ReorderLine
isActive={this.props.someListIsDragging}
isVisible={reorderLineDropReceivingState.isDraggedOver}
onDragEnter={(e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
// Needed to push this op back on the event queue, so it fires after the previous
//  list item's `onDropLeave` event
setTimeout(
() => reorderLineDropReceivingState.onDragEnter(),
0,
)
}}
onDragEnter={reorderLineDropReceivingState.onDragEnter}
onDragLeave={reorderLineDropReceivingState.onDragLeave}
onDragOver={(e: React.DragEvent) => {
e.preventDefault()
Expand Down
6 changes: 5 additions & 1 deletion src/dashboard-refactor/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4382,9 +4382,13 @@ export class DashboardLogic extends UILogic<State, Events> {
})
}

setDragOverListId: EventHandler<'setDragOverListId'> = async ({
setDragOverListId: EventHandler<'setDragOverListId'> = ({
event,
previousState,
}) => {
if (event.listId === previousState.listsSidebar.dragOverListId) {
return
}
this.emitMutation({
listsSidebar: { dragOverListId: { $set: event.listId } },
})
Expand Down
5 changes: 3 additions & 2 deletions src/dashboard-refactor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type { SpaceSearchSuggestion } from '@worldbrain/memex-common/lib/editor'
import type { HighlightColor } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/types'
import type { BulkEditCollection } from 'src/bulk-edit/types'
import type { PersonalCloudRemoteInterface } from 'src/personal-cloud/background/types'
import type { DragEventHandler } from 'react'

export interface RootState {
loadState: TaskState
Expand Down Expand Up @@ -152,8 +153,8 @@ export interface DropReceivingState {
wasPageDropped?: boolean
canReceiveDroppedItems?: boolean
onDrop(dataTransfer: DataTransfer): void
onDragEnter(): void
onDragLeave(): void
onDragEnter: DragEventHandler
onDragLeave: DragEventHandler
}

export interface SearchResultTextPart {
Expand Down

0 comments on commit ffab2f8

Please sign in to comment.