tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
29
pulls
pipelines
oops forgot the new files
cozylittle.house
1 month ago
acbb9604
cbae01a9
+118
3 changed files
expand all
collapse all
unified
split
app
(home-pages)
reader
GlobalContent.tsx
InboxContent.tsx
LocalContent.tsx
+9
app/(home-pages)/reader/GlobalContent.tsx
···
1
1
+
"use client";
2
2
+
3
3
+
export const GlobalContent = () => {
4
4
+
return (
5
5
+
<div className="flex flex-col gap-2 container bg-[rgba(var(--bg-page),.7)] sm:p-4 p-3 justify-between text-center text-tertiary">
6
6
+
Nothing here yet…
7
7
+
</div>
8
8
+
);
9
9
+
};
+100
app/(home-pages)/reader/InboxContent.tsx
···
1
1
+
"use client";
2
2
+
import { ButtonPrimary } from "components/Buttons";
3
3
+
import { DiscoverSmall } from "components/Icons/DiscoverSmall";
4
4
+
import type { Cursor, Post } from "./getReaderFeed";
5
5
+
import useSWRInfinite from "swr/infinite";
6
6
+
import { getReaderFeed } from "./getReaderFeed";
7
7
+
import { useEffect, useRef } from "react";
8
8
+
import Link from "next/link";
9
9
+
import { PostListing } from "components/PostListing";
10
10
+
11
11
+
export const InboxContent = (props: {
12
12
+
posts: Post[];
13
13
+
nextCursor: Cursor | null;
14
14
+
}) => {
15
15
+
const getKey = (
16
16
+
pageIndex: number,
17
17
+
previousPageData: {
18
18
+
posts: Post[];
19
19
+
nextCursor: Cursor | null;
20
20
+
} | null,
21
21
+
) => {
22
22
+
// Reached the end
23
23
+
if (previousPageData && !previousPageData.nextCursor) return null;
24
24
+
25
25
+
// First page, we don't have previousPageData
26
26
+
if (pageIndex === 0) return ["reader-feed", null] as const;
27
27
+
28
28
+
// Add the cursor to the key
29
29
+
return ["reader-feed", previousPageData?.nextCursor] as const;
30
30
+
};
31
31
+
32
32
+
const { data, size, setSize, isValidating } = useSWRInfinite(
33
33
+
getKey,
34
34
+
([_, cursor]) => getReaderFeed(cursor),
35
35
+
{
36
36
+
fallbackData: [{ posts: props.posts, nextCursor: props.nextCursor }],
37
37
+
revalidateFirstPage: false,
38
38
+
},
39
39
+
);
40
40
+
41
41
+
const loadMoreRef = useRef<HTMLDivElement>(null);
42
42
+
43
43
+
// Set up intersection observer to load more when trigger element is visible
44
44
+
useEffect(() => {
45
45
+
const observer = new IntersectionObserver(
46
46
+
(entries) => {
47
47
+
if (entries[0].isIntersecting && !isValidating) {
48
48
+
const hasMore = data && data[data.length - 1]?.nextCursor;
49
49
+
if (hasMore) {
50
50
+
setSize(size + 1);
51
51
+
}
52
52
+
}
53
53
+
},
54
54
+
{ threshold: 0.1 },
55
55
+
);
56
56
+
57
57
+
if (loadMoreRef.current) {
58
58
+
observer.observe(loadMoreRef.current);
59
59
+
}
60
60
+
61
61
+
return () => observer.disconnect();
62
62
+
}, [data, size, setSize, isValidating]);
63
63
+
64
64
+
const allPosts = data ? data.flatMap((page) => page.posts) : [];
65
65
+
66
66
+
if (allPosts.length === 0 && !isValidating) return <ReaderEmpty />;
67
67
+
68
68
+
return (
69
69
+
<div className="flex flex-col gap-3 relative">
70
70
+
{allPosts.map((p) => (
71
71
+
<PostListing {...p} key={p.documents.uri} />
72
72
+
))}
73
73
+
{/* Trigger element for loading more posts */}
74
74
+
<div
75
75
+
ref={loadMoreRef}
76
76
+
className="absolute bottom-96 left-0 w-full h-px pointer-events-none"
77
77
+
aria-hidden="true"
78
78
+
/>
79
79
+
{isValidating && (
80
80
+
<div className="text-center text-tertiary py-4">
81
81
+
Loading more posts...
82
82
+
</div>
83
83
+
)}
84
84
+
</div>
85
85
+
);
86
86
+
};
87
87
+
88
88
+
export const ReaderEmpty = () => {
89
89
+
return (
90
90
+
<div className="flex flex-col gap-2 container bg-[rgba(var(--bg-page),.7)] sm:p-4 p-3 justify-between text-center text-tertiary">
91
91
+
Nothing to read yet… <br />
92
92
+
Subscribe to publications and find their posts here!
93
93
+
<Link href={"/discover"}>
94
94
+
<ButtonPrimary className="mx-auto place-self-center">
95
95
+
<DiscoverSmall /> Discover Publications
96
96
+
</ButtonPrimary>
97
97
+
</Link>
98
98
+
</div>
99
99
+
);
100
100
+
};
+9
app/(home-pages)/reader/LocalContent.tsx
···
1
1
+
"use client";
2
2
+
3
3
+
export const LocalContent = () => {
4
4
+
return (
5
5
+
<div className="flex flex-col gap-2 container bg-[rgba(var(--bg-page),.7)] sm:p-4 p-3 justify-between text-center text-tertiary">
6
6
+
Nothing here yet…
7
7
+
</div>
8
8
+
);
9
9
+
};