mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 11:16:57 +00:00
Merge pull request #86 from shumaiOne/fix/grid-virtualization-collapse
fix(webui): add stable item keys to grid and list virtualizers
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
'use client'
|
||||
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import type { SearchSort } from '@shumai/dtos'
|
||||
import { useDroppable } from '@dnd-kit/react'
|
||||
import type { AssetInfo, SearchSort } from '@shumai/dtos'
|
||||
import { useVirtualizer } from '@tanstack/react-virtual'
|
||||
import { ChevronDown, ChevronRight } from 'lucide-react'
|
||||
import React, { useMemo, useState, useEffect, useRef } from 'react'
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { cn } from '../../lib/utils'
|
||||
import type { DragState } from '../dnd-types'
|
||||
|
||||
@@ -170,14 +169,21 @@ export function FileBrowserGridView({
|
||||
const rowVirtualizer = useVirtualizer({
|
||||
count: rows.length,
|
||||
getScrollElement: () => scrollContainerRef.current,
|
||||
estimateSize: (index) => (rows[index]?.type === 'header' ? 40 : 200),
|
||||
estimateSize: (index) => (rows[index]?.type === 'header' ? 40 : 350),
|
||||
getItemKey: React.useCallback(
|
||||
(index: number) => {
|
||||
const row = rows[index]
|
||||
if (!row) return index
|
||||
if (row.type === 'header') {
|
||||
return `header-${row.kind}`
|
||||
}
|
||||
return `row-${row.kind}-${row.rowIndex}`
|
||||
},
|
||||
[rows],
|
||||
),
|
||||
overscan: 5,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
rowVirtualizer.measure()
|
||||
}, [folders.length, files.length, foldersExpanded, filesExpanded, cols, rowVirtualizer])
|
||||
|
||||
const virtualItems = rowVirtualizer.getVirtualItems()
|
||||
|
||||
useEffect(() => {
|
||||
@@ -306,7 +312,7 @@ export function FileBrowserGridView({
|
||||
}
|
||||
|
||||
return (
|
||||
<div key={item.id} className="group-reorder relative flex">
|
||||
<div key={item.id} className="group-reorder relative flex pb-10">
|
||||
<ReorderIndicator
|
||||
id={`reorder-before-${row.kind}-${item.id}`}
|
||||
item={item}
|
||||
|
||||
@@ -257,13 +257,21 @@ export function FileBrowserListView({
|
||||
count: items.length,
|
||||
getScrollElement: () => localScrollRef.current,
|
||||
estimateSize: () => 40,
|
||||
getItemKey: React.useCallback(
|
||||
(index: number) => {
|
||||
const item = items[index]
|
||||
if (!item) return index
|
||||
if (item.type === 'header') {
|
||||
return `header-${item.kind}`
|
||||
}
|
||||
const dataItem = item.kind === 'folder' ? folders[item.index] : files[item.index]
|
||||
return dataItem?.id || `item-${item.kind}-${item.index}`
|
||||
},
|
||||
[items, folders, files],
|
||||
),
|
||||
overscan: 10,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
rowVirtualizer.measure()
|
||||
}, [folders.length, files.length, foldersExpanded, filesExpanded, rowVirtualizer])
|
||||
|
||||
const virtualItems = rowVirtualizer.getVirtualItems()
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user