Project settings load properly

This commit is contained in:
49lf
2024-07-31 11:15:32 -04:00
parent 1a1e358238
commit 4cc06e0efc
6 changed files with 38 additions and 39 deletions

View File

@ -28,8 +28,8 @@ import { CoreDumpManager } from 'lib/coredump'
import { UnitsMenu } from 'components/UnitsMenu'
export function App() {
useRefreshSettings(paths.FILE + 'SETTINGS')
const { project, file } = useLoaderData() as IndexLoaderData
useRefreshSettings(paths.FILE + 'SETTINGS')
const navigate = useNavigate()
const filePath = useAbsoluteFilePath()
const { onProjectOpen } = useLspContext()

View File

@ -209,11 +209,11 @@ export const SettingsAuthProviderBase = ({
},
services: {
'Persist settings': (context) =>
saveSettings(context, loadedProject?.path),
saveSettings(context, loadedProject?.project.path),
},
}
)
settingsStateRef = settingsState.context
settingsStateRef = settingsState.context.settings ?? settingsState.context
// Add settings commands to the command bar
// They're treated slightly differently than other commands

View File

@ -41,7 +41,7 @@ export const settingsLoader: LoaderFunction = async ({
const { settings: s } = await loadAndValidateSettings(
project_path || undefined
)
settings = s
return s
}
}
@ -71,9 +71,8 @@ export const onboardingRedirectLoader: ActionFunction = async (args) => {
return settingsLoader(args)
}
export const fileLoader: LoaderFunction = async ({
params,
}): Promise<FileLoaderData | Response> => {
export const fileLoader: LoaderFunction = async (routerData): Promise<FileLoaderData | Response> => {
const { params } = routerData
let { configuration } = await loadAndValidateSettings()
const projectPathData = await getProjectMetaByRouteId(
@ -86,27 +85,31 @@ export const fileLoader: LoaderFunction = async ({
const { project_name, project_path, current_file_name, current_file_path } =
projectPathData
if (!current_file_name || !current_file_path || !project_name) {
return redirect(
`${paths.FILE}/${encodeURIComponent(
`${params.id}${isDesktop() ? window.electron.path.sep : '/'}${PROJECT_ENTRYPOINT}`
)}`
)
const urlObj = URL.parse(routerData.request.url)
let code = ''
if (!urlObj.pathname.endsWith('/settings')) {
// TODO: PROJECT_ENTRYPOINT is hardcoded
// until we support setting a project's entrypoint file
if (!current_file_name || !current_file_path || !project_name) {
return redirect(
`${paths.FILE}/${encodeURIComponent(
`${params.id}${isDesktop() ? window.electron.path.sep : '/'}${PROJECT_ENTRYPOINT}`
)}`
)
}
code = await window.electron.readFile(current_file_path)
// Update both the state and the editor's code.
// We explicitly do not write to the file here since we are loading from
// the file system and not the editor.
codeManager.updateCurrentFilePath(current_file_path)
codeManager.updateCodeStateEditor(code)
// We don't want to call await on execute code since we don't want to block the UI
kclManager.executeCode(true)
}
// TODO: PROJECT_ENTRYPOINT is hardcoded
// until we support setting a project's entrypoint file
const code = await window.electron.readFile(current_file_path)
// Update both the state and the editor's code.
// We explicitly do not write to the file here since we are loading from
// the file system and not the editor.
codeManager.updateCurrentFilePath(current_file_path)
codeManager.updateCodeStateEditor(code)
// We don't want to call await on execute code since we don't want to block the UI
kclManager.executeCode(true)
// Set the file system manager to the project path
// So that WASM gets an updated path for operations
fileSystemManager.dir = project_path
@ -126,7 +129,7 @@ export const fileLoader: LoaderFunction = async ({
},
file: {
name: current_file_name,
path: current_file_path.split('/').slice(0, -1).join('/'),
path: current_file_path?.split('/').slice(0, -1).join('/'),
children: [],
},
}

View File

@ -158,12 +158,11 @@ export interface AppSettings {
export async function loadAndValidateSettings(
projectPath?: string
): Promise<AppSettings> {
const settings = createSettings()
const onDesktop = isDesktop()
// Make sure we have wasm initialized.
await initPromise
const onDesktop = isDesktop()
// Load the app settings from the file system or localStorage.
const appSettingsPayload = onDesktop
? await readAppSettingsFile()
@ -171,6 +170,7 @@ export async function loadAndValidateSettings(
if (err(appSettingsPayload)) return Promise.reject(appSettingsPayload)
const settings = createSettings()
setSettingsAtLevel(settings, 'user', appSettingsPayload)
// Load the project settings if they exist
@ -182,8 +182,7 @@ export async function loadAndValidateSettings(
if (err(projectSettings))
return Promise.reject(new Error('Invalid project settings'))
const projectSettingsPayload =
projectConfigurationToSettingsPayload(projectSettings)
const projectSettingsPayload = projectSettings
setSettingsAtLevel(settings, 'project', projectSettingsPayload)
}
@ -238,7 +237,7 @@ export async function saveSettings(
// Write the project settings.
if (onDesktop) {
await writeProjectSettingsFile(projectPath, projectSettings)
await writeProjectSettingsFile({ projectPath, configuration: { settings: projectSettings }})
} else {
localStorage.setItem(localStorageProjectSettingsPath(), tomlStr)
}

View File

@ -89,8 +89,6 @@ export const settingsMachine = createMachine(
},
'Set all settings': {
target: 'persisting settings',
actions: [
'setAllSettings',
'setThemeClass',

View File

@ -24,7 +24,6 @@ import {
import useStateMachineCommands from '../hooks/useStateMachineCommands'
import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext'
import { useCommandsContext } from 'hooks/useCommandsContext'
import { join, sep } from '@tauri-apps/api/path'
import { homeCommandBarConfig } from 'lib/commandBarConfigs/homeCommandConfig'
import { useHotkeys } from 'react-hotkeys-hook'
import { isDesktop } from 'lib/isDesktop'
@ -40,13 +39,13 @@ import {
} from 'lib/desktop'
import { ProjectSearchBar, useProjectSearch } from 'components/ProjectSearchBar'
// This route only opens in the Tauri desktop context for now,
// as defined in Router.tsx, so we can use the Tauri APIs and types.
// This route only opens in the desktop context for now,
// as defined in Router.tsx, so we can use the desktop APIs and types.
const Home = () => {
const { projects: loadedProjects } = useLoaderData() as HomeLoaderData
useRefreshSettings(paths.HOME + 'SETTINGS')
const { commandBarSend } = useCommandsContext()
const navigate = useNavigate()
const { projects: loadedProjects } = useLoaderData() as HomeLoaderData
const {
settings: { context: settings },
} = useSettingsAuthContext()