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

useCachedPromise: Fix optimisticUpdate not working when paginating beyond the first page. #27

Merged
merged 2 commits into from
Mar 18, 2024
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
11 changes: 10 additions & 1 deletion docs/utils-reference/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ npm install --save @raycast/utils

## Changelog

### v1.13.1
### v1.13.3

- Fixed `optimisticUpdate` not working when paginating beyond the first page when using `useCachedPromise` or other hooks that build on top of it..
- Fixed `useFetch` type requiring `mapResult` for non-paginated overload.

### v1.13.2

- Added default OAuth URLs for Google, Jira, and Zoom

### v1.13.1

- Fixed `useFetch` type for non-paginated overload.

### v1.13.0

- Added pagination support to `usePromise`, `useCachedPromise` and `useFetch`.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raycast/utils",
"version": "1.13.2",
"version": "1.13.3",
"description": "Set of utilities to streamline building Raycast extensions",
"author": "Raycast Technologies Ltd.",
"homepage": "https://developers.raycast.com/utils-reference",
Expand Down
2 changes: 1 addition & 1 deletion src/useCachedPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export function useCachedPromise<
data: returnedData,
isLoading: state.isLoading,
error: state.error,
mutate,
mutate: paginationArgsRef.current && paginationArgsRef.current.page > 0 ? _mutate : mutate,
pagination,
revalidate,
};
Expand Down
2 changes: 1 addition & 1 deletion src/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function useFetch<V = unknown, U = undefined, T extends unknown[] = unkno
export function useFetch<V = unknown, U = undefined, T = V>(
url: RequestInfo,
options?: RequestInit & {
mapResult: (result: V) => { data: T; hasMore?: boolean };
mapResult?: (result: V) => { data: T; hasMore?: boolean };
parseResponse?: (response: Response) => Promise<V>;
} & Omit<CachedPromiseOptions<(url: RequestInfo, options?: RequestInit) => Promise<T>, U>, "abortable">,
): UseCachedPromiseReturnType<T, U> & { pagination: undefined };
Expand Down
33 changes: 21 additions & 12 deletions tests/src/cached-promise-paginated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { setTimeout } from "timers/promises";
export default function Command() {
const [searchText, setSearchText] = useState<string>("");

const { isLoading, data, pagination, revalidate } = useCachedPromise(
const { isLoading, data, mutate, pagination, revalidate } = useCachedPromise(
(text: string) => async (options) => {
await setTimeout(500);
const data = items(text, options.page);
Expand All @@ -20,18 +20,27 @@ export default function Command() {
);

return (
<List
isLoading={isLoading}
onSearchTextChange={setSearchText}
pagination={pagination}
actions={
<ActionPanel>
<Action title="Reload" onAction={() => revalidate()} />
</ActionPanel>
}
>
<List isLoading={isLoading} onSearchTextChange={setSearchText} pagination={pagination}>
{data.map((item) => (
<List.Item key={item} title={item} />
<List.Item
key={item}
title={item}
actions={
<ActionPanel>
<Action title="Reload" onAction={() => revalidate()} />
<Action
title="Delete All Items But This One"
onAction={async () => {
mutate(setTimeout(1000), {
optimisticUpdate: () => {
return [item];
},
});
}}
/>
</ActionPanel>
}
/>
))}
</List>
);
Expand Down
13 changes: 12 additions & 1 deletion tests/src/fetch-paginated.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ActionPanel, Action, Icon, Image, List, Grid } from "@raycast/api";
import { useFetch } from "@raycast/utils";
import { useState } from "react";
import { setTimeout } from "timers/promises";

type SearchResult = {
companies: Company[];
Expand Down Expand Up @@ -39,7 +40,7 @@ function getSearchParams(searchText: string, page: number) {
export default function Command() {
const [searchText, setSearchText] = useState("");

const { isLoading, pagination, data, revalidate } = useFetch(
const { isLoading, pagination, data, mutate, revalidate } = useFetch(
(pagination) =>
"https://api.ycombinator.com/v0.1/companies?" + getSearchParams(searchText, pagination.page + 1).toString(),
{
Expand All @@ -64,6 +65,16 @@ export default function Command() {
actions={
<ActionPanel title={company.name}>
<Action title="Reload" onAction={() => revalidate()} />
<Action
title="Delete All Items But This One"
onAction={async () => {
mutate(setTimeout(1000), {
optimisticUpdate: () => {
return [company];
},
});
}}
/>
</ActionPanel>
}
/>
Expand Down
36 changes: 24 additions & 12 deletions tests/src/promise-paginated.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { List, ActionPanel, Action } from "@raycast/api";
import { usePromise } from "@raycast/utils";
import { useState } from "react";
import { setTimeout as setTimeoutAsync } from "timers/promises";

export default function Command() {
const [searchText, setSearchText] = useState<string>("");

const { isLoading, data, revalidate, pagination } = usePromise(
const { isLoading, data, revalidate, mutate, pagination } = usePromise(
(text: string) => async (options) => {
await sleep(500);
const data = items(text, options.page);
Expand All @@ -15,17 +16,28 @@ export default function Command() {
);

return (
<List
isLoading={isLoading}
onSearchTextChange={setSearchText}
pagination={pagination}
actions={
<ActionPanel>
<Action title="Reload" onAction={() => revalidate()} />
</ActionPanel>
}
>
{data?.map((item) => <List.Item key={item} title={item} />)}
<List isLoading={isLoading} onSearchTextChange={setSearchText} pagination={pagination}>
{data?.map((item) => (
<List.Item
key={item}
title={item}
actions={
<ActionPanel>
<Action title="Reload" onAction={() => revalidate()} />
<Action
title="Delete All Items But This One"
onAction={async () => {
mutate(setTimeoutAsync(1000), {
optimisticUpdate: () => {
return [item];
},
});
}}
/>
</ActionPanel>
}
/>
))}
</List>
);
}
Expand Down
Loading