Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslombart committed Jun 7, 2024
1 parent 73d1a3a commit 84f1d92
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
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
4 changes: 2 additions & 2 deletions src/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function useFetch<V = unknown, U = undefined, T extends unknown[] = unkno
onError,
onData,
onWillExecute,
errorTitle,
failureToastOptions,
...fetchOptions
} = options || {};

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

const parseResponseRef = useLatest(parseResponse || defaultParsing);
Expand Down
11 changes: 6 additions & 5 deletions 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 Down Expand Up @@ -27,10 +27,10 @@ export type PromiseOptions<T extends FunctionReturningPromise | FunctionReturnin
*/
execute?: boolean;
/**
* Custom error title for the generic failure toast.
* This will replace "Failed to fetch latest data".
* Options for the generic failure toast.
* It allows you to customize the title, message, and primary action of the failure toast.
*/
errorTitle?: string;
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 @@ -188,14 +188,15 @@ export function usePromise<T extends FunctionReturningPromise | FunctionReturnin
} else {
if (environment.launchType !== LaunchType.Background) {
showFailureToast(error, {
title: options?.errorTitle ?? "Failed to fetch latest data",
title: "Failed to fetch latest data",
primaryAction: {
title: "Retry",
onAction(toast) {
toast.hide();
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>
);
}

0 comments on commit 84f1d92

Please sign in to comment.