mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {
2 AppBskyActorDefs,
3 AppBskyEmbedRecord,
4 AppBskyEmbedRecordWithMedia,
5 AppBskyFeedDefs,
6 AppBskyFeedPost,
7 AtUri,
8} from '@atproto/api'
9import {InfiniteData, QueryClient, QueryKey} from '@tanstack/react-query'
10
11export function truncateAndInvalidate<T = any>(
12 queryClient: QueryClient,
13 queryKey: QueryKey,
14) {
15 queryClient.setQueriesData<InfiniteData<T>>({queryKey}, data => {
16 if (data) {
17 return {
18 pageParams: data.pageParams.slice(0, 1),
19 pages: data.pages.slice(0, 1),
20 }
21 }
22 return data
23 })
24 queryClient.invalidateQueries({queryKey})
25}
26
27// Given an AtUri, this function will check if the AtUri matches a
28// hit regardless of whether the AtUri uses a DID or handle as a host.
29//
30// AtUri should be the URI that is being searched for, while currentUri
31// is the URI that is being checked. currentAuthor is the author
32// of the currentUri that is being checked.
33export function didOrHandleUriMatches(
34 atUri: AtUri,
35 record: {uri: string; author: AppBskyActorDefs.ProfileViewBasic},
36) {
37 if (atUri.host.startsWith('did:')) {
38 return atUri.href === record.uri
39 }
40
41 return atUri.host === record.author.handle && record.uri.endsWith(atUri.rkey)
42}
43
44export function getEmbeddedPost(
45 v: unknown,
46): AppBskyEmbedRecord.ViewRecord | undefined {
47 if (AppBskyEmbedRecord.isView(v)) {
48 if (
49 AppBskyEmbedRecord.isViewRecord(v.record) &&
50 AppBskyFeedPost.isRecord(v.record.value)
51 ) {
52 return v.record
53 }
54 }
55 if (AppBskyEmbedRecordWithMedia.isView(v)) {
56 if (
57 AppBskyEmbedRecord.isViewRecord(v.record.record) &&
58 AppBskyFeedPost.isRecord(v.record.record.value)
59 ) {
60 return v.record.record
61 }
62 }
63}
64
65export function embedViewRecordToPostView(
66 v: AppBskyEmbedRecord.ViewRecord,
67): AppBskyFeedDefs.PostView {
68 return {
69 uri: v.uri,
70 cid: v.cid,
71 author: v.author,
72 record: v.value,
73 indexedAt: v.indexedAt,
74 labels: v.labels,
75 embed: v.embeds?.[0],
76 likeCount: v.likeCount,
77 quoteCount: v.quoteCount,
78 replyCount: v.replyCount,
79 repostCount: v.repostCount,
80 }
81}