From f90811695dd61bde8cec991b25d549f3677a3577 Mon Sep 17 00:00:00 2001 From: Frank Noirot Date: Fri, 17 Jan 2025 11:24:38 -0500 Subject: [PATCH] Refactor: break out `copyFileShareLink` into standalone function --- src/components/ProjectSidebarMenu.tsx | 36 +++------------------------ src/lib/links.ts | 31 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/components/ProjectSidebarMenu.tsx b/src/components/ProjectSidebarMenu.tsx index 7f97d4559..724aa4e98 100644 --- a/src/components/ProjectSidebarMenu.tsx +++ b/src/components/ProjectSidebarMenu.tsx @@ -15,10 +15,8 @@ import { MachineManagerContext } from 'components/MachineManagerProvider' import usePlatform from 'hooks/usePlatform' import { useAbsoluteFilePath } from 'hooks/useAbsoluteFilePath' import Tooltip from './Tooltip' -import { createCreateFileUrl, createShortlink } from 'lib/links' +import { copyFileShareLink } from 'lib/links' import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext' -import toast from 'react-hot-toast' -import { err } from 'lib/trap' const ProjectSidebarMenu = ({ project, @@ -190,40 +188,12 @@ function ProjectMenuPopover({ Element: 'button', children: 'Share link to file', onClick: async () => { - /** - * We don't have a dev shortlink API service, - * 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({ + await copyFileShareLink({ + token: auth?.context.token || '', code: codeManager.code, name: project?.name || '', 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', diff --git a/src/lib/links.ts b/src/lib/links.ts index 2cbc76340..4e00708c0 100644 --- a/src/lib/links.ts +++ b/src/lib/links.ts @@ -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 { stringToBase64 } from './base64' 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 { code: string name: string 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 * the "Import file from URL" command in the app.