Skip to content

Commit

Permalink
Merge pull request #13 from swan-io/async-data-hooks-2
Browse files Browse the repository at this point in the history
Add AsyncData hooks
  • Loading branch information
bloodyowl authored Oct 29, 2024
2 parents 81339fe + 3469a6a commit 322ce36
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/react/usePagination.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DocumentNode } from "@0no-co/graphql.web";
import { Option, Result } from "@swan-io/boxed";
import { AsyncData, Option, Result } from "@swan-io/boxed";
import { useCallback, useContext, useRef, useSyncExternalStore } from "react";
import { match } from "ts-pattern";
import { Connection } from "../types";
Expand Down Expand Up @@ -180,3 +180,33 @@ const createPaginationHook = (direction: mode) => {
export const useForwardPagination = createPaginationHook("after");

export const useBackwardPagination = createPaginationHook("before");

export const useForwardAsyncDataPagination = <
A,
E,
T extends AsyncData<Result<Connection<A>, E>>,
>(
connection: T,
): T => {
const data = connection
.toOption()
.flatMap((result) => result.toOption())
.toNull();
const patchedData = useForwardPagination(data);
return connection.mapOk(() => patchedData) as T;
};

export const useBackwardAsyncDataPagination = <
A,
E,
T extends AsyncData<Result<Connection<A>, E>>,
>(
connection: T,
): T => {
const data = connection
.toOption()
.flatMap((result) => result.toOption())
.toNull();
const patchedData = useBackwardPagination(data);
return connection.mapOk(() => patchedData) as T;
};

0 comments on commit 322ce36

Please sign in to comment.