From 9b791b4f3c77ca17ee59c758ed09f5b7d13fc805 Mon Sep 17 00:00:00 2001 From: Cats Juice Date: Thu, 7 Nov 2024 14:21:26 +0800 Subject: [PATCH] feat(mobile): use cached random height for all docs masonry --- .../src/mobile/components/doc-card/index.tsx | 17 ++++++- .../mobile/components/doc-card/styles.css.tsx | 1 - .../mobile/views/all-docs/doc/masonry.css.ts | 3 -- .../src/mobile/views/all-docs/doc/masonry.tsx | 48 ++++++++++++++----- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/packages/frontend/core/src/mobile/components/doc-card/index.tsx b/packages/frontend/core/src/mobile/components/doc-card/index.tsx index dfd0509735978..0a17cb21cb92a 100644 --- a/packages/frontend/core/src/mobile/components/doc-card/index.tsx +++ b/packages/frontend/core/src/mobile/components/doc-card/index.tsx @@ -21,10 +21,18 @@ export interface DocCardProps extends Omit { title?: ReactNode; } & { [key: string]: any }; showTags?: boolean; + + /** + * If provided, content's height will be calculated: `previewRows * line-height` + */ + previewRows?: number; } export const DocCard = forwardRef( - function DocCard({ showTags = true, meta, className, ...attrs }, ref) { + function DocCard( + { showTags = true, meta, className, previewRows, ...attrs }, + ref + ) { const favAdapter = useService(CompatibleFavoriteItemsAdapter); const workspace = useService(WorkspaceService).workspace; @@ -38,6 +46,8 @@ export const DocCard = forwardRef( [favAdapter, meta.id] ); + const contentHeight = previewRows ? `${previewRows * 18}px` : 'unset'; + return ( ( } /> -
+
{ + const maxCardWidth = 220; + const windowWidth = window.innerWidth; + const newColumnCount = Math.floor( + (windowWidth - styles.paddingX * 2 - styles.columnGap) / maxCardWidth + ); + return Math.max(newColumnCount, 2); +}; + export const MasonryDocs = ({ items, showTags, @@ -12,30 +25,39 @@ export const MasonryDocs = ({ items: DocMeta[]; showTags?: boolean; }) => { - const [columnCount, setColumnCount] = useState(2); + const globalCache = useService(GlobalCacheService).globalCache; + const [columnCount, setColumnCount] = useState(calcColumnCount()); const updateColumnCount = useCallback(() => { - const maxCardWidth = 220; - const windowWidth = window.innerWidth; - const newColumnCount = Math.floor( - (windowWidth - styles.paddingX * 2 - styles.columnGap) / maxCardWidth - ); - setColumnCount(Math.max(newColumnCount, 2)); + setColumnCount(calcColumnCount()); }, []); - useGlobalEvent('resize', updateColumnCount); - useEffect(() => { - updateColumnCount(); - }, [updateColumnCount]); + + // Randomly assign preview rows to each item and cache it + const previewRowsMap = useMemo(() => { + const cache = globalCache.get>(cacheKey) || {}; + const newCache = items.reduce( + (acc, item) => { + if (cache[item.id]) return acc; + const row = rows[Math.floor(Math.random() * rows.length)]; + acc[item.id] = row; + return acc; + }, + { ...cache } + ); + globalCache.set(cacheKey, newCache); + return newCache; + }, [globalCache, items]); return (
{items.map(item => ( ))}