Fix existing: file renaming (and more things that spin out of settings file path parsing) (#3584)

* Fix the behavior so that we navigate to the new file path

* This change is done in other PRs but is also necessary here

* Add an Electron Playwright test for renaming a file

* Add tests for renaming dir, one is failing

* Don't need that console.warn

* Add DeepPartial utility type

* Fix settings parsing so that project path parsing is fixed

* Move URL check after DOM checks

* Revert this fallback behavior from https://github.com/KittyCAD/modeling-app/pull/3564 as we don't need it now that config parsing is fixed

* Make new bad prompt each run

* Fix onboarding asset path in web

* Remove double parsing of settings config

* Remove unused imports

* More unused imports

* Fix broken rename test

* Update src/lib/desktop.ts

Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>

* Add test for renaming file we do not have open

* fmt

---------

Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
This commit is contained in:
Frank Noirot
2024-08-20 22:16:44 -04:00
committed by GitHub
parent d14b8f5443
commit c09775f5eb
10 changed files with 472 additions and 91 deletions

View File

@ -21,6 +21,7 @@ import {
} from 'lib/desktop'
import { ProjectConfiguration } from 'wasm-lib/kcl/bindings/ProjectConfiguration'
import { BROWSER_PROJECT_NAME } from 'lib/constants'
import { DeepPartial } from 'lib/types'
/**
* Convert from a rust settings struct into the JS settings struct.
@ -28,8 +29,8 @@ import { BROWSER_PROJECT_NAME } from 'lib/constants'
* for hiding and showing settings.
**/
export function configurationToSettingsPayload(
configuration: Configuration
): Partial<SaveSettingsPayload> {
configuration: DeepPartial<Configuration>
): DeepPartial<SaveSettingsPayload> {
return {
app: {
theme: appThemeToTheme(configuration?.settings?.app?.appearance?.theme),
@ -66,8 +67,8 @@ export function configurationToSettingsPayload(
}
export function projectConfigurationToSettingsPayload(
configuration: ProjectConfiguration
): Partial<SaveSettingsPayload> {
configuration: DeepPartial<ProjectConfiguration>
): DeepPartial<SaveSettingsPayload> {
return {
app: {
theme: appThemeToTheme(configuration?.settings?.app?.appearance?.theme),
@ -106,7 +107,7 @@ function localStorageProjectSettingsPath() {
}
export function readLocalStorageAppSettingsFile():
| Partial<SaveSettingsPayload>
| DeepPartial<Configuration>
| Error {
// TODO: Remove backwards compatibility after a few releases.
let stored =
@ -132,7 +133,7 @@ export function readLocalStorageAppSettingsFile():
}
function readLocalStorageProjectSettingsFile():
| Partial<SaveSettingsPayload>
| DeepPartial<ProjectConfiguration>
| Error {
// TODO: Remove backwards compatibility after a few releases.
let stored = localStorage.getItem(localStorageProjectSettingsPath()) ?? ''
@ -156,7 +157,7 @@ function readLocalStorageProjectSettingsFile():
export interface AppSettings {
settings: ReturnType<typeof createSettings>
configuration: Partial<SaveSettingsPayload>
configuration: DeepPartial<Configuration>
}
export async function loadAndValidateSettings(
@ -175,7 +176,11 @@ export async function loadAndValidateSettings(
if (err(appSettingsPayload)) return Promise.reject(appSettingsPayload)
const settings = createSettings()
setSettingsAtLevel(settings, 'user', appSettingsPayload)
setSettingsAtLevel(
settings,
'user',
configurationToSettingsPayload(appSettingsPayload)
)
// Load the project settings if they exist
if (projectPath) {
@ -187,11 +192,18 @@ export async function loadAndValidateSettings(
return Promise.reject(new Error('Invalid project settings'))
const projectSettingsPayload = projectSettings
setSettingsAtLevel(settings, 'project', projectSettingsPayload)
setSettingsAtLevel(
settings,
'project',
projectConfigurationToSettingsPayload(projectSettingsPayload)
)
}
// Return the settings object
return { settings, configuration: appSettingsPayload }
return {
settings,
configuration: appSettingsPayload,
}
}
export async function saveSettings(
@ -204,21 +216,14 @@ export async function saveSettings(
// Get the user settings.
const jsAppSettings = getChangedSettingsAtLevel(allSettings, 'user')
const tomlString = tomlStringify({ settings: jsAppSettings })
if (err(tomlString)) return
// Parse this as a Configuration.
const appSettings = parseAppSettings(tomlString)
if (err(appSettings)) return
const tomlString2 = tomlStringify({ settings: appSettings })
if (err(tomlString2)) return
const appTomlString = tomlStringify({ settings: jsAppSettings })
if (err(appTomlString)) return
// Write the app settings.
if (onDesktop) {
await writeAppSettingsFile(appSettings)
await writeAppSettingsFile(appTomlString)
} else {
localStorage.setItem(localStorageAppSettingsPath(), tomlString2)
localStorage.setItem(localStorageAppSettingsPath(), appTomlString)
}
if (!projectPath) {
@ -231,19 +236,11 @@ export async function saveSettings(
const projectTomlString = tomlStringify({ settings: jsProjectSettings })
if (err(projectTomlString)) return
// Parse this as a Configuration.
const projectSettings = parseProjectSettings(projectTomlString)
if (err(projectSettings)) return
const tomlStr = tomlStringify(projectSettings)
if (err(tomlStr)) return
// Write the project settings.
if (onDesktop) {
await writeProjectSettingsFile(projectPath, projectSettings)
await writeProjectSettingsFile(projectPath, projectTomlString)
} else {
localStorage.setItem(localStorageProjectSettingsPath(), tomlStr)
localStorage.setItem(localStorageProjectSettingsPath(), projectTomlString)
}
}