diff --git a/package.json b/package.json index 4e50490..ef9b0ca 100644 --- a/package.json +++ b/package.json @@ -28,24 +28,6 @@ "placeholder": "Trunk ID", "type": "text" } - ], - "preferences": [ - { - "name": "ai-tagging", - "title": "Enable AI Tagging", - "description": "Automatically tag new destinations using AI (requires AI Instance URL)", - "type": "checkbox", - "required": true, - "default": false - }, - { - "name": "ai-instance", - "title": "AI Instance URL", - "description": "The URL of your LLM instance for AI-tagging", - "type": "textfield", - "required": false, - "placeholder": "https://ai.hackclub.com" - } ] }, { @@ -85,6 +67,22 @@ "title": "Session Token", "description": "The session token for your Regreso account. If you have a session token, you can use it instead of email and password.", "type": "password" + }, + { + "name": "ai-tagging", + "title": "Enable AI Tagging", + "description": "Automatically tag new destinations using AI (requires AI Instance URL)", + "type": "checkbox", + "required": true, + "default": false + }, + { + "name": "ai-instance", + "title": "AI Instance URL", + "description": "The URL of your LLM instance for AI-tagging", + "type": "textfield", + "required": false, + "placeholder": "https://ai.hackclub.com" } ], "dependencies": { diff --git a/src/create-destination.tsx b/src/create-destination.tsx index 88e5c1f..e6ce533 100644 --- a/src/create-destination.tsx +++ b/src/create-destination.tsx @@ -160,7 +160,6 @@ export default function CreateDestination( { - if ( destinationTypeProps.type.value == "location" && destinationTypeProps.location.value != destinationProps.location.value && diff --git a/src/list-destinations.tsx b/src/list-destinations.tsx index 1b7c8df..bf2a7de 100644 --- a/src/list-destinations.tsx +++ b/src/list-destinations.tsx @@ -1,137 +1,137 @@ -import { useEffect, useState } from "react"; -import { Icon, Action, ActionPanel, List, useNavigation, showToast, Toast } from "@raycast/api"; -import { usePromise } from "@raycast/utils"; - -import { fetchDestinations } from "./utils/destination"; -import { fetchWorkspaces } from "./utils/workspace"; -import { handleAuthStuff, setDefaultWorkspace } from "./utils/auth"; -import CreateDestination from "./create-destination"; - -export default function ListDestinations() { - const [searchText, setSearchText] = useState(""); - - const [sessionToken, setSessionToken] = useState(""); - const [workspaces, setWorkspaces] = useState([]); - const [selectedWorkspace, setSelectedWorkspace] = useState(undefined); - const { push } = useNavigation(); - - const { isLoading, data, pagination } = usePromise( - (searchText: string) => async (options: { page: number }) => { - if (!sessionToken || sessionToken == "") { - return { data: [], hasMore: false }; - } - const newData = await fetchDestinations(searchText, options.page * 10, selectedWorkspace, sessionToken); - - return { - data: newData.items.map((it, i) => { - it.page = options.page; - it.text = it.name; - it.index = i; - return it; - }), - hasMore: options.page * 10 + 10 < newData.count, - }; - }, - [searchText], - ); - - if (!sessionToken || sessionToken == "") { - handleAuthStuff() - .then(async (a) => { - setSessionToken(a.sessionToken); - setSearchText("e"); - await new Promise((r) => setTimeout(r, 100)); - setSearchText(""); - - setWorkspaces((await fetchWorkspaces(undefined, a.sessionToken)).items); - }) - .catch((error) => { - showToast({ - style: Toast.Style.Failure, - title: "Oops! Something went wrong with authentication", - message: error.message, - }); - }); - } - - return ( - 0 ? `List Destinations (${data.length})` : "List Destinations"} - actions={ - - push()} /> - - } - searchBarAccessory={ - { - if (newValue === "all") { - setSelectedWorkspace(undefined); - } else { - setSelectedWorkspace(parseInt(newValue)); - } - }} - > - - {workspaces.map((workspace) => ( - - ))} - - } - > - {data?.length === 0 ? ( - - ) : ( - data?.map((item) => ( - - - {item.type == "location" ? ( - - ) : null} - - - - - - - - - {item.tags.map((tag) => ( - - ))} - - - - - - } - /> - } - /> - )) - )} - - ); -} +import { useEffect, useState } from "react"; +import { Icon, Action, ActionPanel, List, useNavigation, showToast, Toast } from "@raycast/api"; +import { usePromise } from "@raycast/utils"; + +import { fetchDestinations } from "./utils/destination"; +import { fetchWorkspaces } from "./utils/workspace"; +import { handleAuthStuff, setDefaultWorkspace } from "./utils/auth"; +import CreateDestination from "./create-destination"; + +export default function ListDestinations() { + const [searchText, setSearchText] = useState(""); + + const [sessionToken, setSessionToken] = useState(""); + const [workspaces, setWorkspaces] = useState([]); + const [selectedWorkspace, setSelectedWorkspace] = useState(undefined); + const { push } = useNavigation(); + + const { isLoading, data, pagination } = usePromise( + (searchText: string) => async (options: { page: number }) => { + if (!sessionToken || sessionToken == "") { + return { data: [], hasMore: false }; + } + const newData = await fetchDestinations(searchText, options.page * 10, selectedWorkspace, sessionToken); + + return { + data: newData.items.map((it, i) => { + it.page = options.page; + it.text = it.name; + it.index = i; + return it; + }), + hasMore: options.page * 10 + 10 < newData.count, + }; + }, + [searchText], + ); + + if (!sessionToken || sessionToken == "") { + handleAuthStuff() + .then(async (a) => { + setSessionToken(a.sessionToken); + setSearchText("e"); + await new Promise((r) => setTimeout(r, 100)); + setSearchText(""); + + setWorkspaces((await fetchWorkspaces(undefined, a.sessionToken)).items); + }) + .catch((error) => { + showToast({ + style: Toast.Style.Failure, + title: "Oops! Something went wrong with authentication", + message: error.message, + }); + }); + } + + return ( + 0 ? `List Destinations (${data.length})` : "List Destinations"} + actions={ + + push()} /> + + } + searchBarAccessory={ + { + if (newValue === "all") { + setSelectedWorkspace(undefined); + } else { + setSelectedWorkspace(parseInt(newValue)); + } + }} + > + + {workspaces.map((workspace) => ( + + ))} + + } + > + {data?.length === 0 ? ( + + ) : ( + data?.map((item) => ( + + + {item.type == "location" ? ( + + ) : null} + + + + + + + + + {item.tags.map((tag) => ( + + ))} + + + + + + } + /> + } + /> + )) + )} + + ); +} diff --git a/src/utils/auth.ts b/src/utils/auth.ts index d9df2cc..ea63650 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,55 +1,55 @@ -// @ts-nocheck -import { getPreferenceValues, LocalStorage } from "@raycast/api"; - -export async function handleAuthStuff() { - const preferences = getPreferenceValues(); - - let session = await LocalStorage.getItem("session-token"); - - if (preferences["session-token"] && preferences["session-token"] != "") { - session = preferences["session-token"]; - } - let sessionInfo = { - id: false, - }; - - if (session && session != "") { - sessionInfo = await fetch(`${preferences["instance-url"]}/api/v1/session`, { - headers: { - Authorization: session, - "Content-Type": "application/json", - }, - }).then((res) => res.json()); - } - - if ( - !sessionInfo.id || - (sessionInfo.user.email != preferences["email"] && !sessionInfo.user.githubId && !sessionInfo.user.googleId) - ) { - await LocalStorage.setItem("session-token", ""); - - let newSessionInfo = { success: false }; - if (!preferences["session-token"]) { - newSessionInfo = await fetch(`${preferences["instance-url"]}/api/v1/session`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - email: preferences["email"], - password: preferences["password"], - }), - }).then((res) => res.json()); - } - - if (!newSessionInfo.success) { - await LocalStorage.setItem("session-token", ""); - throw new Error("Failed to authenticate with the provided credentials."); - } - - await LocalStorage.setItem("session-token", newSessionInfo.token); - return { sessionToken: newSessionInfo.token, defaultWorkspace: newSessionInfo.user.workspace }; - } - - return { sessionToken: session, defaultWorkspace: sessionInfo.user.workspace }; -} +// @ts-nocheck +import { getPreferenceValues, LocalStorage } from "@raycast/api"; + +export async function handleAuthStuff() { + const preferences = getPreferenceValues(); + + let session = await LocalStorage.getItem("session-token"); + + if (preferences["session-token"] && preferences["session-token"] != "") { + session = preferences["session-token"]; + } + let sessionInfo = { + id: false, + }; + + if (session && session != "") { + sessionInfo = await fetch(`${preferences["instance-url"]}/api/v1/session`, { + headers: { + Authorization: session, + "Content-Type": "application/json", + }, + }).then((res) => res.json()); + } + + if ( + !sessionInfo.id || + (sessionInfo.user.email != preferences["email"] && !sessionInfo.user.githubId && !sessionInfo.user.googleId) + ) { + await LocalStorage.setItem("session-token", ""); + + let newSessionInfo = { success: false }; + if (!preferences["session-token"]) { + newSessionInfo = await fetch(`${preferences["instance-url"]}/api/v1/session`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + email: preferences["email"], + password: preferences["password"], + }), + }).then((res) => res.json()); + } + + if (!newSessionInfo.success) { + await LocalStorage.setItem("session-token", ""); + throw new Error("Failed to authenticate with the provided credentials."); + } + + await LocalStorage.setItem("session-token", newSessionInfo.token); + return { sessionToken: newSessionInfo.token, defaultWorkspace: newSessionInfo.user.workspace }; + } + + return { sessionToken: session, defaultWorkspace: sessionInfo.user.workspace }; +} diff --git a/src/utils/destination.ts b/src/utils/destination.ts index 1f7614c..ad203fe 100644 --- a/src/utils/destination.ts +++ b/src/utils/destination.ts @@ -1,64 +1,64 @@ -import { getPreferenceValues } from "@raycast/api"; - -export async function fetchDestinations( - searchText: string, - offset: number, - workspaceId: number | undefined, - sessionToken: string, -): Promise { - const preferences = getPreferenceValues(); - - const response = await fetch( - `${preferences["instance-url"]}/api/v1/destinations?searchString=${searchText}&offset=${offset}&limit=10${workspaceId ? `&workspaceId=${workspaceId}` : ""}`, - { - method: "GET", - headers: { - Authorization: sessionToken, - "Content-Type": "application/json", - }, - }, - ) - .then((res) => res.json()) - .catch((e) => { - throw new Error(e.message); - }); - - return { items: response.items, count: response.count }; -} - -export async function createDestination( - title: string, - type: "note" | "location", - body: string | undefined, - url: string | undefined, - tags: string, - workspaceId: string, - sessionToken: string, -): Promise { - const preferences = getPreferenceValues(); - const response = await fetch(`${preferences["instance-url"]}/api/v1/destinations`, { - method: "POST", - headers: { - Authorization: sessionToken, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - name: title, - body, - type, - location: url, - tags: - tags == undefined || tags == "" - ? [] - : tags.split(",").map((tag) => { - return { text: tag.trim(), id: tag.trim() }; - }), - workspaceId: parseInt(workspaceId, 10), - attachments: [], - }), - }).then((res) => res.json()); - - if (!response.success) { - throw new Error(`${response.statusText}`); - } -} +import { getPreferenceValues } from "@raycast/api"; + +export async function fetchDestinations( + searchText: string, + offset: number, + workspaceId: number | undefined, + sessionToken: string, +): Promise { + const preferences = getPreferenceValues(); + + const response = await fetch( + `${preferences["instance-url"]}/api/v1/destinations?searchString=${searchText}&offset=${offset}&limit=10${workspaceId ? `&workspaceId=${workspaceId}` : ""}`, + { + method: "GET", + headers: { + Authorization: sessionToken, + "Content-Type": "application/json", + }, + }, + ) + .then((res) => res.json()) + .catch((e) => { + throw new Error(e.message); + }); + + return { items: response.items, count: response.count }; +} + +export async function createDestination( + title: string, + type: "note" | "location", + body: string | undefined, + url: string | undefined, + tags: string, + workspaceId: string, + sessionToken: string, +): Promise { + const preferences = getPreferenceValues(); + const response = await fetch(`${preferences["instance-url"]}/api/v1/destinations`, { + method: "POST", + headers: { + Authorization: sessionToken, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + name: title, + body, + type, + location: url, + tags: + tags == undefined || tags == "" + ? [] + : tags.split(",").map((tag) => { + return { text: tag.trim(), id: tag.trim() }; + }), + workspaceId: parseInt(workspaceId, 10), + attachments: [], + }), + }).then((res) => res.json()); + + if (!response.success) { + throw new Error(`${response.statusText}`); + } +} diff --git a/src/utils/workspace.ts b/src/utils/workspace.ts index 771be9c..d7b78f8 100644 --- a/src/utils/workspace.ts +++ b/src/utils/workspace.ts @@ -1,22 +1,22 @@ -import { getPreferenceValues } from "@raycast/api"; - -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${archived == "hi" ? `&archived=${"false"}` : ""}`, - { - method: "GET", - headers: { - Authorization: sessionToken, - "Content-Type": "application/json", - }, - }, - ) - .then((res) => res.json()) - .catch((e) => { - throw new Error(e.message); - }); - - return { items: response.items, count: response.count }; -} +import { getPreferenceValues } from "@raycast/api"; + +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${archived == "hi" ? `&archived=${"false"}` : ""}`, + { + method: "GET", + headers: { + Authorization: sessionToken, + "Content-Type": "application/json", + }, + }, + ) + .then((res) => res.json()) + .catch((e) => { + throw new Error(e.message); + }); + + return { items: response.items, count: response.count }; +}