Refactor: break out copyFileShareLink
into standalone function
This commit is contained in:
@ -15,10 +15,8 @@ import { MachineManagerContext } from 'components/MachineManagerProvider'
|
|||||||
import usePlatform from 'hooks/usePlatform'
|
import usePlatform from 'hooks/usePlatform'
|
||||||
import { useAbsoluteFilePath } from 'hooks/useAbsoluteFilePath'
|
import { useAbsoluteFilePath } from 'hooks/useAbsoluteFilePath'
|
||||||
import Tooltip from './Tooltip'
|
import Tooltip from './Tooltip'
|
||||||
import { createCreateFileUrl, createShortlink } from 'lib/links'
|
import { copyFileShareLink } from 'lib/links'
|
||||||
import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext'
|
import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext'
|
||||||
import toast from 'react-hot-toast'
|
|
||||||
import { err } from 'lib/trap'
|
|
||||||
|
|
||||||
const ProjectSidebarMenu = ({
|
const ProjectSidebarMenu = ({
|
||||||
project,
|
project,
|
||||||
@ -190,40 +188,12 @@ function ProjectMenuPopover({
|
|||||||
Element: 'button',
|
Element: 'button',
|
||||||
children: 'Share link to file',
|
children: 'Share link to file',
|
||||||
onClick: async () => {
|
onClick: async () => {
|
||||||
/**
|
await copyFileShareLink({
|
||||||
* We don't have a dev shortlink API service,
|
token: auth?.context.token || '',
|
||||||
* so we need to hit the prod API even in local dev.
|
|
||||||
* This override allows us to shim in an environment variable
|
|
||||||
* for the prod token.
|
|
||||||
*/
|
|
||||||
const token = auth.context.token
|
|
||||||
if (!token) {
|
|
||||||
toast.error('You need to be signed in to share a file.', {
|
|
||||||
duration: 5000,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const shareUrl = createCreateFileUrl({
|
|
||||||
code: codeManager.code,
|
code: codeManager.code,
|
||||||
name: project?.name || '',
|
name: project?.name || '',
|
||||||
units: settings.context.modeling.defaultUnit.current,
|
units: settings.context.modeling.defaultUnit.current,
|
||||||
})
|
})
|
||||||
const shortlink = await createShortlink(token, shareUrl.toString())
|
|
||||||
|
|
||||||
if (err(shortlink)) {
|
|
||||||
toast.error(shortlink.message, {
|
|
||||||
duration: 5000,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
await globalThis.navigator.clipboard.writeText(shortlink.url)
|
|
||||||
toast.success(
|
|
||||||
'Link copied to clipboard. Anyone who clicks this link will get a copy of this file. Share carefully!',
|
|
||||||
{
|
|
||||||
duration: 5000,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'break',
|
'break',
|
||||||
|
@ -2,12 +2,43 @@ import { UnitLength_type } from '@kittycad/lib/dist/types/src/models'
|
|||||||
import { ASK_TO_OPEN_QUERY_PARAM, CREATE_FILE_URL_PARAM } from './constants'
|
import { ASK_TO_OPEN_QUERY_PARAM, CREATE_FILE_URL_PARAM } from './constants'
|
||||||
import { stringToBase64 } from './base64'
|
import { stringToBase64 } from './base64'
|
||||||
import { DEV, VITE_KC_SITE_BASE_URL, VITE_KC_API_BASE_URL } from 'env'
|
import { DEV, VITE_KC_SITE_BASE_URL, VITE_KC_API_BASE_URL } from 'env'
|
||||||
|
import toast from 'react-hot-toast'
|
||||||
|
import { err } from './trap'
|
||||||
export interface FileLinkParams {
|
export interface FileLinkParams {
|
||||||
code: string
|
code: string
|
||||||
name: string
|
name: string
|
||||||
units: UnitLength_type
|
units: UnitLength_type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function copyFileShareLink(
|
||||||
|
args: FileLinkParams & { token: string }
|
||||||
|
) {
|
||||||
|
const token = args.token
|
||||||
|
if (!token) {
|
||||||
|
toast.error('You need to be signed in to share a file.', {
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const shareUrl = createCreateFileUrl(args)
|
||||||
|
const shortlink = await createShortlink(token, shareUrl.toString())
|
||||||
|
|
||||||
|
if (err(shortlink)) {
|
||||||
|
toast.error(shortlink.message, {
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
await globalThis.navigator.clipboard.writeText(shortlink.url)
|
||||||
|
toast.success(
|
||||||
|
'Link copied to clipboard. Anyone who clicks this link will get a copy of this file. Share carefully!',
|
||||||
|
{
|
||||||
|
duration: 5000,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a URL with the necessary query parameters to trigger
|
* Creates a URL with the necessary query parameters to trigger
|
||||||
* the "Import file from URL" command in the app.
|
* the "Import file from URL" command in the app.
|
||||||
|
Reference in New Issue
Block a user