Migrate to XState v5 (#3735)
* migrate settingsMachine
* Guard events with properties instead
* migrate settingsMachine
* Migrate auth machine
* Migrate file machine
* Migrate depracated types
* Migrate home machine
* Migrate command bar machine
* Version fixes
* Migrate command bar machine
* Migrate modeling machine
* Migrate types, state.can, state.matches and state.nextEvents
* Fix syntax
* Pass in modelingState into editor manager instead of modeling event
* Fix issue with missing command bar provider
* Fix state transition
* Fix type issue in Home
* Make sure no guards rely on event type
* Fix up command bar submission logic
* Home machine tweaks to get things running
* Fix AST fillet function args
* Handle "Set selection" when it is called by actor onDone
* Remove unused imports
* A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)
* Fix injectin project to the fileTree machine
* Revert "A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)"
This reverts commit 4b43ff69d1
.
* A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)
* Re-run CI
* Restore success toasts on file/folder deletion
* Replace casting with guarding against event.type
* Remove console.log
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
* Replace all instances of event casting with guards against event.type
---------
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Kurt Hutten Irev-Dev <k.hutten@protonmail.ch>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
Co-authored-by: Frank Noirot <frank@zoo.dev>
This commit is contained in:
committed by
GitHub
parent
7c2cfba0ac
commit
5f8d4f8294
@ -1,4 +1,4 @@
|
||||
import { createMachine, assign } from 'xstate'
|
||||
import { assign, setup, fromPromise } from 'xstate'
|
||||
import { Models } from '@kittycad/lib'
|
||||
import withBaseURL from '../lib/withBaseURL'
|
||||
import { isDesktop } from 'lib/isDesktop'
|
||||
@ -55,79 +55,92 @@ const persistedToken =
|
||||
localStorage?.getItem(TOKEN_PERSIST_KEY) ||
|
||||
''
|
||||
|
||||
export const authMachine = createMachine<UserContext, Events>(
|
||||
{
|
||||
/** @xstate-layout N4IgpgJg5mDOIC5QEECuAXAFgOgMabFwGsBJAMwBkB7KGCEgOwGIIqGxsBLBgNyqI75CRALQAbGnRHcA2gAYAuolAAHKrE7pObZSAAeiAIwBmAEzYA7ABYAbAFZTcgBzGbN44adWANCACeiKbGdthypk4AnBFyVs6uQXYAvom+aFh4BMTk1LSQjExgAE6FVIXYKmIAhuhkpQC2GcLikpDSDPJKSCBqGlo6XQYIrk7YETYWctYRxmMWFk6+AUPj2I5OdjZyrnZOFmbJqRg4Ern0zDkABFQYHbo9mtoMuoOGFhHYxlZOhvbOsUGGRaIL4WbBONzWQxWYwWOx2H4HEBpY4tCAAeQwTEuskUd3UD36oEGIlMNlCuzk8Js0TcVisgP8iG2lmcGysb0mW3ByRSIAYVAgcF0yLxvUez0QIms5ImVJpNjpDKWxmw9PGdLh4Te00+iORjSylFRjFFBKeA0QThGQWcexMwWhniBCGiqrepisUVMdlszgieqO2BOdBNXXufXNRKMHtGVuphlJkXs4Wdriso2CCasdgipOidID6WDkAx6FNEYlCAT5jmcjrckMdj2b3GzpsjbBMVMWezDbGPMSQA */
|
||||
id: 'Auth',
|
||||
initial: 'checkIfLoggedIn',
|
||||
states: {
|
||||
checkIfLoggedIn: {
|
||||
id: 'check-if-logged-in',
|
||||
invoke: {
|
||||
src: 'getUser',
|
||||
id: 'check-logged-in',
|
||||
onDone: [
|
||||
{
|
||||
target: 'loggedIn',
|
||||
actions: assign((context, event) => ({
|
||||
user: event.data.user,
|
||||
token: event.data.token || context.token,
|
||||
})),
|
||||
},
|
||||
],
|
||||
onError: [
|
||||
{
|
||||
target: 'loggedOut',
|
||||
actions: assign({
|
||||
user: () => undefined,
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
loggedIn: {
|
||||
entry: ['goToIndexPage'],
|
||||
on: {
|
||||
'Log out': {
|
||||
target: 'loggedOut',
|
||||
actions: () => {
|
||||
if (isDesktop()) writeTokenFile('')
|
||||
},
|
||||
export const authMachine = setup({
|
||||
types: {} as {
|
||||
context: UserContext
|
||||
events:
|
||||
| Events
|
||||
| {
|
||||
type: 'xstate.done.actor.check-logged-in'
|
||||
output: {
|
||||
user: Models['User_type']
|
||||
token: string
|
||||
}
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
goToIndexPage: () => {},
|
||||
goToSignInPage: () => {},
|
||||
},
|
||||
actors: {
|
||||
getUser: fromPromise(({ input }: { input: { token?: string } }) =>
|
||||
getUser(input)
|
||||
),
|
||||
},
|
||||
}).createMachine({
|
||||
/** @xstate-layout N4IgpgJg5mDOIC5QEECuAXAFgOgMabFwGsBJAMwBkB7KGCEgOwGIIqGxsBLBgNyqI75CRALQAbGnRHcA2gAYAuolAAHKrE7pObZSAAeiAIwBmAEzYA7ABYAbAFZTcgBzGbN44adWANCACeiKbGdthypk4AnBFyVs6uQXYAvom+aFh4BMTk1LSQjExgAE6FVIXYKmIAhuhkpQC2GcLikpDSDPJKSCBqGlo6XQYIrk7YETYWctYRxmMWFk6+AUPj2I5OdjZyrnZOFmbJqRg4Ern0zDkABFQYHbo9mtoMuoOGFhHYxlZOhvbOsUGGRaIL4WbBONzWQxWYwWOx2H4HEBpY4tCAAeQwTEuskUd3UD36oEGIlMNlCuzk8Js0TcVisgP8iG2lmcGysb0mW3ByRSIAYVAgcF0yLxvUez0QIms5ImVJpNjpDKWxmw9PGdLh4Te00+iORjSylFRjFFBKeA0QThGQWcexMwWhniBCGiqrepisUVMdlszgieqO2BOdBNXXufXNRKMHtGVuphlJkXs4Wdriso2CCasdgipOidID6WDkAx6FNEYlCAT5jmcjrckMdj2b3GzpsjbBMVMWezDbGPMSQA */
|
||||
id: 'Auth',
|
||||
initial: 'checkIfLoggedIn',
|
||||
context: {
|
||||
token: persistedToken,
|
||||
},
|
||||
states: {
|
||||
checkIfLoggedIn: {
|
||||
id: 'check-if-logged-in',
|
||||
invoke: {
|
||||
src: 'getUser',
|
||||
input: ({ context }) => ({ token: context.token }),
|
||||
id: 'check-logged-in',
|
||||
onDone: [
|
||||
{
|
||||
target: 'loggedIn',
|
||||
actions: assign(({ context, event }) => ({
|
||||
user: event.output.user,
|
||||
token: event.output.token || context.token,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
loggedOut: {
|
||||
entry: ['goToSignInPage'],
|
||||
on: {
|
||||
'Log in': {
|
||||
target: 'checkIfLoggedIn',
|
||||
],
|
||||
onError: [
|
||||
{
|
||||
target: 'loggedOut',
|
||||
actions: assign({
|
||||
token: (_, event) => {
|
||||
const token = event.token || ''
|
||||
return token
|
||||
},
|
||||
user: () => undefined,
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
loggedIn: {
|
||||
entry: ['goToIndexPage'],
|
||||
on: {
|
||||
'Log out': {
|
||||
target: 'loggedOut',
|
||||
actions: () => {
|
||||
if (isDesktop()) writeTokenFile('')
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
schema: { events: {} as { type: 'Log out' } | { type: 'Log in' } },
|
||||
predictableActionArguments: true,
|
||||
preserveActionOrder: true,
|
||||
context: {
|
||||
token: persistedToken,
|
||||
loggedOut: {
|
||||
entry: ['goToSignInPage'],
|
||||
on: {
|
||||
'Log in': {
|
||||
target: 'checkIfLoggedIn',
|
||||
actions: assign({
|
||||
token: ({ event }) => {
|
||||
const token = event.token || ''
|
||||
return token
|
||||
},
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
actions: {},
|
||||
services: { getUser },
|
||||
guards: {},
|
||||
delays: {},
|
||||
}
|
||||
)
|
||||
schema: { events: {} as { type: 'Log out' } | { type: 'Log in' } },
|
||||
})
|
||||
|
||||
async function getUser(context: UserContext) {
|
||||
const token = await getAndSyncStoredToken(context)
|
||||
async function getUser(input: { token?: string }) {
|
||||
const token = await getAndSyncStoredToken(input)
|
||||
const url = withBaseURL('/user')
|
||||
const headers: { [key: string]: string } = {
|
||||
'Content-Type': 'application/json',
|
||||
@ -156,7 +169,7 @@ async function getUser(context: UserContext) {
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.catch((err) => console.error('error from Browser getUser', err))
|
||||
: getUserDesktop(context.token ?? '', VITE_KC_API_BASE_URL)
|
||||
: getUserDesktop(input.token ?? '', VITE_KC_API_BASE_URL)
|
||||
|
||||
const user = await userPromise
|
||||
|
||||
@ -193,13 +206,15 @@ function getCookie(cname: string): string | null {
|
||||
return null
|
||||
}
|
||||
|
||||
async function getAndSyncStoredToken(context: UserContext): Promise<string> {
|
||||
async function getAndSyncStoredToken(input: {
|
||||
token?: string
|
||||
}): Promise<string> {
|
||||
// dev mode
|
||||
if (VITE_KC_DEV_TOKEN) return VITE_KC_DEV_TOKEN
|
||||
|
||||
const token =
|
||||
context.token && context.token !== ''
|
||||
? context.token
|
||||
input.token && input.token !== ''
|
||||
? input.token
|
||||
: getCookie(COOKIE_NAME) || localStorage?.getItem(TOKEN_PERSIST_KEY) || ''
|
||||
if (token) {
|
||||
// has just logged in, update storage
|
||||
|
Reference in New Issue
Block a user