chore: implementing import file from url which is not actually that?

This commit is contained in:
Kevin Nadro
2025-04-21 12:58:12 -05:00
parent e77b42f308
commit fbf94aaba9
7 changed files with 158 additions and 4 deletions

View File

@ -1,3 +1,27 @@
import { useEffect } from 'react'
import { commandBarActor } from '@src/machines/commandBarMachine'
import { importFileFromURL } from '@src/lib/commandBarConfigs/projectsCommandConfig'
export function SystemIOMachineLogicListenerWeb() {
const useAddProjectCommandsToCommandBar = () => {
const commands = [importFileFromURL]
useEffect(() => {
commandBarActor.send({
type: 'Add commands',
data: {
commands: commands,
},
})
return () => {
commandBarActor.send({
type: 'Remove commands',
data: {
commands: commands,
},
})
}
}, [])
}
useAddProjectCommandsToCommandBar()
return null
}

View File

@ -63,6 +63,7 @@ export function useCreateFileLinkQuery(
method: isDesktop() ? undefined : 'existingProject',
}
console.log('nice callback?')
callback(argDefaultValues)
}
}, [searchParams, immediateState])

View File

@ -320,11 +320,100 @@ export const renameProjectCommand: Command = {
},
},
}
// export const importFileFromURL: Command = {}
export const importFileFromURL: Command = {
name: 'Import file from URL',
groupId: 'projects',
icon: 'file',
description: 'Create a file',
needsReview: true,
onSubmit: (record) => {
console.log('RECORD!', record)
if (record) {
systemIOActor.send({
type: SystemIOMachineEvents.createKCLFile,
data: {
requestedProjectName: record.projectName,
requestedCode: record.code,
requestedFileName: record.name,
},
})
}
},
args: {
method: {
inputType: 'options',
required: true,
skip: true,
options: isDesktop()
? [
{ name: 'New project', value: 'newProject' },
{ name: 'Existing project', value: 'existingProject' },
]
: [{ name: 'Overwrite', value: 'existingProject' }],
valueSummary(value) {
return isDesktop()
? value === 'newProject'
? 'New project'
: 'Existing project'
: 'Overwrite'
},
},
// TODO: We can't get the currently-opened project to auto-populate here because
// it's not available on projectMachine, but lower in fileMachine. Unify these.
projectName: {
inputType: 'options',
required: (commandsContext) =>
isDesktop() &&
commandsContext.argumentsToSubmit.method === 'existingProject',
skip: true,
options: (_, context) => {
const folders = folderSnapshot()
const options: CommandArgumentOption<any>[] = []
folders.forEach((folder) => {
options.push({
name: folder.name,
value: folder.name,
isCurrent: false,
})
})
return options
},
},
name: {
inputType: 'string',
required: isDesktop(),
skip: true,
},
code: {
inputType: 'text',
required: true,
skip: true,
valueSummary(value) {
const lineCount = value?.trim().split('\n').length
return `${lineCount} line${lineCount === 1 ? '' : 's'}`
},
},
},
reviewMessage(commandBarContext) {
return isDesktop()
? `Will add the contents from URL to a new ${
commandBarContext.argumentsToSubmit.method === 'newProject'
? 'project with file main.kcl'
: `file within the project "${commandBarContext.argumentsToSubmit.projectName}"`
} named "${
commandBarContext.argumentsToSubmit.name
}", and set default units to "${
commandBarContext.argumentsToSubmit.units
}".`
: `Will overwrite the contents of the current file with the contents from the URL.`
},
}
export const projectCommands = [
openProjectCommand,
createProjectCommand,
deleteProjectCommand,
renameProjectCommand,
importFileFromURL,
]

View File

@ -191,7 +191,7 @@ export function readLocalStorageAppSettingsFile():
}
}
function readLocalStorageProjectSettingsFile():
export function readLocalStorageProjectSettingsFile():
| DeepPartial<ProjectConfiguration>
| Error {
// TODO: Remove backwards compatibility after a few releases.

View File

@ -183,7 +183,13 @@ export const systemIOMachine = setup({
requestedFileName: string
requestedCode: string
}
}) => {}
}): Promise<{
message: string
fileName: string
projectName: string
}> => {
return { message: '', fileName: '', projectName: '' }
}
),
[SystemIOMachineActors.checkReadWrite]: fromPromise(
async ({

View File

@ -193,6 +193,12 @@ export const systemIOMachineDesktop = systemIOMachine.provide({
configuration,
newFileName
)
return {
message: 'File created successfully',
fileName: input.requestedFileName,
projectName: requestedProjectName,
}
}
),
[SystemIOMachineActors.checkReadWrite]: fromPromise(

View File

@ -2,6 +2,11 @@ import { systemIOMachine } from '@src/machines/systemIO/systemIOMachine'
import type { SystemIOContext } from '@src/machines/systemIO/utils'
import { SystemIOMachineActors } from '@src/machines/systemIO/utils'
import { fromPromise } from 'xstate'
import { newKclFile } from '@src/lang/project'
import { readLocalStorageProjectSettingsFile } from '@src/lib/settings/settingsUtils'
import { err } from '@src/lib/trap'
import { DEFAULT_DEFAULT_LENGTH_UNIT } from '@src/lib/constants'
import { codeManager, kclManager } from '@src/lib/singletons'
export const systemIOMachineWeb = systemIOMachine.provide({
actors: {
@ -15,7 +20,30 @@ export const systemIOMachineWeb = systemIOMachine.provide({
requestedFileName: string
requestedCode: string
}
}) => {}
}) => {
// Browser version doesn't navigate, just overwrites the current file
// clearImportSearchParams()
const projectSettings = readLocalStorageProjectSettingsFile()
if (err(projectSettings)) {
return Promise.reject(
'Unable to read project settings from local storage'
)
}
const codeToWrite = newKclFile(
input.requestedCode,
projectSettings?.settings?.modeling?.base_unit ||
DEFAULT_DEFAULT_LENGTH_UNIT
)
if (err(codeToWrite)) return Promise.reject(codeToWrite)
codeManager.updateCodeStateEditor(codeToWrite)
await codeManager.writeToFile()
await kclManager.executeCode()
return {
message: 'File overwritten successfully',
fileName: input.requestedFileName,
projectName: '',
}
}
),
},
})