Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup(ts/hooks/useDetour): convert compound match to tags #2606

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions assets/src/hooks/useDetour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,18 @@ export enum DetourState {

const selectFinishedState = (
snapshot: SnapshotFrom<typeof createDetourMachine>
) => {
) =>
/*
* There's probably a better way to do this? Tags or maybe context?
* Tags seem more appropriate, but weird to manage Out-Of-Bounds state via tags.
* There's probably a better way to do this? maybe context or sub-machines?
* It seems okay to manage this state via tags if we can keep things documented,
* but it feels slightly leaky to have the concern for this API call to be
* distinguished as a tag within the machine, I think it works temporarily
* and while the machine is small though
*
* Maybe this entire API call could be moved to and managed by an actor
* combined with parallel child states within the state machine?
* -- https://stately.ai/docs/promise-actors */
const isFinishedDrawing = snapshot.matches({
"Detour Drawing": { Editing: "Finished Drawing" },
})
const isSharingDetour = snapshot.matches({ "Detour Drawing": "Share Detour" })
const isInFinishedDetourState = isFinishedDrawing || isSharingDetour
return isInFinishedDetourState
}
snapshot.hasTag("is-finished-drawing")

/** This selects the detour waypoints in-between the detour start and end points */
const selectWaypoints = (snapshot: SnapshotFrom<typeof createDetourMachine>) =>
Expand Down
8 changes: 8 additions & 0 deletions assets/src/models/createDetourMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const createDetourMachine = setup({
context: {
waypoints: ShapePoint[]
}

events:
| { type: "detour.edit.clear-detour" }
| { type: "detour.edit.done" }
Expand All @@ -14,6 +15,11 @@ export const createDetourMachine = setup({
| { type: "detour.edit.resume" }
| { type: "detour.edit.undo" }
| { type: "detour.share.copy-detour"; detourText: string }

tags:
| "" // Placeholder for the formatter
// When we should fetch finished detour details
| "is-finished-drawing"
},
actions: {
"detour.add-waypoint": assign({
Expand Down Expand Up @@ -100,6 +106,7 @@ export const createDetourMachine = setup({
},
},
"Finished Drawing": {
tags: ["is-finished-drawing"],
on: {
"detour.edit.undo": {
actions: "detour.remove-last-waypoint",
Expand All @@ -120,6 +127,7 @@ export const createDetourMachine = setup({
},
},
"Share Detour": {
tags: ["is-finished-drawing"],
on: {
"detour.edit.resume": {
target: "Editing.Finished Drawing",
Expand Down
Loading