2023-08-29 10:48:55 -04:00
|
|
|
import { useMachine } from '@xstate/react'
|
|
|
|
import { useNavigate } from 'react-router-dom'
|
2024-02-11 12:59:00 +11:00
|
|
|
import { paths } from 'lib/paths'
|
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
|
|
|
import { authMachine, TOKEN_PERSIST_KEY } from '../machines/authMachine'
|
2023-08-29 10:48:55 -04:00
|
|
|
import withBaseUrl from '../lib/withBaseURL'
|
|
|
|
import React, { createContext, useEffect, useRef } from 'react'
|
|
|
|
import useStateMachineCommands from '../hooks/useStateMachineCommands'
|
2024-02-15 14:14:14 -05:00
|
|
|
import { settingsMachine } from 'machines/settingsMachine'
|
|
|
|
import {
|
|
|
|
initialSettings,
|
|
|
|
SETTINGS_PERSIST_KEY,
|
|
|
|
validateSettings,
|
|
|
|
} from 'lib/settings'
|
2023-08-29 10:48:55 -04:00
|
|
|
import { toast } from 'react-hot-toast'
|
2023-08-31 09:34:13 -04:00
|
|
|
import { setThemeClass, Themes } from 'lib/theme'
|
2023-08-29 10:48:55 -04:00
|
|
|
import {
|
|
|
|
AnyStateMachine,
|
|
|
|
ContextFrom,
|
|
|
|
InterpreterFrom,
|
|
|
|
Prop,
|
|
|
|
StateFrom,
|
|
|
|
} from 'xstate'
|
2023-09-14 19:31:16 -04:00
|
|
|
import { isTauri } from 'lib/isTauri'
|
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
|
|
|
import { settingsCommandBarConfig } from 'lib/commandBarConfigs/settingsCommandConfig'
|
|
|
|
import { authCommandBarConfig } from 'lib/commandBarConfigs/authCommandConfig'
|
2024-02-15 14:14:14 -05:00
|
|
|
import { initializeProjectDirectory, readSettingsFile } from 'lib/tauriFS'
|
2023-08-29 10:48:55 -04:00
|
|
|
|
|
|
|
type MachineContext<T extends AnyStateMachine> = {
|
|
|
|
state: StateFrom<T>
|
|
|
|
context: ContextFrom<T>
|
|
|
|
send: Prop<InterpreterFrom<T>, 'send'>
|
|
|
|
}
|
|
|
|
|
2024-02-15 14:14:14 -05:00
|
|
|
type SettingsAuthContext = {
|
2023-08-29 10:48:55 -04:00
|
|
|
auth: MachineContext<typeof authMachine>
|
|
|
|
settings: MachineContext<typeof settingsMachine>
|
|
|
|
}
|
|
|
|
|
2024-02-15 14:14:14 -05:00
|
|
|
export const SettingsAuthStateContext = createContext({} as SettingsAuthContext)
|
2023-08-29 10:48:55 -04:00
|
|
|
|
2024-02-15 14:14:14 -05:00
|
|
|
export const SettingsAuthStateProvider = ({
|
2023-08-29 10:48:55 -04:00
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
}) => {
|
|
|
|
const navigate = useNavigate()
|
|
|
|
|
|
|
|
// Settings machine setup
|
2024-02-15 14:14:14 -05:00
|
|
|
// Load settings from local storage
|
|
|
|
// and validate them
|
2023-08-29 10:48:55 -04:00
|
|
|
const retrievedSettings = useRef(
|
2024-02-15 14:14:14 -05:00
|
|
|
validateSettings(
|
|
|
|
JSON.parse(localStorage?.getItem(SETTINGS_PERSIST_KEY) || '{}')
|
|
|
|
)
|
2023-08-29 10:48:55 -04:00
|
|
|
)
|
|
|
|
const persistedSettings = Object.assign(
|
2024-02-15 14:14:14 -05:00
|
|
|
{},
|
|
|
|
initialSettings,
|
|
|
|
retrievedSettings.current.settings
|
2023-08-29 10:48:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const [settingsState, settingsSend] = useMachine(settingsMachine, {
|
|
|
|
context: persistedSettings,
|
|
|
|
actions: {
|
|
|
|
toastSuccess: (context, event) => {
|
|
|
|
const truncatedNewValue =
|
|
|
|
'data' in event && event.data instanceof Object
|
|
|
|
? (context[Object.keys(event.data)[0] as keyof typeof context]
|
|
|
|
.toString()
|
|
|
|
.substring(0, 28) as any)
|
|
|
|
: undefined
|
|
|
|
toast.success(
|
|
|
|
event.type +
|
|
|
|
(truncatedNewValue
|
|
|
|
? ` to "${truncatedNewValue}${
|
|
|
|
truncatedNewValue.length === 28 ? '...' : ''
|
|
|
|
}"`
|
|
|
|
: '')
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-02-15 14:14:14 -05:00
|
|
|
// If the app is running in the Tauri context,
|
|
|
|
// try to read the settings from a file
|
|
|
|
// after doing some validation on them
|
|
|
|
useEffect(() => {
|
|
|
|
async function getFileBasedSettings() {
|
|
|
|
if (isTauri()) {
|
|
|
|
const newSettings = await readSettingsFile()
|
|
|
|
|
|
|
|
if (newSettings) {
|
|
|
|
if (newSettings.defaultDirectory) {
|
|
|
|
const newDefaultDirectory = await initializeProjectDirectory(
|
|
|
|
newSettings.defaultDirectory || ''
|
|
|
|
)
|
|
|
|
if (newDefaultDirectory.error !== null) {
|
|
|
|
toast.error(newDefaultDirectory.error.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newDefaultDirectory.path !== null) {
|
|
|
|
newSettings.defaultDirectory = newDefaultDirectory.path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { settings: validatedSettings, errors: validationErrors } =
|
|
|
|
validateSettings(newSettings)
|
|
|
|
|
|
|
|
retrievedSettings.current = Object.assign(
|
|
|
|
{},
|
|
|
|
initialSettings,
|
|
|
|
retrievedSettings.current,
|
|
|
|
validatedSettings
|
|
|
|
)
|
|
|
|
|
|
|
|
settingsSend({
|
|
|
|
type: 'Set All Settings',
|
|
|
|
data: validatedSettings,
|
|
|
|
})
|
|
|
|
|
|
|
|
return validationErrors
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the app is not running in the Tauri context,
|
|
|
|
// just use the settings from local storage
|
|
|
|
// after they've been validated to ensure they are correct.
|
|
|
|
settingsSend({
|
|
|
|
type: 'Set All Settings',
|
|
|
|
data: retrievedSettings.current.settings,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
void getFileBasedSettings().then((validationErrors: string[]) => {
|
|
|
|
const combinedErrors = new Set([
|
|
|
|
...retrievedSettings.current.errors,
|
|
|
|
...validationErrors,
|
|
|
|
])
|
|
|
|
|
|
|
|
if (combinedErrors.size > 0) {
|
|
|
|
const errorMessage =
|
|
|
|
'Error validating persisted settings: ' +
|
|
|
|
Array.from(combinedErrors).join(', ') +
|
|
|
|
'. Using defaults.'
|
|
|
|
console.error(errorMessage)
|
|
|
|
toast.error(errorMessage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, [settingsSend])
|
|
|
|
|
2023-08-29 10:48:55 -04:00
|
|
|
useStateMachineCommands({
|
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
|
|
|
machineId: 'settings',
|
2023-08-29 10:48:55 -04:00
|
|
|
state: settingsState,
|
|
|
|
send: settingsSend,
|
2023-12-06 14:44:13 -05:00
|
|
|
commandBarConfig: settingsCommandBarConfig,
|
2023-08-29 10:48:55 -04:00
|
|
|
})
|
|
|
|
|
2023-08-31 09:34:13 -04:00
|
|
|
// Listen for changes to the system theme and update the app theme accordingly
|
|
|
|
// This is only done if the theme setting is set to 'system'.
|
|
|
|
// It can't be done in XState (in an invoked callback, for example)
|
|
|
|
// because there doesn't seem to be a good way to listen to
|
|
|
|
// events outside of the machine that also depend on the machine's context
|
|
|
|
useEffect(() => {
|
|
|
|
const matcher = window.matchMedia('(prefers-color-scheme: dark)')
|
|
|
|
const listener = (e: MediaQueryListEvent) => {
|
|
|
|
if (settingsState.context.theme !== 'system') return
|
|
|
|
setThemeClass(e.matches ? Themes.Dark : Themes.Light)
|
|
|
|
}
|
|
|
|
|
|
|
|
matcher.addEventListener('change', listener)
|
|
|
|
return () => matcher.removeEventListener('change', listener)
|
|
|
|
}, [settingsState.context])
|
2023-08-29 10:48:55 -04:00
|
|
|
|
|
|
|
// Auth machine setup
|
|
|
|
const [authState, authSend] = useMachine(authMachine, {
|
|
|
|
actions: {
|
|
|
|
goToSignInPage: () => {
|
|
|
|
navigate(paths.SIGN_IN)
|
2023-09-14 19:31:16 -04:00
|
|
|
|
2024-02-11 12:59:00 +11:00
|
|
|
logout()
|
2023-08-29 10:48:55 -04:00
|
|
|
},
|
|
|
|
goToIndexPage: () => {
|
|
|
|
if (window.location.pathname.includes(paths.SIGN_IN)) {
|
|
|
|
navigate(paths.INDEX)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
useStateMachineCommands({
|
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
|
|
|
machineId: 'auth',
|
2023-08-29 10:48:55 -04:00
|
|
|
state: authState,
|
|
|
|
send: authSend,
|
2023-12-06 14:44:13 -05:00
|
|
|
commandBarConfig: authCommandBarConfig,
|
2023-08-29 10:48:55 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
2024-02-15 14:14:14 -05:00
|
|
|
<SettingsAuthStateContext.Provider
|
2023-08-29 10:48:55 -04:00
|
|
|
value={{
|
|
|
|
auth: {
|
|
|
|
state: authState,
|
|
|
|
context: authState.context,
|
|
|
|
send: authSend,
|
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
state: settingsState,
|
|
|
|
context: settingsState.context,
|
|
|
|
send: settingsSend,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
2024-02-15 14:14:14 -05:00
|
|
|
</SettingsAuthStateContext.Provider>
|
2023-08-29 10:48:55 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-15 14:14:14 -05:00
|
|
|
export default SettingsAuthStateProvider
|
2023-08-29 10:48:55 -04:00
|
|
|
|
|
|
|
export function logout() {
|
|
|
|
localStorage.removeItem(TOKEN_PERSIST_KEY)
|
2023-09-14 19:31:16 -04:00
|
|
|
return (
|
|
|
|
!isTauri() &&
|
|
|
|
fetch(withBaseUrl('/logout'), {
|
|
|
|
method: 'POST',
|
|
|
|
credentials: 'include',
|
|
|
|
})
|
|
|
|
)
|
2023-08-29 10:48:55 -04:00
|
|
|
}
|