-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: React Suspense support for useFeed
- Loading branch information
1 parent
21f652d
commit 5b5b0ff
Showing
11 changed files
with
169 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@lens-protocol/react": patch | ||
"@lens-protocol/react-native": patch | ||
"@lens-protocol/react-web": patch | ||
--- | ||
|
||
**feat:** add React Suspense support to `useFeed` hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,112 @@ | ||
import { FeedItem, FeedRequest, useFeed as useBaseFeedQuery } from '@lens-protocol/api-bindings'; | ||
import { FeedDocument, FeedItem, FeedRequest, FeedWhere } from '@lens-protocol/api-bindings'; | ||
|
||
import { SessionType, useSession } from '../authentication'; | ||
import { SessionType, UseSessionArgs, useSession } from '../authentication'; | ||
import { useLensApolloClient } from '../helpers/arguments'; | ||
import { PaginatedArgs, PaginatedReadResult, usePaginatedReadResult } from '../helpers/reads'; | ||
import { PaginatedArgs, PaginatedReadResult } from '../helpers/reads'; | ||
import { | ||
SuspendablePaginatedResult, | ||
SuspenseEnabled, | ||
SuspensePaginatedResult, | ||
useSuspendablePaginatedQuery, | ||
} from '../helpers/suspense'; | ||
import { useFragmentVariables } from '../helpers/variables'; | ||
|
||
/** | ||
* {@link useFeed} hook arguments | ||
*/ | ||
export type UseFeedArgs = PaginatedArgs<FeedRequest>; | ||
|
||
export type { FeedRequest, FeedWhere }; | ||
|
||
/** | ||
* {@link useFeed} hook arguments with Suspense support | ||
* | ||
* @experimental This API can change without notice | ||
*/ | ||
export type UseSuspenseFeedArgs = SuspenseEnabled<UseFeedArgs>; | ||
|
||
/** | ||
* Fetch a the feed of a given profile and filters. | ||
* | ||
* You MUST be authenticated via {@link useLogin} to use this hook. | ||
* | ||
* @example | ||
* ```tsx | ||
* const { data, loading, error } = useFeed({ | ||
* where: { | ||
* for: '0x01`, // profileId | ||
* }, | ||
* }); | ||
* | ||
* if (loading) return <div>Loading...</div>; | ||
* | ||
* if (error) return <div>Error: {error.message}</div>; | ||
* | ||
* return ( | ||
* <ul> | ||
* {data.map((item, idx) => ( | ||
* <li key={`${item.root.id}-${idx}`}> | ||
* // render item details | ||
* </li> | ||
* ))} | ||
* </ul> | ||
* ); | ||
* ``` | ||
* | ||
* @category Discovery | ||
* @group Hooks | ||
* @param args - {@link UseFeedArgs} | ||
*/ | ||
export function useFeed({ where }: UseFeedArgs): PaginatedReadResult<FeedItem[]>; | ||
|
||
/** | ||
* Fetch a the feed of a given profile and filters. | ||
* | ||
* You MUST be authenticated via {@link useLogin} to use this hook. | ||
* | ||
* This signature supports [React Suspense](https://react.dev/reference/react/Suspense). | ||
* | ||
* @example | ||
* ```tsx | ||
* import { useFeed, ProfileId } from '@lens-protocol/react'; | ||
* | ||
* function Feed({ profileId }: { profileId: ProfileId }) { | ||
* const { data, loading, error } = useFeed({ | ||
* where: { | ||
* for: profileId, | ||
* }, | ||
* }); | ||
* | ||
* if (loading) return <div>Loading...</div>; | ||
* | ||
* if (error) return <div>Error: {error.message}</div>; | ||
* | ||
* return ( | ||
* <ul> | ||
* {data.map((item, idx) => ( | ||
* <li key={`${item.root.id}-${idx}`}> | ||
* // render item details | ||
* </li> | ||
* ))} | ||
* </ul> | ||
* ); | ||
* } | ||
* const { data, loading, error } = useFeed({ | ||
* where: { | ||
* for: '0x01`, // profileId | ||
* }, | ||
* suspense: true, | ||
* }); | ||
* | ||
* return ( | ||
* <ul> | ||
* {data.map((item, idx) => ( | ||
* <li key={`${item.root.id}-${idx}`}> | ||
* // render item details | ||
* </li> | ||
* ))} | ||
* </ul> | ||
* ); | ||
* ``` | ||
* | ||
* @category Discovery | ||
* @group Hooks | ||
* @param args - {@link UseFeedArgs} | ||
*/ | ||
export function useFeed({ where }: UseFeedArgs): PaginatedReadResult<FeedItem[]> { | ||
const { data: session } = useSession(); | ||
|
||
return usePaginatedReadResult( | ||
useBaseFeedQuery( | ||
useLensApolloClient({ | ||
variables: useFragmentVariables({ | ||
where, | ||
statsFor: where?.metadata?.publishedOn, | ||
}), | ||
skip: session?.type !== SessionType.WithProfile, | ||
export function useFeed({ where }: UseSuspenseFeedArgs): SuspensePaginatedResult<FeedItem[]>; | ||
|
||
export function useFeed({ | ||
suspense = false, | ||
where, | ||
}: UseFeedArgs & { suspense?: boolean }): SuspendablePaginatedResult<FeedItem[]> { | ||
const { data: session } = useSession({ suspense } as UseSessionArgs); | ||
|
||
return useSuspendablePaginatedQuery({ | ||
suspense, | ||
query: FeedDocument, | ||
options: useLensApolloClient({ | ||
variables: useFragmentVariables({ | ||
where, | ||
statsFor: where?.metadata?.publishedOn, | ||
}), | ||
), | ||
); | ||
skip: session.type !== SessionType.WithProfile, | ||
}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.