diff --git a/src/components/board-nav/board-nav.tsx b/src/components/board-nav/board-nav.tsx index df5920df..8e26b59b 100644 --- a/src/components/board-nav/board-nav.tsx +++ b/src/components/board-nav/board-nav.tsx @@ -1,3 +1,4 @@ +import { useMemo } from 'react'; import { Link, useLocation, useNavigate, useParams } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Subplebbit } from '@plebbit/plebbit-react-hooks'; @@ -13,11 +14,25 @@ const BoardNavDesktop = ({ subplebbits }: BoardNavProps) => { const { t } = useTranslation(); const isInCatalogView = isCatalogView(useLocation().pathname, useParams()); + const sortedSubplebbits = useMemo(() => { + const oneHourAgo = Date.now() - 60 * 60 * 1000; + const retainedSubplebbits: (Subplebbit | undefined)[] = []; + + subplebbits.forEach((sub: Subplebbit | undefined) => { + if (!sub || sub.updatedAt === undefined || sub.updatedAt * 1000 > oneHourAgo) { + // retain the position of loading, undefined, or online subplebbits + retainedSubplebbits.push(sub); + } + // don't include offline subs + }); + return retainedSubplebbits; + }, [subplebbits]); + return (
[ - {subplebbits.map((subplebbit: any, index: number) => { + {sortedSubplebbits.map((subplebbit: any, index: number) => { const address = subplebbit?.address || ''; const title = subplebbit?.title || ''; return ( @@ -26,7 +41,7 @@ const BoardNavDesktop = ({ subplebbits }: BoardNavProps) => { {address.includes('.') ? address : title || address.slice(0, 10).concat('...')} - {index !== subplebbits.length - 1 ? ' /' : null} + {index !== sortedSubplebbits.length - 1 ? ' /' : null} ); })} @@ -46,6 +61,20 @@ const BoardNavMobile = ({ subplebbits, currentSubplebbit }: BoardNavProps) => { const currentSubplebbitIsInList = subplebbits.some((subplebbit: any) => subplebbit?.address === currentSubplebbit); + const sortedSubplebbits = useMemo(() => { + const oneHourAgo = Date.now() - 60 * 60 * 1000; + const retainedSubplebbits: (Subplebbit | undefined)[] = []; + + subplebbits.forEach((sub: Subplebbit | undefined) => { + if (!sub || sub.updatedAt === undefined || sub.updatedAt * 1000 > oneHourAgo) { + // retain the position of loading, undefined, or online subplebbits + retainedSubplebbits.push(sub); + } + // don't include offline subs + }); + return retainedSubplebbits; + }, [subplebbits]); + const isInCatalogView = isCatalogView(useLocation().pathname, useParams()); const boardSelect = ( @@ -53,7 +82,7 @@ const BoardNavMobile = ({ subplebbits, currentSubplebbit }: BoardNavProps) => { {!currentSubplebbitIsInList && currentSubplebbit && } - {subplebbits.map((subplebbit: any, index: number) => { + {sortedSubplebbits.map((subplebbit: any, index: number) => { const address = subplebbit?.address || ''; const title = subplebbit?.title || ''; const subplebbitAddress = address?.includes('.') ? address : title || address?.slice(0, 10).concat('...');