Rearchitect settings system to be scoped (#1956)
* BROKEN: start of scopes for each setting * Clean up later: mostly-functional scoped settings! Broken command bar, unimplemented generated settings components * Working persisted project settings in-folder * Start working toward automatic commands and settings UI * Relatively stable, settings-menu-editable * Settings persistence tweaks after merge * Custom settings UI working properly, cleaner types * Allow boolean command types, create Settings UI for them * Add support for option and string Settings input types * Proof of concept settings from command bar * Add all settings to command bar * Allow settings to be hidden on a level * Better command titles for settings * Hide the settings the settings from the commands bar * Derive command defaultValue from *current* settingsMachine context * Fix generated settings UI for 'options' type settings * Pretty settings modal 💅 * Allow for rollback to parent level setting * fmt * Fix tsc errors not related to loading from localStorage * Better setting descriptions, better buttons * Make displayName searchable in command bar * Consolidate constants, get working in browser * Start fixing tests, better types for saved settings payloads * Fix playwright tests * Add a test for the settings modal * Add AtLeast to codespell ignore list * Goofed merge of codespellrc * Try fixing linux E2E tests * Make codespellrc word lowercase * fmt * Fix data-testid in Tauri test * Don't set text settings if nothing changed * Turn off unimplemented settings * Allow for multiple "execution-done" messages to have appeared in snapshot tests * Try fixing up snapshot tests * Switch from .json to .toml settings file format * Use a different method for overriding the default units * Try to force using the new common storage state in snapshot tests * Update tests to use TOML * fmt and remove console logs * Restore units to export * tsc errors, make snapshot tests use TOML * Ensure that snapshot tests use the basicStorageState * Re-organize use of test.use() * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * Update snapshots one more time since lighting changed * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * Fix broken "Show in folder" for project-level settings * Fire all relevant actions after settings reset * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * Properly reset the default directory * Hide settings by platform * Actually honor showDebugPanel * Unify settings hiding logic * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fix first extrusion snapshot * another attempt to fix extrustion snapshot * Rerun test suite * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * trigger CI * more extrusion stuff * Replace resetSettings console log with comment --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Kurt Hutten Irev-Dev <k.hutten@protonmail.ch>
This commit is contained in:
@ -1,14 +1,11 @@
|
||||
import { BROWSER_FILE_NAME } from 'Router'
|
||||
import { type IndexLoaderData } from 'lib/types'
|
||||
import { paths } from 'lib/paths'
|
||||
import { BROWSER_PATH, paths } from 'lib/paths'
|
||||
import { useRouteLoaderData } from 'react-router-dom'
|
||||
|
||||
export function useAbsoluteFilePath() {
|
||||
const routeData = useRouteLoaderData(paths.FILE) as IndexLoaderData
|
||||
|
||||
return (
|
||||
paths.FILE +
|
||||
'/' +
|
||||
encodeURIComponent(routeData?.file?.path || BROWSER_FILE_NAME)
|
||||
paths.FILE + '/' + encodeURIComponent(routeData?.file?.path || BROWSER_PATH)
|
||||
)
|
||||
}
|
||||
|
28
src/hooks/useRefreshSettings.ts
Normal file
28
src/hooks/useRefreshSettings.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { useRouteLoaderData } from 'react-router-dom'
|
||||
import { useSettingsAuthContext } from './useSettingsAuthContext'
|
||||
import { paths } from 'lib/paths'
|
||||
import { settings } from 'lib/settings/initialSettings'
|
||||
import { useEffect } from 'react'
|
||||
|
||||
/**
|
||||
* I was dismayed to learn that index route in Router.tsx where we initially load up the settings
|
||||
* doesn't re-run on subsequent navigations. This hook is a workaround,
|
||||
* in conjunction with additional uses of settingsLoader further down the router tree.
|
||||
* @param routeId - The id defined in Router.tsx to load the settings from.
|
||||
*/
|
||||
export function useRefreshSettings(routeId: string = paths.INDEX) {
|
||||
const ctx = useSettingsAuthContext()
|
||||
const routeData = useRouteLoaderData(routeId) as typeof settings
|
||||
|
||||
if (!ctx) {
|
||||
throw new Error(
|
||||
'useRefreshSettings must be used within a SettingsAuthProvider'
|
||||
)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
ctx.settings.send('Set all settings', {
|
||||
settings: routeData,
|
||||
})
|
||||
}, [])
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
import { validateSettings } from 'lib/settings/settingsUtils'
|
||||
import { useEffect } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
import { useRouteLoaderData } from 'react-router-dom'
|
||||
import { useSettingsAuthContext } from './useSettingsAuthContext'
|
||||
import { paths } from 'lib/paths'
|
||||
|
||||
// This hook must only be used within a descendant of the SettingsAuthProvider component
|
||||
// (and, by extension, the Router component).
|
||||
// Specifically it relies on the Router's indexLoader data and the settingsMachine send function.
|
||||
// for the settings and validation errors to be available.
|
||||
export function useValidateSettings() {
|
||||
const {
|
||||
settings: { send },
|
||||
} = useSettingsAuthContext()
|
||||
const { settings, errors } = useRouteLoaderData(paths.INDEX) as Awaited<
|
||||
ReturnType<typeof validateSettings>
|
||||
>
|
||||
|
||||
// If there were validation errors either from local storage or from the file,
|
||||
// log them to the console and show a toast message to the user.
|
||||
useEffect(() => {
|
||||
if (errors.length > 0) {
|
||||
send('Set All Settings', settings)
|
||||
const errorMessage =
|
||||
'Error validating persisted settings: ' +
|
||||
errors.join(', ') +
|
||||
'. Using defaults.'
|
||||
console.error(errorMessage)
|
||||
toast.error(errorMessage)
|
||||
}
|
||||
}, [errors])
|
||||
}
|
Reference in New Issue
Block a user