-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dub Link Shortener] Improvements to 'Search Links' and 'Shorten Link…
…s' commands (#12992) * perf(dub-link-shortener): adds workspace and domain selection to commands closes #12991 * refactor(dub-link-shortener): lint fix closes #12991 * doc(dub-link-shortener): improved docs closes #12991 * perf(dub-link-shortener): better error handling and mutation closes #12991 * refactor(dub-link-shortener): imports and accessories closes #12991 * fix(dub-link-shortener): correct use for mutate closes #12991 * fix(dub-link-shortener): more disambiguation for user-agent closes #12991 * refactor(dub-link-shortener): optimize axios calls closes #12991 * chore(dub-link-shortener): upgraded dependencies closes #12991 * refactor(dub-link-shortener) adds todos closes #12991 * Update CHANGELOG.md and optimise images --------- Co-authored-by: raycastbot <bot@raycast.com>
- Loading branch information
1 parent
55f01ca
commit aa33412
Showing
27 changed files
with
1,165 additions
and
673 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import axios, { AxiosRequestConfig } from "axios"; | ||
import { BASE_API_URL } from "@utils/constants"; | ||
import { apiKey, commandName, extensionName } from "@utils/env"; | ||
import { DeleteLinkResponseBody, LinkSchema, TagSchema, WorkspaceId, WorkspaceSchema } from "@/types"; | ||
import { captureException } from "@raycast/api"; | ||
|
||
const hasHttps = (url: string) => url.startsWith("https://"); | ||
const headers = { | ||
Authorization: "Bearer " + apiKey, | ||
"Content-Type": "application/json", | ||
"user-agent": `raycast/${extensionName}/${commandName}`, | ||
}; | ||
|
||
/** | ||
* todo: Replace with SDK https://d.to/sdk once it is stable | ||
* see: https://github.com/dubinc/dub-node/blob/765e170c45a361de3ae62e0d19571ceca4a3f0f4/README.md#maturity | ||
*/ | ||
export const callApi = async <T>(config: AxiosRequestConfig) => { | ||
return await axios({ ...config, headers }) | ||
.then(({ data }) => data as T) | ||
.catch(({ response }) => { | ||
const err = new Error(response?.data?.error?.message ?? "Unknown error"); | ||
captureException(err); | ||
throw err; | ||
}); | ||
}; | ||
|
||
export const getAllWorkspaces = async () => { | ||
return await callApi<WorkspaceSchema[]>({ method: "GET", url: `${BASE_API_URL}/workspaces` }); | ||
}; | ||
|
||
export const createShortLink = async ({ | ||
originalUrl, | ||
key, | ||
workspaceId, | ||
domain, | ||
tagIds, | ||
comments, | ||
}: { originalUrl: string; key?: string; domain?: string; tagIds?: string[]; comments?: string } & WorkspaceId) => { | ||
const url = hasHttps(originalUrl) ? originalUrl : `https://${originalUrl}`; | ||
return await callApi<LinkSchema>({ | ||
method: "POST", | ||
url: `${BASE_API_URL}/links?workspaceId=${workspaceId}`, | ||
data: { domain, url, key, tagIds, comments }, | ||
}); | ||
}; | ||
|
||
export const getAllShortLinks = async ({ workspaceId }: WorkspaceId) => { | ||
return await callApi<LinkSchema[]>({ method: "GET", url: `${BASE_API_URL}/links?workspaceId=${workspaceId}` }); | ||
}; | ||
|
||
export const deleteShortLink = async ({ linkId, workspaceId }: { linkId: string } & WorkspaceId) => { | ||
return await callApi<DeleteLinkResponseBody>({ | ||
method: "DELETE", | ||
url: `${BASE_API_URL}/links/${linkId}?workspaceId=${workspaceId}`, | ||
}); | ||
}; | ||
|
||
/** | ||
* todo: Add commands and api(s) to create/manage tags in the workspace. | ||
*/ | ||
export const getAllTags = async ({ workspaceId }: WorkspaceId) => { | ||
return await callApi<TagSchema[]>({ method: "GET", url: `${BASE_API_URL}/tags?workspaceId=${workspaceId}` }); | ||
}; |
10 changes: 0 additions & 10 deletions
10
extensions/dub-link-shortener/src/components/action-go-dub-co.tsx
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
extensions/dub-link-shortener/src/components/list-empty-view.tsx
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
extensions/dub-link-shortener/src/hooks/use-short-links.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useCachedPromise } from "@raycast/utils"; | ||
import { getAllShortLinks } from "@/api"; | ||
import { WorkspaceId } from "@/types"; | ||
|
||
export const useShortLinks = ({ workspaceId, workspacesError }: WorkspaceId & { workspacesError?: Error }) => { | ||
const { | ||
data: shortLinks, | ||
isLoading, | ||
error, | ||
mutate, | ||
} = useCachedPromise(getAllShortLinks, [{ workspaceId }], { | ||
initialData: [], | ||
execute: workspaceId.length > 0 && !workspacesError, | ||
failureToastOptions: { title: "❗ Failed to fetch short links" }, | ||
}); | ||
|
||
return { shortLinks, mutate, isLoading: (!shortLinks && !error) || isLoading, error }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useCachedPromise } from "@raycast/utils"; | ||
import { getAllTags, getAllWorkspaces } from "@/api"; | ||
import { WorkspaceSchema } from "@/types"; | ||
|
||
export const useWorkspaces = () => { | ||
const { | ||
data: workspaces, | ||
isLoading, | ||
error, | ||
} = useCachedPromise( | ||
async () => { | ||
const workspaces = await getAllWorkspaces(); | ||
return await Promise.all( | ||
workspaces.map(async (w) => { | ||
const tags = await getAllTags({ workspaceId: w.id }); | ||
return { ...w, tags } as WorkspaceSchema; | ||
}), | ||
); | ||
}, | ||
[], | ||
{ | ||
initialData: [], | ||
failureToastOptions: { title: "❗ Failed to fetch workspaces" }, | ||
}, | ||
); | ||
|
||
return { workspaces, isLoading: (!workspaces && !error) || isLoading, error }; | ||
}; |
Oops, something went wrong.