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
28
pulls
pipelines
add subscriptions to profiles
awarm.space
3 months ago
d3672135
be08f13f
+144
-7
4 changed files
expand all
collapse all
unified
split
app
(home-pages)
reader
SubscriptionsContent.tsx
getSubscriptions.ts
p
[didOrHandle]
(profile)
subscriptions
SubscriptionsContent.tsx
page.tsx
+1
-1
app/(home-pages)/reader/SubscriptionsContent.tsx
···
32
32
33
33
const { data, error, size, setSize, isValidating } = useSWRInfinite(
34
34
getKey,
35
35
-
([_, cursor]) => getSubscriptions(cursor),
35
35
+
([_, cursor]) => getSubscriptions(null, cursor),
36
36
{
37
37
fallbackData: [
38
38
{ subscriptions: props.publications, nextCursor: props.nextCursor },
+13
-4
app/(home-pages)/reader/getSubscriptions.ts
···
8
8
import { idResolver } from "./idResolver";
9
9
import { Cursor } from "./getReaderFeed";
10
10
11
11
-
export async function getSubscriptions(cursor?: Cursor | null): Promise<{
11
11
+
export async function getSubscriptions(
12
12
+
did?: string | null,
13
13
+
cursor?: Cursor | null,
14
14
+
): Promise<{
12
15
nextCursor: null | Cursor;
13
16
subscriptions: PublicationSubscription[];
14
17
}> {
15
15
-
let auth_res = await getIdentityData();
16
16
-
if (!auth_res?.atp_did) return { subscriptions: [], nextCursor: null };
18
18
+
// If no DID provided, use logged-in user's DID
19
19
+
let identity = did;
20
20
+
if (!identity) {
21
21
+
const auth_res = await getIdentityData();
22
22
+
if (!auth_res?.atp_did) return { subscriptions: [], nextCursor: null };
23
23
+
identity = auth_res.atp_did;
24
24
+
}
25
25
+
17
26
let query = supabaseServerClient
18
27
.from("publication_subscriptions")
19
28
.select(`*, publications(*, documents_in_publications(*, documents(*)))`)
···
25
34
})
26
35
.limit(1, { referencedTable: "publications.documents_in_publications" })
27
36
.limit(25)
28
28
-
.eq("identity", auth_res.atp_did);
37
37
+
.eq("identity", identity);
29
38
30
39
if (cursor) {
31
40
query = query.or(
+103
app/p/[didOrHandle]/(profile)/subscriptions/SubscriptionsContent.tsx
···
1
1
+
"use client";
2
2
+
3
3
+
import { useEffect, useRef } from "react";
4
4
+
import useSWRInfinite from "swr/infinite";
5
5
+
import { PubListing } from "app/(home-pages)/discover/PubListing";
6
6
+
import {
7
7
+
getSubscriptions,
8
8
+
type PublicationSubscription,
9
9
+
} from "app/(home-pages)/reader/getSubscriptions";
10
10
+
import { Cursor } from "app/(home-pages)/reader/getReaderFeed";
11
11
+
12
12
+
export const ProfileSubscriptionsContent = (props: {
13
13
+
did: string;
14
14
+
subscriptions: PublicationSubscription[];
15
15
+
nextCursor: Cursor | null;
16
16
+
}) => {
17
17
+
const getKey = (
18
18
+
pageIndex: number,
19
19
+
previousPageData: {
20
20
+
subscriptions: PublicationSubscription[];
21
21
+
nextCursor: Cursor | null;
22
22
+
} | null,
23
23
+
) => {
24
24
+
// Reached the end
25
25
+
if (previousPageData && !previousPageData.nextCursor) return null;
26
26
+
27
27
+
// First page, we don't have previousPageData
28
28
+
if (pageIndex === 0)
29
29
+
return ["profile-subscriptions", props.did, null] as const;
30
30
+
31
31
+
// Add the cursor to the key
32
32
+
return [
33
33
+
"profile-subscriptions",
34
34
+
props.did,
35
35
+
previousPageData?.nextCursor,
36
36
+
] as const;
37
37
+
};
38
38
+
39
39
+
const { data, size, setSize, isValidating } = useSWRInfinite(
40
40
+
getKey,
41
41
+
([_, did, cursor]) => getSubscriptions(did, cursor),
42
42
+
{
43
43
+
fallbackData: [
44
44
+
{ subscriptions: props.subscriptions, nextCursor: props.nextCursor },
45
45
+
],
46
46
+
revalidateFirstPage: false,
47
47
+
},
48
48
+
);
49
49
+
50
50
+
const loadMoreRef = useRef<HTMLDivElement>(null);
51
51
+
52
52
+
// Set up intersection observer to load more when trigger element is visible
53
53
+
useEffect(() => {
54
54
+
const observer = new IntersectionObserver(
55
55
+
(entries) => {
56
56
+
if (entries[0].isIntersecting && !isValidating) {
57
57
+
const hasMore = data && data[data.length - 1]?.nextCursor;
58
58
+
if (hasMore) {
59
59
+
setSize(size + 1);
60
60
+
}
61
61
+
}
62
62
+
},
63
63
+
{ threshold: 0.1 },
64
64
+
);
65
65
+
66
66
+
if (loadMoreRef.current) {
67
67
+
observer.observe(loadMoreRef.current);
68
68
+
}
69
69
+
70
70
+
return () => observer.disconnect();
71
71
+
}, [data, size, setSize, isValidating]);
72
72
+
73
73
+
const allSubscriptions = data
74
74
+
? data.flatMap((page) => page.subscriptions)
75
75
+
: [];
76
76
+
77
77
+
if (allSubscriptions.length === 0 && !isValidating) {
78
78
+
return (
79
79
+
<div className="text-tertiary text-center py-4">No subscriptions yet</div>
80
80
+
);
81
81
+
}
82
82
+
83
83
+
return (
84
84
+
<div className="relative">
85
85
+
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3 gap-3">
86
86
+
{allSubscriptions.map((sub) => (
87
87
+
<PubListing key={sub.uri} {...sub} />
88
88
+
))}
89
89
+
</div>
90
90
+
{/* Trigger element for loading more subscriptions */}
91
91
+
<div
92
92
+
ref={loadMoreRef}
93
93
+
className="absolute bottom-96 left-0 w-full h-px pointer-events-none"
94
94
+
aria-hidden="true"
95
95
+
/>
96
96
+
{isValidating && (
97
97
+
<div className="text-center text-tertiary py-4">
98
98
+
Loading more subscriptions...
99
99
+
</div>
100
100
+
)}
101
101
+
</div>
102
102
+
);
103
103
+
};
+27
-2
app/p/[didOrHandle]/(profile)/subscriptions/page.tsx
···
1
1
-
export default function ProfileSubscriptionsPage() {
2
2
-
return <div>subscriptions here!</div>;
1
1
+
import { idResolver } from "app/(home-pages)/reader/idResolver";
2
2
+
import { getSubscriptions } from "app/(home-pages)/reader/getSubscriptions";
3
3
+
import { ProfileSubscriptionsContent } from "./SubscriptionsContent";
4
4
+
5
5
+
export default async function ProfileSubscriptionsPage(props: {
6
6
+
params: Promise<{ didOrHandle: string }>;
7
7
+
}) {
8
8
+
const params = await props.params;
9
9
+
const didOrHandle = decodeURIComponent(params.didOrHandle);
10
10
+
11
11
+
// Resolve handle to DID if necessary
12
12
+
let did = didOrHandle;
13
13
+
if (!didOrHandle.startsWith("did:")) {
14
14
+
const resolved = await idResolver.handle.resolve(didOrHandle);
15
15
+
if (!resolved) return null;
16
16
+
did = resolved;
17
17
+
}
18
18
+
19
19
+
const { subscriptions, nextCursor } = await getSubscriptions(did);
20
20
+
21
21
+
return (
22
22
+
<ProfileSubscriptionsContent
23
23
+
did={did}
24
24
+
subscriptions={subscriptions}
25
25
+
nextCursor={nextCursor}
26
26
+
/>
27
27
+
);
3
28
}