mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
perf(catalog): optimize feed row rendering
This commit is contained in:
@@ -116,15 +116,12 @@ const CatalogPost = ({ post }: { post: Comment }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
interface CatalogRowProps {
|
interface CatalogRowProps {
|
||||||
description: any | undefined;
|
index?: number;
|
||||||
index: number;
|
|
||||||
rules: any | undefined;
|
|
||||||
row: Comment[];
|
row: Comment[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const CatalogRow = ({ description, index, row, rules }: CatalogRowProps) => {
|
const CatalogRow = ({ row }: CatalogRowProps) => {
|
||||||
const rowPosts = index === 0 ? [...(rules?.content?.length > 0 ? [rules] : []), ...(description?.content?.length > 0 ? [description] : []), ...row] : row;
|
const posts = row.map((post, index) => <CatalogPost key={index} post={post} />);
|
||||||
const posts = rowPosts.map((post, index) => <CatalogPost key={index} post={post} />);
|
|
||||||
return <div className={styles.row}>{posts}</div>;
|
return <div className={styles.row}>{posts}</div>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useMemo, useRef } from 'react';
|
import { useEffect, useMemo, useRef } from 'react';
|
||||||
import { useParams } from 'react-router-dom';
|
import { useParams } from 'react-router-dom';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useFeed, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
import { Subplebbit, useFeed, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
||||||
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
|
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
|
||||||
import useFeedStateString from '../../hooks/use-feed-state-string';
|
import useFeedStateString from '../../hooks/use-feed-state-string';
|
||||||
import useWindowWidth from '../../hooks/use-window-width';
|
import useWindowWidth from '../../hooks/use-window-width';
|
||||||
@@ -11,40 +11,63 @@ import styles from './catalog.module.css';
|
|||||||
|
|
||||||
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
|
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
|
||||||
|
|
||||||
const useFeedRows = (feed: any[], columnCount: number, includeDescription: boolean, includeRules: boolean) => {
|
const useFeedRows = (columnCount: number, feed: any, isFeedLoaded: boolean, subplebbit: Subplebbit) => {
|
||||||
const rowsRef = useRef<any[]>([]);
|
const modifiedFeed = useMemo(() => {
|
||||||
return useMemo(() => {
|
if (!isFeedLoaded) {
|
||||||
const rows: any[] = [];
|
return []; // prevent rules and description from appearing while feed is loading
|
||||||
let startIndex = 0;
|
|
||||||
let adjustment = 0; // Adjustment for the first row if description/rules are included
|
|
||||||
|
|
||||||
// Calculate adjustment for the first row
|
|
||||||
if (includeDescription) adjustment += 1;
|
|
||||||
if (includeRules) adjustment += 1;
|
|
||||||
|
|
||||||
while (startIndex < feed.length) {
|
|
||||||
let currentColumnCount = columnCount;
|
|
||||||
|
|
||||||
// Adjust the number of items in the first row to compensate for description/rules
|
|
||||||
if (rows.length === 0 && adjustment > 0) {
|
|
||||||
currentColumnCount = columnCount - adjustment;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure we don't try to slice more items than are available
|
|
||||||
const endIndex = Math.min(startIndex + currentColumnCount, feed.length);
|
|
||||||
|
|
||||||
if (rowsRef.current[rows.length] && rowsRef.current[rows.length].length === currentColumnCount) {
|
|
||||||
rows.push(rowsRef.current[rows.length]);
|
|
||||||
} else {
|
|
||||||
rows.push(feed.slice(startIndex, endIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
startIndex = endIndex; // Update startIndex to the end of the current row
|
|
||||||
}
|
}
|
||||||
|
if (!subplebbit?.description && !subplebbit?.rules) {
|
||||||
|
return feed;
|
||||||
|
}
|
||||||
|
const _feed = [...feed];
|
||||||
|
if (subplebbit?.description) {
|
||||||
|
_feed.unshift({
|
||||||
|
isDescription: true,
|
||||||
|
subplebbitAddress: subplebbit?.address,
|
||||||
|
timestamp: subplebbit?.createdAt,
|
||||||
|
author: { displayName: '## Board Mods' },
|
||||||
|
content: subplebbit?.description,
|
||||||
|
link: subplebbit?.suggested?.avatarUrl,
|
||||||
|
title: 'Welcome to ' + (subplebbit?.title || `p/${subplebbit?.shortAddress}`),
|
||||||
|
pinned: true,
|
||||||
|
locked: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (subplebbit?.rules) {
|
||||||
|
_feed.unshift({
|
||||||
|
isRules: true,
|
||||||
|
subplebbitAddress: subplebbit?.address,
|
||||||
|
timestamp: subplebbit?.createdAt,
|
||||||
|
author: { displayName: '## Board Mods' },
|
||||||
|
content: subplebbit?.rules.map((rule: string, index: number) => `${index + 1}. ${rule}`).join('\n'),
|
||||||
|
title: 'Rules',
|
||||||
|
pinned: true,
|
||||||
|
locked: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return _feed;
|
||||||
|
}, [
|
||||||
|
feed,
|
||||||
|
subplebbit?.description,
|
||||||
|
subplebbit?.rules,
|
||||||
|
subplebbit?.address,
|
||||||
|
isFeedLoaded,
|
||||||
|
subplebbit?.createdAt,
|
||||||
|
subplebbit?.title,
|
||||||
|
subplebbit?.shortAddress,
|
||||||
|
subplebbit?.suggested?.avatarUrl,
|
||||||
|
]);
|
||||||
|
|
||||||
rowsRef.current = rows;
|
// Memoize rows calculation, ensuring it updates on changes to the modified feed or column count
|
||||||
|
const rows = useMemo(() => {
|
||||||
|
const rows = [];
|
||||||
|
for (let i = 0; i < modifiedFeed.length; i += columnCount) {
|
||||||
|
rows.push(modifiedFeed.slice(i, i + columnCount));
|
||||||
|
}
|
||||||
return rows;
|
return rows;
|
||||||
}, [feed, columnCount, includeDescription, includeRules]);
|
}, [modifiedFeed, columnCount]);
|
||||||
|
|
||||||
|
return rows;
|
||||||
};
|
};
|
||||||
|
|
||||||
const columnWidth = 180;
|
const columnWidth = 180;
|
||||||
@@ -62,8 +85,7 @@ const Catalog = () => {
|
|||||||
const { feed, hasMore, loadMore } = useFeed({ subplebbitAddresses, sortType: 'active', postsPerPage });
|
const { feed, hasMore, loadMore } = useFeed({ subplebbitAddresses, sortType: 'active', postsPerPage });
|
||||||
|
|
||||||
const subplebbit = useSubplebbit({ subplebbitAddress });
|
const subplebbit = useSubplebbit({ subplebbitAddress });
|
||||||
const { createdAt, description, rules, shortAddress, state, suggested, title } = subplebbit || {};
|
const { shortAddress, state, title } = subplebbit || {};
|
||||||
const { avatarUrl } = suggested || {};
|
|
||||||
const loadingStateString = useFeedStateString(subplebbitAddresses) || t('loading');
|
const loadingStateString = useFeedStateString(subplebbitAddresses) || t('loading');
|
||||||
const loadingString = <div className={styles.stateString}>{state === 'failed' ? state : <LoadingEllipsis string={loadingStateString} />}</div>;
|
const loadingString = <div className={styles.stateString}>{state === 'failed' ? state : <LoadingEllipsis string={loadingStateString} />}</div>;
|
||||||
|
|
||||||
@@ -78,34 +100,10 @@ const Catalog = () => {
|
|||||||
return <div className={styles.footer}>{footerContent}</div>;
|
return <div className={styles.footer}>{footerContent}</div>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isFeedloaded = feed.length > 0 || state === 'failed';
|
||||||
|
|
||||||
// split feed into rows
|
// split feed into rows
|
||||||
const includeDescription = !!description && description.length > 0;
|
const rows = useFeedRows(columnCount, feed, isFeedloaded, subplebbit);
|
||||||
const includeRules = !!rules && rules.length > 0;
|
|
||||||
const rows = useFeedRows(feed, columnCount, includeDescription, includeRules);
|
|
||||||
|
|
||||||
const descriptionPost = {
|
|
||||||
isDescription: true,
|
|
||||||
subplebbitAddress,
|
|
||||||
timestamp: createdAt,
|
|
||||||
author: { displayName: '## Board Mods' },
|
|
||||||
content: description,
|
|
||||||
link: avatarUrl,
|
|
||||||
title: 'Welcome to ' + (title || `p/${shortAddress}`),
|
|
||||||
pinned: true,
|
|
||||||
locked: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const content = rules?.map((rule: string, index: number) => `${index + 1}. ${rule}`).join('\n');
|
|
||||||
const rulesPost = {
|
|
||||||
isRules: true,
|
|
||||||
subplebbitAddress,
|
|
||||||
timestamp: createdAt,
|
|
||||||
author: { displayName: '## Board Mods' },
|
|
||||||
content,
|
|
||||||
title: 'Rules',
|
|
||||||
pinned: true,
|
|
||||||
locked: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
// save the last Virtuoso state to restore it when navigating back
|
// save the last Virtuoso state to restore it when navigating back
|
||||||
const virtuosoRef = useRef<VirtuosoHandle | null>(null);
|
const virtuosoRef = useRef<VirtuosoHandle | null>(null);
|
||||||
@@ -135,9 +133,7 @@ const Catalog = () => {
|
|||||||
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
||||||
totalCount={rows?.length || 0}
|
totalCount={rows?.length || 0}
|
||||||
data={rows}
|
data={rows}
|
||||||
itemContent={(index, row) => (
|
itemContent={(index, row) => <CatalogRow index={index} row={row} />}
|
||||||
<CatalogRow description={index === 0 && includeDescription && descriptionPost} index={index} row={row} rules={index === 0 && includeRules && rulesPost} />
|
|
||||||
)}
|
|
||||||
useWindowScroll={true}
|
useWindowScroll={true}
|
||||||
components={{ Footer }}
|
components={{ Footer }}
|
||||||
endReached={loadMore}
|
endReached={loadMore}
|
||||||
|
|||||||
Reference in New Issue
Block a user