diff --git a/src/create-destination.tsx b/src/create-destination.tsx index affc287..6716d44 100644 --- a/src/create-destination.tsx +++ b/src/create-destination.tsx @@ -7,6 +7,7 @@ import { getWebDetails } from "@regreso/utils"; import { handleAuthStuff } from "./utils/auth"; import { createDestination } from "./utils/destination"; +import { fetchWorkspaces } from "./utils/workspace"; interface DestinationTypeFormValues { type: string; @@ -56,22 +57,36 @@ export default function CreateDestination( if (workspaces.length > 0 || sessionToken == "") { return; } - - fetch(`${preferences["instance-url"]}/api/v1/workspaces?archived=false`, { - method: "GET", - headers: { - Authorization: sessionToken, - "Content-Type": "application/json", - }, - }) - .then((res) => res.json()) - .then((data) => { - return data.items.map((workspace: any) => { - return { name: workspace.name, id: workspace.id }; - }); + fetchWorkspaces(false, sessionToken) + .then((workspacesResponse) => { + if (!workspacesResponse || !workspacesResponse.items) { + showToast({ + style: Toast.Style.Failure, + title: "Failed to fetch workspaces", + message: "Please check your network connection or try again later.", + }); + return; + } + if (workspacesResponse.items.length === 0) { + showToast({ + style: Toast.Style.Failure, + title: "No workspaces found", + message: "Please create a workspace before creating a destination.", + }); + return; + } + setWorkspaces( + workspacesResponse.items.map((workspace: any) => { + return { name: workspace.name, id: workspace.id }; + }), + ); }) - .then((its: Workspace[]) => { - setWorkspaces(its); + .catch((error) => { + showToast({ + style: Toast.Style.Failure, + title: "Failed to fetch workspaces", + message: error.message, + }); }); }, [setWorkspaces, sessionToken]); const { title, url, trunk } = props.arguments ?? { title: "", url: "", trunk: undefined }; @@ -102,7 +117,7 @@ export default function CreateDestination( enableDrafts actions={ - {(destinationProps.type.value == "note" && destinationProps.headline.value != "") || + {(destinationTypeProps.type.value == "note" && destinationProps.headline.value != "") || (destinationProps.type.value == "location" && destinationProps.location.value != "" && destinationProps.location.value != undefined) ? ( @@ -124,8 +139,8 @@ export default function CreateDestination( title: "Success!", message: "Destination created successfully", }); - await new Promise((r) => setTimeout(r, 500)); - popToRoot(); + await new Promise((r) => setTimeout(r, 500)); + popToRoot(); }) .catch((error) => { showToast({ @@ -134,7 +149,6 @@ export default function CreateDestination( message: error.message, }); }); - }} /> ) : null} diff --git a/src/list-destinations.tsx b/src/list-destinations.tsx index f7c42e2..1b7c8df 100644 --- a/src/list-destinations.tsx +++ b/src/list-destinations.tsx @@ -43,7 +43,7 @@ export default function ListDestinations() { await new Promise((r) => setTimeout(r, 100)); setSearchText(""); - setWorkspaces((await fetchWorkspaces(a.sessionToken)).items); + setWorkspaces((await fetchWorkspaces(undefined, a.sessionToken)).items); }) .catch((error) => { showToast({ diff --git a/src/utils/destination.ts b/src/utils/destination.ts index dca1d32..1f7614c 100644 --- a/src/utils/destination.ts +++ b/src/utils/destination.ts @@ -47,8 +47,14 @@ export async function createDestination( body, type, location: url, - tags: tags.split(",").map((tag) => tag.trim()), + tags: + tags == undefined || tags == "" + ? [] + : tags.split(",").map((tag) => { + return { text: tag.trim(), id: tag.trim() }; + }), workspaceId: parseInt(workspaceId, 10), + attachments: [], }), }).then((res) => res.json()); diff --git a/src/utils/workspace.ts b/src/utils/workspace.ts index e993e61..771be9c 100644 --- a/src/utils/workspace.ts +++ b/src/utils/workspace.ts @@ -1,15 +1,18 @@ import { getPreferenceValues } from "@raycast/api"; -export async function fetchWorkspaces(sessionToken: string): Promise { +export async function fetchWorkspaces(archived: boolean | undefined, sessionToken: string): Promise { const preferences = getPreferenceValues(); - const response = await fetch(`${preferences["instance-url"]}/api/v1/workspaces?offset=0&limit=10`, { - method: "GET", - headers: { - Authorization: sessionToken, - "Content-Type": "application/json", + const response = await fetch( + `${preferences["instance-url"]}/api/v1/workspaces?offset=0&limit=10${archived == "hi" ? `&archived=${"false"}` : ""}`, + { + method: "GET", + headers: { + Authorization: sessionToken, + "Content-Type": "application/json", + }, }, - }) + ) .then((res) => res.json()) .catch((e) => { throw new Error(e.message);