feat(subplebbit): add subplebbit feed, enable links to it

This commit is contained in:
plebeius.eth
2023-11-13 17:58:39 +01:00
parent fa63ff1bfe
commit c48cd07d93
8 changed files with 68 additions and 18 deletions

View File

@@ -60,12 +60,7 @@ const Header = () => {
}
const subplebbitTitle = (
<Link
to={`/p/${params.subplebbitAddress}`}
onClick={(e) => {
e.preventDefault();
}}
>
<Link to={`/p/${params.subplebbitAddress}`}>
{title || shortAddress}
</Link>
);

View File

@@ -117,7 +117,7 @@ const Post = ({ post, index }: PostProps) => {
u/{postAuthor}
</Link>
 {t('post_to')}
<Link className={styles.subplebbit} to={`p/${subplebbitAddress}`} onClick={(e) => e.preventDefault()}>
<Link className={styles.subplebbit} to={`/p/${subplebbitAddress}`}>
{' '}
p/{subplebbit?.shortAddress}
</Link>

View File

@@ -42,7 +42,7 @@ const TopBar = () => {
</div>
<div className={styles.dropChoices} style={{ display: isClicked && subscriptions?.length ? 'block' : 'none' }}>
{subscriptions?.map((subscription: string, index: number) => (
<Link key={index} to={`p/${subscription}`} className={styles.choice} onClick={(event) => event.preventDefault()}>
<Link key={index} to={`/p/${subscription}`} className={styles.choice}>
{subscription}
</Link>
))}
@@ -68,7 +68,7 @@ const TopBar = () => {
{ethFilteredAddresses?.map((address: string, index: number) => (
<li key={index}>
{index !== 0 && <span className={styles.separator}>-</span>}
<Link to={`p/${address}`} className={styles.choice} onClick={(event) => event.preventDefault()}>
<Link to={`/p/${address}`} className={styles.choice}>
{address.slice(0, -4)}
</Link>
</li>

View File

@@ -2,11 +2,11 @@ import { useEffect, useRef } from 'react';
import { useParams } from 'react-router-dom';
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
import { useFeed } from '@plebbit/plebbit-react-hooks';
import useDefaultSubplebbitAddresses from '../../hooks/use-default-subplebbit-addresses';
import { useTranslation } from 'react-i18next';
import styles from './home.module.css';
import Post from '../../components/post';
import useDefaultSubplebbitAddresses from '../../hooks/use-default-subplebbit-addresses';
import useFeedStateString from '../../hooks/use-feed-state-string';
import { useTranslation } from 'react-i18next';
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};

View File

@@ -1,4 +1,4 @@
.post {
.content {
padding: 7px 5px 0px 5px;
margin-bottom: 25px;
}

View File

@@ -71,7 +71,7 @@ const Post = () => {
}, []);
return (
<div className={styles.post}>
<div className={styles.content}>
<PostComponent post={comment} />
<div className={styles.replyArea}>
<div className={styles.repliesTitle}>

View File

@@ -0,0 +1,3 @@
.content {
padding: 7px 5px 0px 5px;
}

View File

@@ -1,17 +1,69 @@
import { useEffect, useMemo, useRef } from "react";
import { useParams } from "react-router-dom";
import { useSubplebbit } from "@plebbit/plebbit-react-hooks";
import { useFeed, useSubplebbit } from "@plebbit/plebbit-react-hooks";
import { Virtuoso, VirtuosoHandle, StateSnapshot } from "react-virtuoso";
import { useTranslation } from "react-i18next";
import styles from "./subplebbit.module.css";
import Post from "../../components/post/post";
import useFeedStateString from "../../hooks/use-feed-state-string";
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
const NoPosts = () => 'no posts';
const Subplebbit = () => {
const { t } = useTranslation();
const params = useParams();
const subplebbitAddress = params.subplebbitAddress;
const subplebbitAddresses = useMemo(() => [subplebbitAddress], [subplebbitAddress]) as string[];
const subplebbit = useSubplebbit({subplebbitAddress});
const { title, description, shortAddress } = subplebbit || {};
const { title, shortAddress } = subplebbit || {};
const { feed, hasMore, loadMore } = useFeed({ subplebbitAddresses, sortType: 'hot' });
const loadingStateString = useFeedStateString(subplebbitAddresses) || t('loading');
useEffect(() => {
document.title = (title ? title : shortAddress) + " - seedit";
}, [title, shortAddress]);
let Footer;
if (feed?.length === 0) {
Footer = NoPosts;
}
if (hasMore || subplebbitAddresses.length === 0) {
Footer = () => loadingStateString;
}
const virtuosoRef = useRef<VirtuosoHandle | null>(null);
useEffect(() => {
const setLastVirtuosoState = () => {
virtuosoRef.current?.getState((snapshot: StateSnapshot) => {
if (snapshot?.ranges?.length) {
lastVirtuosoStates[`${subplebbitAddress}`] = snapshot;
}
});
};
window.addEventListener('scroll', setLastVirtuosoState);
return () => window.removeEventListener('scroll', setLastVirtuosoState);
}, [subplebbitAddress]);
const lastVirtuosoState = lastVirtuosoStates[`${subplebbitAddress}`];
return (
<>
{title || shortAddress}
</>
<div className={styles.content}>
<Virtuoso
increaseViewportBy={{ bottom: 1200, top: 600 }}
totalCount={feed?.length || 0}
data={feed}
itemContent={(index, post) => <Post index={index} post={post} />}
useWindowScroll={true}
components={{ Footer }}
endReached={loadMore}
ref={virtuosoRef}
restoreStateFrom={lastVirtuosoState}
initialScrollTop={lastVirtuosoState?.scrollTop}
/>
</div>
);
}