-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graph): use a state machine to allow actions from external sourc…
…e (e.g. Console)
- Loading branch information
Showing
7 changed files
with
322 additions
and
14 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
graph/client/src/app/console-project-details/project-details.app.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { useCallback } from 'react'; | ||
import { | ||
ErrorToastUI, | ||
ExpandedTargetsProvider, | ||
getExternalApiService, | ||
} from '@nx/graph/shared'; | ||
import { useMachine, useSelector } from '@xstate/react'; | ||
import { ProjectDetails } from '@nx/graph-internal/ui-project-details'; | ||
import { | ||
ProjectDetailsEvents, | ||
ProjectDetailsState, | ||
} from './project-details.machine'; | ||
import { Interpreter } from 'xstate'; | ||
|
||
export function ProjectDetailsApp({ | ||
service, | ||
}: { | ||
service: Interpreter<ProjectDetailsState, any, ProjectDetailsEvents>; | ||
}) { | ||
const externalApiService = getExternalApiService(); | ||
|
||
const project = useSelector(service, (state) => state.context.project); | ||
const sourceMap = useSelector(service, (state) => state.context.sourceMap); | ||
const errors = useSelector(service, (state) => state.context.errors); | ||
const connectedToCloud = useSelector( | ||
service, | ||
(state) => state.context.connectedToCloud | ||
); | ||
|
||
const handleViewInProjectGraph = useCallback( | ||
(data: { projectName: string }) => { | ||
externalApiService.postEvent({ | ||
type: 'open-project-graph', | ||
payload: { | ||
projectName: data.projectName, | ||
}, | ||
}); | ||
}, | ||
[externalApiService] | ||
); | ||
|
||
const handleViewInTaskGraph = useCallback( | ||
(data: { projectName: string; targetName: string }) => { | ||
externalApiService.postEvent({ | ||
type: 'open-task-graph', | ||
payload: { | ||
projectName: data.projectName, | ||
targetName: data.targetName, | ||
}, | ||
}); | ||
}, | ||
[externalApiService] | ||
); | ||
|
||
const handleRunTarget = useCallback( | ||
(data: { projectName: string; targetName: string }) => { | ||
externalApiService.postEvent({ | ||
type: 'run-task', | ||
payload: { taskId: `${data.projectName}:${data.targetName}` }, | ||
}); | ||
}, | ||
[externalApiService] | ||
); | ||
|
||
const handleNxConnect = useCallback( | ||
() => | ||
externalApiService.postEvent({ | ||
type: 'nx-connect', | ||
}), | ||
[externalApiService] | ||
); | ||
|
||
if (project && sourceMap) { | ||
return ( | ||
<> | ||
<ExpandedTargetsProvider> | ||
<ProjectDetails | ||
project={project} | ||
sourceMap={sourceMap} | ||
onViewInProjectGraph={handleViewInProjectGraph} | ||
onViewInTaskGraph={handleViewInTaskGraph} | ||
onRunTarget={handleRunTarget} | ||
viewInProjectGraphPosition="bottom" | ||
connectedToCloud={connectedToCloud} | ||
onNxConnect={handleNxConnect} | ||
/> | ||
</ExpandedTargetsProvider> | ||
<ErrorToastUI errors={errors} /> | ||
</> | ||
); | ||
} else { | ||
return null; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
graph/client/src/app/console-project-details/project-details.machine.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { interpret } from 'xstate'; | ||
import { projectDetailsMachine } from './project-details.machine'; | ||
|
||
describe('graphMachine', () => { | ||
let service; | ||
|
||
beforeEach(() => { | ||
service = interpret(projectDetailsMachine).start(); | ||
}); | ||
|
||
afterEach(() => { | ||
service.stop(); | ||
}); | ||
|
||
it('should have initial idle state', () => { | ||
expect(service.state.value).toEqual('idle'); | ||
expect(service.state.context.project).toEqual(null); | ||
expect(service.state.context.errors).toEqual(null); | ||
}); | ||
|
||
it('should handle setting project and source map', () => { | ||
service.send({ | ||
type: 'loadData', | ||
project: { | ||
type: 'app', | ||
name: 'proj', | ||
data: {}, | ||
}, | ||
sourceMap: { | ||
root: ['project.json', 'nx-core-build-project-json-nodes'], | ||
}, | ||
}); | ||
|
||
expect(service.state.context.project).toEqual({ | ||
type: 'app', | ||
name: 'proj', | ||
data: {}, | ||
}); | ||
expect(service.state.context.sourceMap).toEqual({ | ||
root: ['project.json', 'nx-core-build-project-json-nodes'], | ||
}); | ||
}); | ||
|
||
it('should handle errors', () => { | ||
const testError = { | ||
message: 'test', | ||
stack: 'test', | ||
cause: 'test', | ||
name: 'test', | ||
pluginName: 'test', | ||
}; | ||
|
||
service.send({ | ||
type: 'setErrors', | ||
errors: [testError], | ||
}); | ||
expect(service.state.value).toEqual('error'); | ||
expect(service.state.context.errors).toBeDefined(); | ||
|
||
service.send({ type: 'clearErrors' }); | ||
expect(service.state.value).toEqual('idle'); | ||
|
||
service.send({ | ||
type: 'setErrors', | ||
errors: [testError], | ||
}); | ||
expect(service.state.value).toEqual('error'); | ||
service.send({ | ||
type: 'loadData', | ||
project: { | ||
type: 'app', | ||
name: 'proj', | ||
data: {}, | ||
}, | ||
sourceMap: { | ||
root: ['project.json', 'nx-core-build-project-json-nodes'], | ||
}, | ||
}); | ||
// Still in error state | ||
expect(service.state.value).toEqual('error'); | ||
}); | ||
}); |
101 changes: 101 additions & 0 deletions
101
graph/client/src/app/console-project-details/project-details.machine.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* eslint-disable @nx/enforce-module-boundaries */ | ||
// nx-ignore-next-line | ||
import type { ProjectGraphProjectNode } from '@nx/devkit'; | ||
// nx-ignore-next-line | ||
import { GraphError } from 'nx/src/command-line/graph/graph'; | ||
/* eslint-enable @nx/enforce-module-boundaries */ | ||
import { createMachine } from 'xstate'; | ||
import { assign } from '@xstate/immer'; | ||
|
||
export interface ProjectDetailsState { | ||
project: null | ProjectGraphProjectNode; | ||
sourceMap: null | Record<string, string[]>; | ||
errors: null | GraphError[]; | ||
connectedToCloud: boolean; | ||
} | ||
|
||
export type ProjectDetailsEvents = | ||
| { | ||
type: 'loadData'; | ||
project: ProjectGraphProjectNode; | ||
sourceMap: Record<string, string[]>; | ||
connectedToCloud: boolean; | ||
} | ||
| { | ||
type: 'setErrors'; | ||
errors: GraphError[]; | ||
} | ||
| { | ||
type: 'clearErrors'; | ||
}; | ||
|
||
const initialContext: ProjectDetailsState = { | ||
project: null, | ||
sourceMap: null, | ||
errors: null, | ||
connectedToCloud: false, | ||
}; | ||
|
||
export const projectDetailsMachine = createMachine< | ||
ProjectDetailsState, | ||
ProjectDetailsEvents | ||
>({ | ||
predictableActionArguments: true, | ||
preserveActionOrder: true, | ||
id: 'project-view', | ||
initial: 'idle', | ||
context: initialContext, | ||
states: { | ||
idle: {}, | ||
loaded: {}, | ||
error: {}, | ||
}, | ||
on: { | ||
loadData: [ | ||
{ | ||
target: 'loaded', | ||
cond: (ctx, _event) => ctx.errors === null || ctx.errors.length === 0, | ||
actions: [ | ||
assign((ctx, event) => { | ||
ctx.project = event.project; | ||
ctx.sourceMap = event.sourceMap; | ||
ctx.connectedToCloud = event.connectedToCloud; | ||
}), | ||
], | ||
}, | ||
{ | ||
target: 'error', | ||
cond: (ctx, _event) => ctx.errors !== null && ctx.errors.length > 0, | ||
actions: [ | ||
assign((ctx, event) => { | ||
ctx.project = event.project; | ||
ctx.sourceMap = event.sourceMap; | ||
ctx.connectedToCloud = event.connectedToCloud; | ||
}), | ||
], | ||
}, | ||
], | ||
setErrors: { | ||
target: 'error', | ||
actions: assign((ctx, event) => { | ||
ctx.errors = event.errors; | ||
}), | ||
}, | ||
clearErrors: [ | ||
{ | ||
target: 'idle', | ||
cond: (ctx, _event) => ctx.project === null, | ||
actions: assign((ctx) => { | ||
ctx.errors = null; | ||
}), | ||
}, | ||
{ | ||
target: 'loaded', | ||
cond: (ctx, _event) => ctx.project !== null, | ||
actions: assign((ctx) => { | ||
ctx.errors = null; | ||
}), | ||
}, | ||
], | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters