首页布局优化前
This commit is contained in:
@@ -9,10 +9,7 @@
|
||||
|
||||
import { useRef, useCallback, useMemo, useEffect, type CSSProperties, type ReactNode } from 'react';
|
||||
import React from 'react';
|
||||
import { VariableSizeList } from 'react-window';
|
||||
|
||||
// Type alias for convenience
|
||||
type List = VariableSizeList;
|
||||
import type { ListImperativeAPI } from 'react-window';
|
||||
|
||||
/**
|
||||
* Message item interface for virtualization
|
||||
@@ -52,8 +49,8 @@ const DEFAULT_HEIGHTS: Record<string, number> = {
|
||||
* Hook return type for virtualized message management
|
||||
*/
|
||||
export interface UseVirtualizedMessagesReturn {
|
||||
/** Reference to the VariableSizeList instance */
|
||||
listRef: React.RefObject<VariableSizeList | null>;
|
||||
/** Reference to the List instance */
|
||||
listRef: React.RefObject<ListImperativeAPI | null>;
|
||||
/** Get the current height for a message by id and role */
|
||||
getHeight: (id: string, role: string) => number;
|
||||
/** Update the measured height for a message */
|
||||
@@ -99,7 +96,7 @@ export function useVirtualizedMessages(
|
||||
messages: VirtualizedMessageItem[],
|
||||
defaultHeights: Record<string, number> = DEFAULT_HEIGHTS
|
||||
): UseVirtualizedMessagesReturn {
|
||||
const listRef = useRef<List>(null);
|
||||
const listRef = useRef<ListImperativeAPI>(null);
|
||||
const heightsRef = useRef<Map<string, number>>(new Map());
|
||||
const prevMessagesLengthRef = useRef<number>(0);
|
||||
|
||||
@@ -121,8 +118,7 @@ export function useVirtualizedMessages(
|
||||
const current = heightsRef.current.get(id);
|
||||
if (current !== height) {
|
||||
heightsRef.current.set(id, height);
|
||||
// Reset cache to force recalculation
|
||||
listRef.current?.resetAfterIndex(0);
|
||||
// Height updated - the list will use the new height on next render
|
||||
}
|
||||
}, []);
|
||||
|
||||
@@ -141,7 +137,7 @@ export function useVirtualizedMessages(
|
||||
*/
|
||||
const scrollToBottom = useCallback((): void => {
|
||||
if (listRef.current && messages.length > 0) {
|
||||
listRef.current.scrollToItem(messages.length - 1, 'end');
|
||||
listRef.current.scrollToRow({ index: messages.length - 1, align: 'end' });
|
||||
}
|
||||
}, [messages.length]);
|
||||
|
||||
@@ -150,7 +146,7 @@ export function useVirtualizedMessages(
|
||||
*/
|
||||
const scrollToIndex = useCallback((index: number): void => {
|
||||
if (listRef.current && index >= 0 && index < messages.length) {
|
||||
listRef.current.scrollToItem(index, 'center');
|
||||
listRef.current.scrollToRow({ index, align: 'center' });
|
||||
}
|
||||
}, [messages.length]);
|
||||
|
||||
@@ -159,7 +155,6 @@ export function useVirtualizedMessages(
|
||||
*/
|
||||
const resetCache = useCallback((): void => {
|
||||
heightsRef.current.clear();
|
||||
listRef.current?.resetAfterIndex(0);
|
||||
}, []);
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user