Skip to content

Commit

Permalink
feat: Suspense support to useExplorePublications, useExploreProfiles,…
Browse files Browse the repository at this point in the history
… useRecommendedProfiles hooks
  • Loading branch information
cesarenaldi committed Jun 13, 2024
1 parent 980e327 commit 697eca1
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 181 deletions.
7 changes: 7 additions & 0 deletions .changeset/seven-pugs-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@lens-protocol/react": minor
"@lens-protocol/react-native": minor
"@lens-protocol/react-web": minor
---

**feat:** add React Suspense support to `useExplorePublications`, `useExploreProfiles`, `useRecommendedProfiles` hooks
9 changes: 2 additions & 7 deletions examples/web/src/discovery/UseExploreProfiles.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { useExploreProfiles, ExploreProfilesOrderByType } from '@lens-protocol/react-web';

import { ErrorMessage } from '../components/error/ErrorMessage';
import { Loading } from '../components/loading/Loading';
import { useInfiniteScroll } from '../hooks/useInfiniteScroll';
import { ProfileCard } from '../profiles/components/ProfileCard';

export function UseExploreProfiles() {
const { data, error, loading, hasMore, observeRef } = useInfiniteScroll(
const { data, hasMore, observeRef } = useInfiniteScroll(
useExploreProfiles({
orderBy: ExploreProfilesOrderByType.LatestCreated,
suspense: true,
}),
);

if (loading) return <Loading />;

if (error) return <ErrorMessage error={error} />;

if (data.length === 0) return <p>No items</p>;

return (
Expand Down
9 changes: 1 addition & 8 deletions examples/web/src/discovery/UseExplorePublications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
} from '@lens-protocol/react-web';

import { PublicationCard } from '../components/cards';
import { ErrorMessage } from '../components/error/ErrorMessage';
import { Loading } from '../components/loading/Loading';
import { useInfiniteScroll } from '../hooks/useInfiniteScroll';
import { formatAmount, formatFiatAmount } from '../utils/formatAmount';

Expand Down Expand Up @@ -45,20 +43,15 @@ function PublicationCollectPolicy({ publication }: { publication: ExplorePublica
export function UseExplorePublications() {
const {
data: publications,
error,
loading,
hasMore,
observeRef,
} = useInfiniteScroll(
useExplorePublications({
orderBy: ExplorePublicationsOrderByType.Latest,
suspense: true,
}),
);

if (loading) return <Loading />;

if (error) return <ErrorMessage error={error} />;

return (
<div>
<h1>
Expand Down
34 changes: 12 additions & 22 deletions examples/web/src/discovery/UseRecommendedProfiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import {
} from '@lens-protocol/react-web';

import { RequireProfileSession } from '../components/auth';
import { ErrorMessage } from '../components/error/ErrorMessage';
import { Loading } from '../components/loading/Loading';
import { useInfiniteScroll } from '../hooks/useInfiniteScroll';
import { ProfileCard } from '../profiles/components/ProfileCard';

function UseRecommendedProfilesInner({ profileId }: { profileId: ProfileId }) {
const {
data: profiles,
error,
loading,
hasMore,
observeRef,
} = useInfiniteScroll(
useRecommendedProfiles({
for: profileId,
suspense: true,
}),
);

Expand All @@ -29,21 +26,20 @@ function UseRecommendedProfilesInner({ profileId }: { profileId: ProfileId }) {
void dismiss({ profileIds: [id] });
};

if (loading) return <Loading />;

if (error) return <ErrorMessage error={error} />;

return (
<div>
{profiles.map((p) => (
<div key={p.id}>
<ProfileCard profile={p}>
<h2>
<code>useRecommendedProfiles</code>
</h2>
<div>
{profiles.map((p) => (
<ProfileCard key={p.id} profile={p}>
<button onClick={() => dismissRecommendation(p.id)} disabled={dismissing}>
Dismiss recommendation
</button>
</ProfileCard>
</div>
))}
))}
</div>

{hasMore && <p ref={observeRef}>Loading more...</p>}
</div>
Expand All @@ -52,14 +48,8 @@ function UseRecommendedProfilesInner({ profileId }: { profileId: ProfileId }) {

export function UseRecommendedProfiles() {
return (
<div>
<h2>
<code>useRecommendedProfiles & useDismissRecommendedProfiles</code>
</h2>

<RequireProfileSession message="Log in to view this example.">
{({ profile }) => <UseRecommendedProfilesInner profileId={profile.id} />}
</RequireProfileSession>
</div>
<RequireProfileSession message="Log in to view this example.">
{({ profile }) => <UseRecommendedProfilesInner profileId={profile.id} />}
</RequireProfileSession>
);
}
2 changes: 1 addition & 1 deletion packages/react/src/authentication/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export type UseSessionArgs = SuspenseEnabled;
* function Page() {
* const { data, error, loading } = useSession();
*
* if (loading) return <p>Loading...</p>;
* if (loading) return <Loader />;
*
* if (error) return <p>Something went wrong.</p>;
*
Expand Down
112 changes: 76 additions & 36 deletions packages/react/src/discovery/useExploreProfiles.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,101 @@
import {
ExploreProfilesDocument,
ExploreProfilesOrderByType,
ExploreProfilesRequest,
ExploreProfilesWhere,
Profile,
useExploreProfiles as useBaseExploreProfilesQuery,
} from '@lens-protocol/api-bindings';

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 useExploreProfiles} hook arguments
*/
export type UseExploreProfilesArgs = PaginatedArgs<ExploreProfilesRequest>;

export type { ExploreProfilesRequest, ExploreProfilesWhere };

/**
* `useExploreProfiles` is a paginated hook that lets you discover new profiles based on a defined criteria
*
* @category Discovery
* @group Hooks
* @param args - {@link UseExploreProfilesArgs}
* {@link useExploreProfiles} hook arguments with Suspense support
*/
export type UseSuspenseExploreProfilesArgs = SuspenseEnabled<UseExploreProfilesArgs>;

/**
* Discover new profiles based on a defined criteria.
*
* @example
* Explore the latest created profiles
* ```tsx
* import { useExploreProfiles, ExploreProfilesOrderByType } from '@lens-protocol/react';
* const { data, error, loading } = useExploreProfiles({
* orderBy: ExploreProfilesOrderByType.LatestCreated,
* });
*
* if (loading) return <Loader />;
*
* if (error) return <Error message={error.message} />;
*
* function ExploreProfiles() {
* const { data, error, loading } = useExploreProfiles({
* orderBy: ExploreProfilesOrderByType.LatestCreated,
* });
* return (
* <>
* {data.map((profile) => (
* <Profile key={profile.id} profile={profile} />
* ))}
* </>
* );
* ```
*
* @category Discovery
* @group Hooks
*/
export function useExploreProfiles(args?: UseExploreProfilesArgs): PaginatedReadResult<Profile[]>;

/**
* Discover new profiles based on a defined criteria.
*
* if (loading) return <p>Loading...</p>;
* This signature supports [React Suspense](https://react.dev/reference/react/Suspense).
*
* if (error) return <p>Error: {error.message}</p>;
* ```ts
* const { data } = useExploreProfiles({
* orderBy: ExploreProfilesOrderByType.LatestCreated,
* suspense: true,
* );
*
* return (
* <ul>
* {data.map((profile) => (
* <li key={profile.id}>{profile.handle}</li>
* ))}
* </ul>
* );
* }
* console.log(data);
* ```
*
* @experimental This API can change without notice
* @category Discovery
* @group Hooks
*/
export function useExploreProfiles(
{ where, limit, orderBy = ExploreProfilesOrderByType.LatestCreated }: UseExploreProfilesArgs = {
args: UseSuspenseExploreProfilesArgs,
): SuspensePaginatedResult<Profile[]>;

export function useExploreProfiles(
{
where,
limit,
orderBy = ExploreProfilesOrderByType.LatestCreated,
suspense = false,
}: UseExploreProfilesArgs & { suspense?: boolean } = {
orderBy: ExploreProfilesOrderByType.LatestCreated,
suspense: false,
},
): PaginatedReadResult<Profile[]> {
return usePaginatedReadResult(
useBaseExploreProfilesQuery(
useLensApolloClient({
variables: useFragmentVariables({
limit,
where,
orderBy,
}),
): SuspendablePaginatedResult<Profile[]> {
return useSuspendablePaginatedQuery({
suspense,
query: ExploreProfilesDocument,
options: useLensApolloClient({
variables: useFragmentVariables({
limit,
where,
orderBy,
}),
),
);
}),
});
}
Loading

0 comments on commit 697eca1

Please sign in to comment.