From 9747c81d6c81e21f7b058addc3c6a19c07dac785 Mon Sep 17 00:00:00 2001 From: Henrique Macedo <2805206+ickas@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:07:43 +0100 Subject: [PATCH] feat: use native ai error --- extensions/time-teller/src/index.tsx | 57 +++++++++++++--------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/extensions/time-teller/src/index.tsx b/extensions/time-teller/src/index.tsx index f08ed95db7d..adfb4463f82 100644 --- a/extensions/time-teller/src/index.tsx +++ b/extensions/time-teller/src/index.tsx @@ -1,6 +1,6 @@ import { useEffect, useState } from "react"; -import { Action, ActionPanel, List, AI, environment } from "@raycast/api"; -import { getFavicon } from "@raycast/utils"; +import { Action, ActionPanel, List, AI, popToRoot, Icon } from "@raycast/api"; +import { showFailureToast } from "@raycast/utils"; import { setTimeout } from "timers/promises"; interface Timeframe { @@ -12,12 +12,11 @@ export default function Command() { const [searchText, setSearchText] = useState(""); const [timeframe, setTimeframe] = useState({ startDate: undefined, endDate: undefined }); const [isLoading, setIsLoading] = useState(false); - const hasProSubscription = environment.canAccess(AI); /** * Fetches and processes date information from the AI based on the current input text. * - * This effect is triggered whenever the search text changes. It waits for 1.5 seconds before + * This effect is triggered whenever the search text changes. It waits for 0.8 seconds before * querying the AI to avoid being rate limited. The AI's response is expected to be in the * format "startDate: YYYY-MM-DDTHH:MM:SSZ, endDate: YYYY-MM-DDTHH:MM:SSZ". * @@ -28,27 +27,38 @@ export default function Command() { * Checks if the input text is not empty before proceeding with the data fetching process. * This condition ensures that unnecessary requests are not sent to the AI when the input text is empty. */ - if (hasProSubscription && searchText !== "") { + if (searchText !== "") { const fetchData = async () => { setIsLoading(true); await setTimeout(800); const aiPrompt = `translate the date and give only a direct answer with the current date and the date in the text, or between the two dates in the text, format in ISO 8601 and give a string with start and end dates like "startDate: 2024-10-05T13:00:00Z, endDate: 2024-10-06T22:00:00Z": ${searchText}`; - const answer = await AI.ask(aiPrompt); - const dates = answer.split(", "); - const dateObject = dates.reduce((acc: Timeframe, date) => { - const [key, value] = date.split(": "); - acc[key as keyof Timeframe] = value; - return acc; - }, {} as Timeframe); + try { + const answer = await AI.ask(aiPrompt, {}); + const dates = answer.split(", "); + const dateObject = dates.reduce((acc: Timeframe, date) => { + const [key, value] = date.split(": "); + acc[key as keyof Timeframe] = value; + return acc; + }, {} as Timeframe); + + setTimeframe(dateObject); + } catch (err: unknown) { + if (err instanceof Error) { + if (err.message === "Process cancelled") { + popToRoot(); + } else { + showFailureToast(err); + } + } + } - setTimeframe(dateObject); setIsLoading(false); }; fetchData(); } - }, [searchText, hasProSubscription]); + }, [searchText]); function calculateTimeframes(startDate: Date, endDate: Date) { // Convert the input dates to date objects if they are not already @@ -85,22 +95,9 @@ export default function Command() { throttle={true} isLoading={isLoading} > - {/** - * Checks if the user has access to the AI feature. - * If not, displays an empty view with a message and a link to try Raycast Pro. - */} - {!hasProSubscription ? ( - - - - } - /> - ) : ( + + + {timeframe.startDate !== undefined && ( <>