changes
This commit is contained in:
parent
be90a09ce4
commit
3c7c2a6957
6 changed files with 30 additions and 33 deletions
|
@ -1,11 +1,13 @@
|
||||||
import { Post } from '../model/posts/posts.ts'
|
import { Post } from '../model/posts/posts.ts'
|
||||||
import { Link } from 'react-router'
|
import { Link } from 'react-router'
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
interface PostItemProps {
|
interface PostItemProps {
|
||||||
post: Post
|
post: Post
|
||||||
|
index: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function PostItem({ post }: PostItemProps) {
|
export default function PostItem({ post, index }: PostItemProps) {
|
||||||
const formattedDate = post.createdAt.toLocaleString('en-US', {
|
const formattedDate = post.createdAt.toLocaleString('en-US', {
|
||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
month: 'short',
|
month: 'short',
|
||||||
|
@ -14,8 +16,23 @@ export default function PostItem({ post }: PostItemProps) {
|
||||||
minute: '2-digit',
|
minute: '2-digit',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const [visible, setVisible] = useState(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const timeout = setTimeout(() => setVisible(true))
|
||||||
|
return () => clearTimeout(timeout)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const opacity = visible ? 'opacity-100' : 'opacity-0'
|
||||||
|
|
||||||
|
const delayMs = index * 100
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<article className="w-full p-4" key={post.postId}>
|
<article
|
||||||
|
className={`w-full p-4 ${opacity} transition-opacity duration-500`}
|
||||||
|
style={{ transitionDelay: `${delayMs}ms` }}
|
||||||
|
key={post.postId}
|
||||||
|
>
|
||||||
<div className="text-sm text-gray-500 mb-3">
|
<div className="text-sm text-gray-500 mb-3">
|
||||||
<Link to={`/u/${post.authorName}`} className="text-gray-400 hover:underline mr-2">
|
<Link to={`/u/${post.authorName}`} className="text-gray-400 hover:underline mr-2">
|
||||||
@{post.authorName}
|
@{post.authorName}
|
||||||
|
|
|
@ -2,14 +2,18 @@ import { Post } from '../model/posts/posts.ts'
|
||||||
import PostItem from './PostItem'
|
import PostItem from './PostItem'
|
||||||
|
|
||||||
interface PostsFeedProps {
|
interface PostsFeedProps {
|
||||||
posts: Post[]
|
pages: Post[][]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function PostsList({ posts }: PostsFeedProps) {
|
export default function PostsList({ pages }: PostsFeedProps) {
|
||||||
|
return <div className="flex flex-col gap-6 w-full">{pages.map(renderPage)}</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderPage(posts: Post[]) {
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-6 w-full">
|
<div className="flex flex-col gap-6 w-full">
|
||||||
{posts.map((post) => (
|
{posts.map((post, idx) => (
|
||||||
<PostItem key={post.postId} post={post} />
|
<PostItem key={post.postId} post={post} index={idx} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,7 +2,6 @@ import { useCallback } from 'react'
|
||||||
import { useParams } from 'react-router'
|
import { useParams } from 'react-router'
|
||||||
import { loadPostsForAuthor } from '../api/api.ts'
|
import { loadPostsForAuthor } from '../api/api.ts'
|
||||||
import { Post } from '../model/posts/posts.ts'
|
import { Post } from '../model/posts/posts.ts'
|
||||||
import './FeedView.css'
|
|
||||||
import FeedView from './FeedView.tsx'
|
import FeedView from './FeedView.tsx'
|
||||||
|
|
||||||
export default function AuthorPage() {
|
export default function AuthorPage() {
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
@media (width >= 48rem) {
|
|
||||||
main {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 1.618fr 1fr;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { Post } from '../model/posts/posts.ts'
|
import { Post } from '../model/posts/posts.ts'
|
||||||
import PostsList from '../components/PostsList.tsx'
|
import PostsList from '../components/PostsList.tsx'
|
||||||
import { useCallback, useRef, useState } from 'react'
|
import { useCallback, useRef, useState } from 'react'
|
||||||
import './FeedView.css'
|
|
||||||
import { useIntersectionLoad } from '../hooks/useIntersectionLoad.ts'
|
import { useIntersectionLoad } from '../hooks/useIntersectionLoad.ts'
|
||||||
|
|
||||||
const PageSize = 20
|
const PageSize = 20
|
||||||
|
@ -12,10 +11,7 @@ interface FeedViewProps {
|
||||||
|
|
||||||
export default function FeedView({ loadPosts }: FeedViewProps) {
|
export default function FeedView({ loadPosts }: FeedViewProps) {
|
||||||
const [pages, setPages] = useState<Post[][]>([])
|
const [pages, setPages] = useState<Post[][]>([])
|
||||||
const posts = pages.flat()
|
|
||||||
|
|
||||||
const [hasMore, setHasMore] = useState(true)
|
const [hasMore, setHasMore] = useState(true)
|
||||||
const [isLoading, setIsLoading] = useState(false)
|
|
||||||
|
|
||||||
const cursor = useRef<string | null>(null)
|
const cursor = useRef<string | null>(null)
|
||||||
const loading = useRef(false)
|
const loading = useRef(false)
|
||||||
|
@ -24,7 +20,6 @@ export default function FeedView({ loadPosts }: FeedViewProps) {
|
||||||
const loadNextPage = useCallback(async () => {
|
const loadNextPage = useCallback(async () => {
|
||||||
if (loading.current || !hasMore) return
|
if (loading.current || !hasMore) return
|
||||||
loading.current = true
|
loading.current = true
|
||||||
setIsLoading(true)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const delay = new Promise((resolve) => setTimeout(resolve, 500))
|
const delay = new Promise((resolve) => setTimeout(resolve, 500))
|
||||||
|
@ -35,27 +30,16 @@ export default function FeedView({ loadPosts }: FeedViewProps) {
|
||||||
setPages((prev) => [...prev, page])
|
setPages((prev) => [...prev, page])
|
||||||
} finally {
|
} finally {
|
||||||
loading.current = false
|
loading.current = false
|
||||||
setIsLoading(false)
|
|
||||||
}
|
}
|
||||||
}, [loadPosts, hasMore])
|
}, [loadPosts, hasMore])
|
||||||
|
|
||||||
useIntersectionLoad(loadNextPage, sentinelRef)
|
useIntersectionLoad(loadNextPage, sentinelRef)
|
||||||
return (
|
return (
|
||||||
<main className="w-full max-w-full px-12 py-6">
|
<main className="w-full flex justify-center">
|
||||||
<div className="col-start-1">
|
<div className="max-w-3xl">
|
||||||
<PostsList posts={posts} />
|
<PostsList pages={pages} />
|
||||||
{isLoading && (
|
|
||||||
<div className="w-full flex justify-center py-4">
|
|
||||||
<Spinner />
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div ref={sentinelRef} className="h-1" />
|
<div ref={sentinelRef} className="h-1" />
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spinner component
|
|
||||||
const Spinner = () => (
|
|
||||||
<div className="animate-spin h-5 w-5 border-2 border-gray-500 rounded-full border-t-transparent"></div>
|
|
||||||
)
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { Post } from '../model/posts/posts.ts'
|
import { Post } from '../model/posts/posts.ts'
|
||||||
import './FeedView.css'
|
|
||||||
import { loadPublicFeed } from '../api/api.ts'
|
import { loadPublicFeed } from '../api/api.ts'
|
||||||
import FeedView from './FeedView.tsx'
|
import FeedView from './FeedView.tsx'
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue