Add "Share file" to command palette

This commit is contained in:
Frank Noirot
2025-01-17 11:24:59 -05:00
parent f90811695d
commit fb37bb83a8
2 changed files with 81 additions and 42 deletions

View File

@ -48,8 +48,9 @@ export const FileMachineProvider = ({
}) => { }) => {
const navigate = useNavigate() const navigate = useNavigate()
const { commandBarSend } = useCommandsContext() const { commandBarSend } = useCommandsContext()
const { settings } = useSettingsAuthContext() const { settings, auth } = useSettingsAuthContext()
const { project, file } = useRouteLoaderData(PATHS.FILE) as IndexLoaderData const projectData = useRouteLoaderData(PATHS.FILE) as IndexLoaderData
const { project, file } = projectData
const [kclSamples, setKclSamples] = React.useState<KclSamplesManifestItem[]>( const [kclSamples, setKclSamples] = React.useState<KclSamplesManifestItem[]>(
[] []
) )
@ -296,40 +297,47 @@ export const FileMachineProvider = ({
const kclCommandMemo = useMemo( const kclCommandMemo = useMemo(
() => () =>
kclCommands( kclCommands({
async (data) => { authToken: auth?.context?.token ?? '',
if (data.method === 'overwrite') { projectData,
codeManager.updateCodeStateEditor(data.code) settings: {
await kclManager.executeCode(true) defaultUnit: settings?.context?.modeling.defaultUnit.current ?? 'mm',
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,
},
})
}
}, },
kclSamples.map((sample) => ({ specialPropsForSampleCommand: {
value: sample.pathFromProjectDirectoryToFirstFile, onSubmit: async (data) => {
name: sample.title, if (data.method === 'overwrite') {
})) codeManager.updateCodeStateEditor(data.code)
).filter( 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' (command) => kclSamples.length || command.name !== 'open-kcl-example'
), ),
[codeManager, kclManager, send, kclSamples] [codeManager, kclManager, send, kclSamples]

View File

@ -1,12 +1,14 @@
import { CommandBarOverwriteWarning } from 'components/CommandBarOverwriteWarning' import { CommandBarOverwriteWarning } from 'components/CommandBarOverwriteWarning'
import { Command, CommandArgumentOption } from './commandTypes' import { Command, CommandArgumentOption } from './commandTypes'
import { kclManager } from './singletons' import { codeManager, kclManager } from './singletons'
import { isDesktop } from './isDesktop' import { isDesktop } from './isDesktop'
import { FILE_EXT, PROJECT_SETTINGS_FILE_NAME } from './constants' import { FILE_EXT, PROJECT_SETTINGS_FILE_NAME } from './constants'
import { UnitLength_type } from '@kittycad/lib/dist/types/src/models' import { UnitLength_type } from '@kittycad/lib/dist/types/src/models'
import { parseProjectSettings } from 'lang/wasm' import { parseProjectSettings } from 'lang/wasm'
import { err, reportRejection } from './trap' import { err, reportRejection } from './trap'
import { projectConfigurationToSettingsPayload } from './settings/settingsUtils' import { projectConfigurationToSettingsPayload } from './settings/settingsUtils'
import { copyFileShareLink } from './links'
import { IndexLoaderData } from './types'
interface OnSubmitProps { interface OnSubmitProps {
sampleName: string sampleName: string
@ -15,10 +17,21 @@ interface OnSubmitProps {
method: 'overwrite' | 'newFile' method: 'overwrite' | 'newFile'
} }
export function kclCommands( interface KclCommandConfig {
onSubmit: (p: OnSubmitProps) => Promise<void>, // TODO: find a different approach that doesn't require
providedOptions: CommandArgumentOption<string>[] // special props for a single command
): Command[] { specialPropsForSampleCommand: {
onSubmit: (p: OnSubmitProps) => Promise<void>
providedOptions: CommandArgumentOption<string>[]
}
projectData: IndexLoaderData
authToken: string
settings: {
defaultUnit: UnitLength_type
}
}
export function kclCommands(commandProps: KclCommandConfig): Command[] {
return [ return [
{ {
name: 'format-code', name: 'format-code',
@ -107,7 +120,9 @@ export function kclCommands(
) )
.then((props) => { .then((props) => {
if (props?.code) { if (props?.code) {
onSubmit(props).catch(reportError) commandProps.specialPropsForSampleCommand
.onSubmit(props)
.catch(reportError)
} }
}) })
.catch(reportError) .catch(reportError)
@ -149,9 +164,25 @@ export function kclCommands(
} }
return value 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)
},
},
] ]
} }