Skip to content
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

Add first batch of methods to react SDK #1059

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/client/src/actions/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export function fetchPostBookmarks(
*
* ```ts
* const result = await fetchPostReferences(anyClient, {
* referencedTypes: [PostReferenceType.CommentOn],
* referencedPost: postId('42'),
* });
* ```
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/account/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './useAccount';
export * from './useAccounts';
4 changes: 2 additions & 2 deletions packages/react/src/account/useAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type UseAccountArgs = AccountRequest;
* This signature supports React Suspense:
*
* ```tsx
* const { data } = useAccount({ managedBy: evmAddress('0x…'), suspense: true });
* const { data } = useAccount({ address: evmAddress('0x…'), suspense: true });
* ```
*/
export function useAccount(args: UseAccountArgs & Suspendable): SuspenseResult<Account | null>;
Expand All @@ -24,7 +24,7 @@ export function useAccount(args: UseAccountArgs & Suspendable): SuspenseResult<A
* Fetch a single Account.
*
* ```tsx
* const { data } = useAccount({ managedBy: evmAddress('0x…') });
* const { data, loading } = useAccount({ address: evmAddress('0x…') });
* ```
*/
export function useAccount(args: UseAccountArgs): ReadResult<Account | null>;
Expand Down
51 changes: 51 additions & 0 deletions packages/react/src/account/useAccounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { Account, AccountsRequest, Paginated } from '@lens-protocol/graphql';
import { AccountsQuery } from '@lens-protocol/graphql';

import type { ReadResult, Suspendable, SuspendableResult, SuspenseResult } from '../helpers';
import { useSuspendableQuery } from '../helpers';

export type AccountsArgs = AccountsRequest;

/**
* Fetch accounts available filtered by the given arguments.
*
* This signature supports React Suspense:
*
* ```tsx
* const { data } = useAccounts({
* filter: {
* searchBy: {
* localNameQuery: 'wagmi'
* },
* },
* suspense: true
* });
* ```
*/
export function useAccounts(args: AccountsArgs & Suspendable): SuspenseResult<Paginated<Account>>;

/**
* Fetch accounts available filtered by the given arguments.
*
* ```tsx
* const { data, loading } = useAccounts({
* filter: {
* searchBy: {
* localNameQuery: 'wagmi'
* },
* },
* );
* ```
*/
export function useAccounts(args: AccountsArgs): ReadResult<Paginated<Account>>;

export function useAccounts({
suspense = false,
...request
}: AccountsArgs & { suspense?: boolean }): SuspendableResult<Paginated<Account>> {
return useSuspendableQuery({
document: AccountsQuery,
variables: { request },
suspense: suspense,
});
}
2 changes: 1 addition & 1 deletion packages/react/src/authentication/useAccountsAvailable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function useAccountsAvailable(
* Fetch the accounts available for a given address.
*
* ```tsx
* const { data } = useAccountsAvailable({ managedBy: evmAddress('0x…') });
* const { data, loading } = useAccountsAvailable({ managedBy: evmAddress('0x…') });
* ```
*/
export function useAccountsAvailable(
Expand Down
6 changes: 4 additions & 2 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ export * from '@lens-protocol/client';
export * from './account';
export * from './authentication';
export type {
AsyncTaskError,
AsyncTaskIdle,
AsyncTaskLoading,
AsyncTaskSuccess,
AsyncTaskError,
AsyncTaskState,
AsyncTaskSuccess,
UseAsyncTask,
} from './helpers';
export * from './LensProvider';
export * from './post';
export * from './timeline';
4 changes: 4 additions & 0 deletions packages/react/src/post/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './usePost';
export * from './usePostReactions';
export * from './usePostReferences';
export * from './usePosts';
38 changes: 38 additions & 0 deletions packages/react/src/post/usePost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { AnyPost, PostRequest } from '@lens-protocol/graphql';
import { PostQuery } from '@lens-protocol/graphql';

import type { ReadResult, Suspendable, SuspendableResult, SuspenseResult } from '../helpers';
import { useSuspendableQuery } from '../helpers';

export type UsePostArgs = PostRequest;

/**
* Fetch a single post.
*
* This signature supports React Suspense:
*
* ```tsx
* const { data } = usePost({ post: postId('34…'), suspense: true });
* ```
*/
export function usePost(args: UsePostArgs & Suspendable): SuspenseResult<AnyPost | null>;

/**
* Fetch a single post.
*
* ```tsx
* const { data, loading } = usePost({ post: postId('34…') });
* ```
*/
export function usePost(args: UsePostArgs): ReadResult<AnyPost | null>;

export function usePost({
suspense = false,
...request
}: UsePostArgs & { suspense?: boolean }): SuspendableResult<AnyPost | null> {
return useSuspendableQuery({
document: PostQuery,
variables: { request },
suspense,
});
}
47 changes: 47 additions & 0 deletions packages/react/src/post/usePostReactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { AccountPostReaction, Paginated, PostReactionsRequest } from '@lens-protocol/graphql';
import { PostReactionsQuery } from '@lens-protocol/graphql';

import type { ReadResult, Suspendable, SuspendableResult, SuspenseResult } from '../helpers';
import { useSuspendableQuery } from '../helpers';

export type PostReactionsArgs = PostReactionsRequest;

/**
* Fetch reactions to a post.
*
* This signature supports React Suspense:
*
* ```tsx
* const { data } = usePostReactions({
* post: postId('42'),
* suspense: true
* });
* ```
*/
export function usePostReactions(
args: PostReactionsArgs & Suspendable,
): SuspenseResult<Paginated<AccountPostReaction>>;

/**
* Fetch reactions to a post.
*
* ```tsx
* const { data, loading } = usePostReactions({
* post: postId('42'),
* );
* ```
*/
export function usePostReactions(
args: PostReactionsArgs,
): ReadResult<Paginated<AccountPostReaction>>;

export function usePostReactions({
suspense = false,
...request
}: PostReactionsArgs & { suspense?: boolean }): SuspendableResult<Paginated<AccountPostReaction>> {
return useSuspendableQuery({
document: PostReactionsQuery,
variables: { request },
suspense: suspense,
});
}
47 changes: 47 additions & 0 deletions packages/react/src/post/usePostReferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { AnyPost, Paginated, PostReferencesRequest } from '@lens-protocol/graphql';
import { PostReferencesQuery } from '@lens-protocol/graphql';

import type { ReadResult, Suspendable, SuspendableResult, SuspenseResult } from '../helpers';
import { useSuspendableQuery } from '../helpers';

export type PostReferencesArgs = PostReferencesRequest;

/**
* Fetch references to a post.
*
* This signature supports React Suspense:
*
* ```tsx
* const { data } = usePostReferences({
* referencedTypes: [PostReferenceType.CommentOn],
* referencedPost: postId('42'),
* suspense: true
* });
* ```
*/
export function usePostReferences(
args: PostReferencesArgs & Suspendable,
): SuspenseResult<Paginated<AnyPost>>;

/**
* Fetch references to a post.
*
* ```tsx
* const { data, loading } = usePostReferences({
* referencedTypes: [PostReferenceType.CommentOn],
* referencedPost: postId('42'),
* );
* ```
*/
export function usePostReferences(args: PostReferencesArgs): ReadResult<Paginated<AnyPost>>;

export function usePostReferences({
suspense = false,
...request
}: PostReferencesArgs & { suspense?: boolean }): SuspendableResult<Paginated<AnyPost>> {
return useSuspendableQuery({
document: PostReferencesQuery,
variables: { request },
suspense: suspense,
});
}
47 changes: 47 additions & 0 deletions packages/react/src/post/usePosts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { AnyPost, Paginated, PostsRequest } from '@lens-protocol/graphql';
import { PostsQuery } from '@lens-protocol/graphql';

import type { ReadResult, Suspendable, SuspendableResult, SuspenseResult } from '../helpers';
import { useSuspendableQuery } from '../helpers';

export type PostsArgs = PostsRequest;

/**
* Fetch posts available filtered by the given arguments.
*
* This signature supports React Suspense:
*
* ```tsx
* const { data } = usePosts({
* filter: {
* searchQuery: 'test',
* },
* suspense: true
* });
* ```
*/
export function usePosts(args: PostsArgs & Suspendable): SuspenseResult<Paginated<AnyPost>>;

/**
* Fetch posts available filtered by the given arguments.
*
* ```tsx
* const { data, loading } = usePosts({
* filter: {
* searchQuery: 'test',
* },
* });
* ```
*/
export function usePosts(args: PostsArgs): ReadResult<Paginated<AnyPost>>;

export function usePosts({
suspense = false,
...request
}: PostsArgs & { suspense?: boolean }): SuspendableResult<Paginated<AnyPost>> {
return useSuspendableQuery({
document: PostsQuery,
variables: { request },
suspense: suspense,
});
}
1 change: 1 addition & 0 deletions packages/react/src/timeline/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useTimeline';
40 changes: 40 additions & 0 deletions packages/react/src/timeline/useTimeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Paginated, TimelineItem, TimelineRequest } from '@lens-protocol/graphql';
import { TimelineQuery } from '@lens-protocol/graphql';

import type { ReadResult, Suspendable, SuspendableResult, SuspenseResult } from '../helpers';
import { useSuspendableQuery } from '../helpers';

export type TimelineArgs = TimelineRequest;

/**
* Fetch timeline from an account.
*
* This signature supports React Suspense:
*
* ```tsx
* const { data } = useTimeline({ account: evmAddress('0x…'), suspense: true });
* ```
*/
export function useTimeline(
args: TimelineArgs & Suspendable,
): SuspenseResult<Paginated<TimelineItem>>;

/**
* Fetch timeline from an account.
*
* ```tsx
* const { data, loading } = useTimeline({ account: evmAddress('0x…') });
* ```
*/
export function useTimeline(args: TimelineArgs): ReadResult<Paginated<TimelineItem>>;

export function useTimeline({
suspense = false,
...request
}: TimelineArgs & { suspense?: boolean }): SuspendableResult<Paginated<TimelineItem>> {
return useSuspendableQuery({
document: TimelineQuery,
variables: { request },
suspense: suspense,
});
}
Loading