Skip to content

Commit

Permalink
cleanup(ts/hooks/useDetour): convert complex match to tags
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed May 20, 2024
1 parent cd015ef commit 38deea4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
14 changes: 7 additions & 7 deletions assets/src/hooks/useDetour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ export const useDetour = ({ routePatternId, shape }: OriginalRoute) => {
const [snapshot, send] = useMachine(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 document,
* 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
const isInFinishedDetourState = snapshot.hasTag("is-finished-drawing")

const firstWaypoint = snapshot.context.waypoints.at(0)
const lastWaypoint = snapshot.context.waypoints.at(-1)
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.share" }
| { type: "detour.resume-editing" }
Expand All @@ -15,6 +16,11 @@ export const createDetourMachine = setup({
| { type: "detour.edit.undo" }
| { type: "detour.edit.done" }
| { 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 @@ -103,6 +109,7 @@ export const createDetourMachine = setup({
},
},
"Finished Drawing": {
tags: ["is-finished-drawing"],
on: {
"detour.edit.undo": {
actions: "detour.undo",
Expand All @@ -123,6 +130,7 @@ export const createDetourMachine = setup({
},
},
"Share Detour": {
tags: ["is-finished-drawing"],
on: {
"detour.resume-editing": {
target: "Editing.Finished Drawing",
Expand Down

0 comments on commit 38deea4

Please sign in to comment.