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-16 09:09:58 -05:00
|
|
|
import { SETTINGS_PERSIST_KEY, settingsMachine } from 'machines/settingsMachine'
|
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-03-01 06:55:49 +11:00
|
|
|
import { sceneInfra } from 'clientSideScene/sceneInfra'
|
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-16 09:09:58 -05:00
|
|
|
type GlobalContext = {
|
2023-08-29 10:48:55 -04:00
|
|
|
auth: MachineContext<typeof authMachine>
|
|
|
|
settings: MachineContext<typeof settingsMachine>
|
|
|
|
}
|
|
|
|
|
2024-02-20 17:55:06 -08:00
|
|
|
// a little hacky for sure, open to changing it
|
|
|
|
// this implies that we should only even have one instance of this provider mounted at any one time
|
|
|
|
// but I think that's a safe assumption
|
|
|
|
let settingsStateRef: (typeof settingsMachine)['context'] | undefined
|
|
|
|
export const getSettingsState = () => settingsStateRef
|
|
|
|
|
2024-02-16 09:09:58 -05:00
|
|
|
export const GlobalStateContext = createContext({} as GlobalContext)
|
2023-08-29 10:48:55 -04:00
|
|
|
|
2024-02-16 09:09:58 -05:00
|
|
|
export const GlobalStateProvider = ({
|
2023-08-29 10:48:55 -04:00
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
}) => {
|
|
|
|
const navigate = useNavigate()
|
|
|
|
|
|
|
|
// Settings machine setup
|
|
|
|
const retrievedSettings = useRef(
|
2024-02-16 09:09:58 -05:00
|
|
|
localStorage?.getItem(SETTINGS_PERSIST_KEY) || '{}'
|
2023-08-29 10:48:55 -04:00
|
|
|
)
|
|
|
|
const persistedSettings = Object.assign(
|
2024-02-16 09:09:58 -05:00
|
|
|
settingsMachine.initialState.context,
|
|
|
|
JSON.parse(retrievedSettings.current) as Partial<
|
|
|
|
(typeof settingsMachine)['context']
|
|
|
|
>
|
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-20 17:55:06 -08:00
|
|
|
settingsStateRef = settingsState.context
|
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)
|
|
|
|
}
|
2024-03-01 06:55:49 +11:00
|
|
|
sceneInfra.baseUnit = settingsState?.context?.baseUnit || 'mm'
|
2023-08-31 09:34:13 -04:00
|
|
|
|
|
|
|
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-16 09:09:58 -05:00
|
|
|
<GlobalStateContext.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-16 09:09:58 -05:00
|
|
|
</GlobalStateContext.Provider>
|
2023-08-29 10:48:55 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-16 09:09:58 -05:00
|
|
|
export default GlobalStateProvider
|
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
|
|
|
}
|