Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 5 additions & 37 deletions src/features/drawer/components/SelectorList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ import { BoolToggle } from '@components/inputs/BoolToggle'
import { GenericSearchMemo } from '@components/inputs/GenericSearch'
import { StandardItem } from '@components/virtual/StandardItem'

import {
getDrawerGridState,
setDrawerGridState,
useDrawerScrollMemory,
} from '../hooks/useScrollMemory'
import { useDrawerScrollMemory } from '../hooks/useScrollMemory'

/**
* @template {keyof import('@rm/types').Available} T
Expand Down Expand Up @@ -111,31 +107,9 @@ function SelectorList({
.map((item) => item.id)
}, [translated, search])

const restoreStateFrom = React.useMemo(
() => getDrawerGridState(listScrollKey),
[listScrollKey],
)
const shouldPersistGridState = drawer && visible
const scrollMemory = useDrawerScrollMemory(
listScrollKey,
shouldPersistGridState,
)

const handleStateChanged = React.useCallback(
(state) => {
if (
!shouldPersistGridState ||
!state.viewport.height ||
!state.viewport.width ||
!state.item.height ||
!state.item.width
) {
return
}
setDrawerGridState(listScrollKey, state)
},
[listScrollKey, shouldPersistGridState],
)
// only the scroll offset is restored: the grid works out which tiles to draw from it. Also restoring a
// grid state snapshot made the two disagree, and the grid drew a range that wasn't on screen (blank tiles)
const scrollMemory = useDrawerScrollMemory(listScrollKey, drawer && visible)

/** @param {'enable' | 'disable' | 'advanced'} action */
const setAll = (action) => {
Expand Down Expand Up @@ -226,13 +200,7 @@ function SelectorList({
: height
}
>
<VirtualGrid
data={items}
xs={4}
scrollerRef={scrollMemory.ref}
restoreStateFrom={restoreStateFrom}
stateChanged={handleStateChanged}
>
<VirtualGrid data={items} xs={4} scrollerRef={scrollMemory.ref}>
{(_, key) => (
<StandardItem
id={key}
Expand Down
19 changes: 0 additions & 19 deletions src/features/drawer/hooks/useScrollMemory.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
// @ts-check
import * as React from 'react'

/** @type {Map<string, import('react-virtuoso').GridStateSnapshot>} */
const gridStateMemory = new Map()

/** @type {Map<string, number>} */
const scrollTopMemory = new Map()

/**
* @param {string} key
* @returns {import('react-virtuoso').GridStateSnapshot | null}
*/
export function getDrawerGridState(key) {
return gridStateMemory.get(key) || null
}

/**
* @param {string} key
* @param {import('react-virtuoso').GridStateSnapshot} state
*/
export function setDrawerGridState(key, state) {
gridStateMemory.set(key, state)
}

/**
* @template {HTMLElement} T
* @param {string} key
Expand Down
Loading