2023-08-29 10:48:55 -04:00
|
|
|
import { useMachine } from '@xstate/react'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { useNavigate, useRouteLoaderData } 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'
|
2024-03-14 15:56:45 -04:00
|
|
|
import React, { createContext, useEffect } from 'react'
|
2023-08-29 10:48:55 -04:00
|
|
|
import useStateMachineCommands from '../hooks/useStateMachineCommands'
|
2024-03-12 10:37:35 -04:00
|
|
|
import { settingsMachine } from 'machines/settingsMachine'
|
2024-03-14 15:56:45 -04:00
|
|
|
import {
|
|
|
|
fallbackLoadedSettings,
|
|
|
|
validateSettings,
|
|
|
|
} from 'lib/settings/settingsUtils'
|
2023-08-29 10:48:55 -04:00
|
|
|
import { toast } from 'react-hot-toast'
|
2024-03-22 09:35:07 -04:00
|
|
|
import { getThemeColorForEngine, 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-22 09:35:07 -04:00
|
|
|
import { kclManager, sceneInfra, engineCommandManager } from 'lib/singletons'
|
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
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-03-14 15:56:45 -04:00
|
|
|
type SettingsAuthContextType = {
|
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-03-14 15:56:45 -04:00
|
|
|
export const SettingsAuthContext = createContext({} as SettingsAuthContextType)
|
2023-08-29 10:48:55 -04:00
|
|
|
|
2024-03-11 20:26:13 -04:00
|
|
|
export const SettingsAuthProvider = ({
|
2023-08-29 10:48:55 -04:00
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
}) => {
|
2024-03-14 15:56:45 -04:00
|
|
|
const loadedSettings = useRouteLoaderData(paths.INDEX) as Awaited<
|
|
|
|
ReturnType<typeof validateSettings>
|
|
|
|
>
|
|
|
|
return (
|
|
|
|
<SettingsAuthProviderBase loadedSettings={loadedSettings}>
|
|
|
|
{children}
|
|
|
|
</SettingsAuthProviderBase>
|
2023-08-29 10:48:55 -04:00
|
|
|
)
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// For use in jest tests we don't want to use the loader data
|
|
|
|
// and mock the whole Router
|
|
|
|
export const SettingsAuthProviderJest = ({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
}) => {
|
|
|
|
const loadedSettings = fallbackLoadedSettings
|
|
|
|
return (
|
|
|
|
<SettingsAuthProviderBase loadedSettings={loadedSettings}>
|
|
|
|
{children}
|
|
|
|
</SettingsAuthProviderBase>
|
2023-08-29 10:48:55 -04:00
|
|
|
)
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const SettingsAuthProviderBase = ({
|
|
|
|
children,
|
|
|
|
loadedSettings,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
loadedSettings: Awaited<ReturnType<typeof validateSettings>>
|
|
|
|
}) => {
|
|
|
|
const { settings: initialLoadedContext } = loadedSettings
|
|
|
|
const navigate = useNavigate()
|
2023-08-29 10:48:55 -04:00
|
|
|
|
2024-03-04 16:06:43 -05:00
|
|
|
const [settingsState, settingsSend, settingsActor] = useMachine(
|
|
|
|
settingsMachine,
|
|
|
|
{
|
2024-03-14 15:56:45 -04:00
|
|
|
context: initialLoadedContext,
|
2024-03-04 16:06:43 -05:00
|
|
|
actions: {
|
2024-03-14 15:56:45 -04:00
|
|
|
setClientSideSceneUnits: (context, event) => {
|
|
|
|
const newBaseUnit =
|
|
|
|
event.type === 'Set Base Unit'
|
|
|
|
? event.data.baseUnit
|
|
|
|
: context.baseUnit
|
|
|
|
sceneInfra.baseUnit = newBaseUnit
|
|
|
|
},
|
2024-03-22 09:35:07 -04:00
|
|
|
setEngineTheme: (context) => {
|
|
|
|
engineCommandManager.sendSceneCommand({
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
type: 'modeling_cmd_req',
|
|
|
|
cmd: {
|
|
|
|
type: 'set_background_color',
|
|
|
|
color: getThemeColorForEngine(context.theme),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2024-03-04 16:06:43 -05:00
|
|
|
toastSuccess: (context, event) => {
|
|
|
|
const truncatedNewValue =
|
|
|
|
'data' in event && event.data instanceof Object
|
2024-03-14 15:56:45 -04:00
|
|
|
? (context[Object.keys(event.data)[0] as keyof typeof context]
|
|
|
|
.toString()
|
|
|
|
.substring(0, 28) as any)
|
2024-03-04 16:06:43 -05:00
|
|
|
: undefined
|
|
|
|
toast.success(
|
|
|
|
event.type +
|
|
|
|
(truncatedNewValue
|
|
|
|
? ` to "${truncatedNewValue}${
|
|
|
|
truncatedNewValue.length === 28 ? '...' : ''
|
|
|
|
}"`
|
|
|
|
: '')
|
|
|
|
)
|
|
|
|
},
|
2024-03-14 15:56:45 -04:00
|
|
|
'Execute AST': () => kclManager.executeAst(),
|
2023-08-29 10:48:55 -04:00
|
|
|
},
|
2024-03-04 16:06:43 -05:00
|
|
|
}
|
|
|
|
)
|
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,
|
2024-03-04 16:06:43 -05:00
|
|
|
actor: settingsActor,
|
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
|
2024-03-04 16:06:43 -05:00
|
|
|
const [authState, authSend, authActor] = useMachine(authMachine, {
|
2023-08-29 10:48:55 -04:00
|
|
|
actions: {
|
|
|
|
goToSignInPage: () => {
|
|
|
|
navigate(paths.SIGN_IN)
|
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,
|
2024-03-04 16:06:43 -05:00
|
|
|
actor: authActor,
|
2023-08-29 10:48:55 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
2024-03-11 20:26:13 -04:00
|
|
|
<SettingsAuthContext.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-03-11 20:26:13 -04:00
|
|
|
</SettingsAuthContext.Provider>
|
2023-08-29 10:48:55 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-03-11 20:26:13 -04:00
|
|
|
export default SettingsAuthProvider
|
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
|
|
|
}
|