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 failureToastOptions prop for usePromise, useCachedPromise, and useFetch #34

Merged
merged 3 commits into from
Jun 11, 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
8 changes: 8 additions & 0 deletions docs/utils-reference/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ npm install --save @raycast/utils

## Changelog

### v1.16.0

- Add a `failureToastOptions` prop to `useFetch`, `useCachedPromise`, and `usePromise` to make it possible to customize the error displayed instead of a generic "Failed to fetch latest data".

### v1.15.0

- Add `useLocalStorage` hook.

### v1.14.0

- Add `useStreamJSON` hook.
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.15.0",
"version": "1.16.0",
"description": "Set of utilities to streamline building Raycast extensions",
"author": "Raycast Technologies Ltd.",
"homepage": "https://developers.raycast.com/utils-reference",
Expand Down
7 changes: 5 additions & 2 deletions src/showFailureToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ import { Clipboard, environment, open, Toast, showToast } from "@raycast/api";
* }
* ```
*/
export function showFailureToast(error: unknown, options?: { title?: string; primaryAction?: Toast.ActionOptions }) {
export function showFailureToast(
error: unknown,
options?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>,
) {
const message = error instanceof Error ? error.message : String(error);
return showToast({
style: Toast.Style.Failure,
title: options?.title ?? "Something went wrong",
message: message,
message: options?.message ?? message,
primaryAction: options?.primaryAction ?? handleErrorToastAction(error),
secondaryAction: options?.primaryAction ? handleErrorToastAction(error) : undefined,
});
Expand Down
2 changes: 2 additions & 0 deletions src/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export function useFetch<V = unknown, U = undefined, T extends unknown[] = unkno
onError,
onData,
onWillExecute,
failureToastOptions,
...fetchOptions
} = options || {};

Expand All @@ -136,6 +137,7 @@ export function useFetch<V = unknown, U = undefined, T extends unknown[] = unkno
onError,
onData,
onWillExecute,
failureToastOptions,
};

const parseResponseRef = useLatest(parseResponse || defaultParsing);
Expand Down
8 changes: 7 additions & 1 deletion src/usePromise.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useCallback, MutableRefObject, useRef, useState } from "react";
import { environment, LaunchType } from "@raycast/api";
import { environment, LaunchType, Toast } from "@raycast/api";
import { useDeepMemo } from "./useDeepMemo";
import {
FunctionReturningPromise,
Expand All @@ -26,6 +26,11 @@ export type PromiseOptions<T extends FunctionReturningPromise | FunctionReturnin
* wait util you have all the arguments ready to execute the function.
*/
execute?: boolean;
/**
* Options for the generic failure toast.
* It allows you to customize the title, message, and primary action of the failure toast.
*/
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
/**
* Called when an execution fails. By default it will log the error and show
* a generic failure toast.
Expand Down Expand Up @@ -191,6 +196,7 @@ export function usePromise<T extends FunctionReturningPromise | FunctionReturnin
latestCallback.current?.(...((latestArgs.current || []) as Parameters<T>));
},
},
...options?.failureToastOptions,
});
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@
"subtitle": "Utils Smoke Tests",
"description": "Utils Smoke Tests",
"mode": "view"
},
{
"name": "show-failure-toast",
"title": "showFailureToast",
"subtitle": "Utils Smoke Tests",
"description": "Utils Smoke Tests",
"mode": "view"
}
],
"dependencies": {
Expand Down
67 changes: 67 additions & 0 deletions tests/src/show-failure-toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Action, ActionPanel, List, openCommandPreferences } from "@raycast/api";
import { showFailureToast } from "@raycast/utils";

export default function Command() {
return (
<List>
<List.Item
title="Default Failure Toast"
actions={
<ActionPanel>
<Action title="Show Toast" onAction={() => showFailureToast(new Error("Some Error"))} />
</ActionPanel>
}
/>
<List.Item
title="Failure Toast with Title"
actions={
<ActionPanel>
<Action
title="Show Toast"
onAction={() =>
showFailureToast(new Error("Some Error"), {
title: "Custom Title",
})
}
/>
</ActionPanel>
}
/>
<List.Item
title="Failure Toast with Custom Action"
actions={
<ActionPanel>
<Action
title="Show Toast"
onAction={() =>
showFailureToast(new Error("Some Error"), {
primaryAction: {
title: "Open Command Preferences",
onAction: () => {
openCommandPreferences();
},
},
})
}
/>
</ActionPanel>
}
/>
<List.Item
title="Failure Toast with Custom Message"
actions={
<ActionPanel>
<Action
title="Show Toast"
onAction={() =>
showFailureToast(new Error("Some Error"), {
message: "Custom Message",
})
}
/>
</ActionPanel>
}
/>
</List>
);
}
Loading