2024-10-08 12:32:47 -04:00
|
|
|
import { UnitLength_type } from '@kittycad/lib/dist/types/src/models'
|
2025-01-09 17:58:12 -05:00
|
|
|
import { ASK_TO_OPEN_QUERY_PARAM, CREATE_FILE_URL_PARAM, PROD_APP_URL } from './constants'
|
2024-10-08 12:32:47 -04:00
|
|
|
import { stringToBase64 } from './base64'
|
2025-01-09 17:58:12 -05:00
|
|
|
import { DEV } from 'env'
|
2024-10-08 14:52:38 -04:00
|
|
|
export interface FileLinkParams {
|
2024-10-10 18:55:31 -04:00
|
|
|
code: string
|
|
|
|
name: string
|
|
|
|
units: UnitLength_type
|
2024-10-08 14:52:38 -04:00
|
|
|
}
|
|
|
|
|
2025-01-10 12:57:16 -05:00
|
|
|
/**
|
|
|
|
* Creates a URL with the necessary query parameters to trigger
|
|
|
|
* the "Import file from URL" command in the app.
|
|
|
|
*
|
|
|
|
* With the additional step of asking the user if they want to
|
|
|
|
* open the URL in the desktop app.
|
|
|
|
*/
|
|
|
|
export function createCreateFileUrl({ code, name, units }: FileLinkParams) {
|
|
|
|
// Use the dev server if we are in development mode
|
|
|
|
let origin = DEV ? 'http://localhost:3000' : PROD_APP_URL
|
|
|
|
const searchParams = new URLSearchParams({
|
|
|
|
[CREATE_FILE_URL_PARAM]: '',
|
|
|
|
name,
|
|
|
|
units,
|
|
|
|
code: stringToBase64(code),
|
|
|
|
[ASK_TO_OPEN_QUERY_PARAM]: '',
|
|
|
|
})
|
|
|
|
const createFileUrl = new URL(`?${searchParams.toString()}`, origin)
|
|
|
|
|
|
|
|
return createFileUrl
|
|
|
|
}
|
|
|
|
|
2024-10-08 12:32:47 -04:00
|
|
|
/**
|
2025-01-10 11:18:14 -05:00
|
|
|
* Given a file's code, name, and units, creates shareable link to the
|
|
|
|
* web app with a query parameter that triggers a modal to "open in desktop app".
|
|
|
|
* That modal is defined in the `OpenInDesktopAppHandler` component.
|
2025-01-08 16:56:48 -05:00
|
|
|
* TODO: update the return type to use TS library after its updated
|
2024-10-08 12:32:47 -04:00
|
|
|
*/
|
2025-01-10 12:57:16 -05:00
|
|
|
export async function createShortlink(
|
2025-01-08 12:00:19 -05:00
|
|
|
token: string,
|
2025-01-10 12:57:16 -05:00
|
|
|
url: string
|
2025-01-08 16:56:48 -05:00
|
|
|
): Promise<Error | { key: string; url: string }> {
|
|
|
|
/**
|
|
|
|
* We don't use our `withBaseURL` function here because
|
|
|
|
* there is no URL shortener service in the dev API.
|
2025-01-09 17:58:12 -05:00
|
|
|
*/
|
2025-01-08 16:56:48 -05:00
|
|
|
const response = await fetch('https://api.zoo.dev/user/shortlinks', {
|
2024-10-19 00:12:58 -04:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-type': 'application/json',
|
2025-01-08 12:00:19 -05:00
|
|
|
Authorization: `Bearer ${token}`,
|
2024-10-19 00:12:58 -04:00
|
|
|
},
|
2025-01-08 16:56:48 -05:00
|
|
|
body: JSON.stringify({
|
2025-01-10 12:57:16 -05:00
|
|
|
url,
|
2025-01-08 16:56:48 -05:00
|
|
|
// In future we can support org-scoped and password-protected shortlinks here
|
|
|
|
// https://zoo.dev/docs/api/shortlinks/create-a-shortlink-for-a-user?lang=typescript
|
2025-01-09 17:58:12 -05:00
|
|
|
}),
|
2025-01-08 16:56:48 -05:00
|
|
|
})
|
|
|
|
if (!response.ok) {
|
|
|
|
const error = await response.json()
|
|
|
|
return new Error(`Failed to create shortlink: ${error.message}`)
|
|
|
|
} else {
|
|
|
|
return response.json()
|
|
|
|
}
|
2024-10-08 12:32:47 -04:00
|
|
|
}
|