-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: React Suspense support for useFeed #940
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,113 @@ | ||
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> | ||
* ); | ||
* ``` | ||
* | ||
* @experimental This API can change without notice | ||
* @category Discovery | ||
* @group Hooks | ||
* @param args - {@link UseSuspenseFeedArgs} | ||
*/ | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simpler is better.