diff --git a/src/components/FileMachineProvider.tsx b/src/components/FileMachineProvider.tsx index ca0c1a4d4..fc7f05005 100644 --- a/src/components/FileMachineProvider.tsx +++ b/src/components/FileMachineProvider.tsx @@ -48,8 +48,9 @@ export const FileMachineProvider = ({ }) => { const navigate = useNavigate() const { commandBarSend } = useCommandsContext() - const { settings } = useSettingsAuthContext() - const { project, file } = useRouteLoaderData(PATHS.FILE) as IndexLoaderData + const { settings, auth } = useSettingsAuthContext() + const projectData = useRouteLoaderData(PATHS.FILE) as IndexLoaderData + const { project, file } = projectData const [kclSamples, setKclSamples] = React.useState( [] ) @@ -296,40 +297,47 @@ export const FileMachineProvider = ({ const kclCommandMemo = useMemo( () => - kclCommands( - async (data) => { - if (data.method === 'overwrite') { - codeManager.updateCodeStateEditor(data.code) - await kclManager.executeCode(true) - await codeManager.writeToFile() - } else if (data.method === 'newFile' && isDesktop()) { - send({ - type: 'Create file', - data: { - name: data.sampleName, - content: data.code, - makeDir: false, - }, - }) - } - - // Either way, we want to overwrite the defaultUnit project setting - // with the sample's setting. - if (data.sampleUnits) { - settings.send({ - type: 'set.modeling.defaultUnit', - data: { - level: 'project', - value: data.sampleUnits, - }, - }) - } + kclCommands({ + authToken: auth?.context?.token ?? '', + projectData, + settings: { + defaultUnit: settings?.context?.modeling.defaultUnit.current ?? 'mm', }, - kclSamples.map((sample) => ({ - value: sample.pathFromProjectDirectoryToFirstFile, - name: sample.title, - })) - ).filter( + specialPropsForSampleCommand: { + onSubmit: async (data) => { + if (data.method === 'overwrite') { + codeManager.updateCodeStateEditor(data.code) + await kclManager.executeCode(true) + await codeManager.writeToFile() + } else if (data.method === 'newFile' && isDesktop()) { + send({ + type: 'Create file', + data: { + name: data.sampleName, + content: data.code, + makeDir: false, + }, + }) + } + + // Either way, we want to overwrite the defaultUnit project setting + // with the sample's setting. + if (data.sampleUnits) { + settings.send({ + type: 'set.modeling.defaultUnit', + data: { + level: 'project', + value: data.sampleUnits, + }, + }) + } + }, + providedOptions: kclSamples.map((sample) => ({ + value: sample.pathFromProjectDirectoryToFirstFile, + name: sample.title, + })), + }, + }).filter( (command) => kclSamples.length || command.name !== 'open-kcl-example' ), [codeManager, kclManager, send, kclSamples] diff --git a/src/lib/kclCommands.ts b/src/lib/kclCommands.ts index ba0fb4db0..9e7dda170 100644 --- a/src/lib/kclCommands.ts +++ b/src/lib/kclCommands.ts @@ -1,12 +1,14 @@ import { CommandBarOverwriteWarning } from 'components/CommandBarOverwriteWarning' import { Command, CommandArgumentOption } from './commandTypes' -import { kclManager } from './singletons' +import { codeManager, kclManager } from './singletons' import { isDesktop } from './isDesktop' import { FILE_EXT, PROJECT_SETTINGS_FILE_NAME } from './constants' import { UnitLength_type } from '@kittycad/lib/dist/types/src/models' import { parseProjectSettings } from 'lang/wasm' import { err, reportRejection } from './trap' import { projectConfigurationToSettingsPayload } from './settings/settingsUtils' +import { copyFileShareLink } from './links' +import { IndexLoaderData } from './types' interface OnSubmitProps { sampleName: string @@ -15,10 +17,21 @@ interface OnSubmitProps { method: 'overwrite' | 'newFile' } -export function kclCommands( - onSubmit: (p: OnSubmitProps) => Promise, - providedOptions: CommandArgumentOption[] -): Command[] { +interface KclCommandConfig { + // TODO: find a different approach that doesn't require + // special props for a single command + specialPropsForSampleCommand: { + onSubmit: (p: OnSubmitProps) => Promise + providedOptions: CommandArgumentOption[] + } + projectData: IndexLoaderData + authToken: string + settings: { + defaultUnit: UnitLength_type + } +} + +export function kclCommands(commandProps: KclCommandConfig): Command[] { return [ { name: 'format-code', @@ -107,7 +120,9 @@ export function kclCommands( ) .then((props) => { if (props?.code) { - onSubmit(props).catch(reportError) + commandProps.specialPropsForSampleCommand + .onSubmit(props) + .catch(reportError) } }) .catch(reportError) @@ -149,9 +164,25 @@ export function kclCommands( } return value }, - options: providedOptions, + options: commandProps.specialPropsForSampleCommand.providedOptions, }, }, }, + { + name: 'share-file-link', + displayName: 'Share file', + description: 'Create a link that contains a copy of the current file.', + groupId: 'code', + needsReview: false, + icon: 'link', + onSubmit: () => { + copyFileShareLink({ + token: commandProps.authToken, + code: codeManager.code, + name: commandProps.projectData.project?.name || '', + units: commandProps.settings.defaultUnit, + }).catch(reportRejection) + }, + }, ] }