-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab684c8
commit 9332d4a
Showing
9 changed files
with
186 additions
and
64 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getApi } from "@flows/shared"; | ||
import { config } from "../store"; | ||
|
||
export const sendEvent = async (props: { | ||
name: "transition" | "tour-update"; | ||
blockId: string; | ||
propertyKey?: string; | ||
properties?: Record<string, unknown>; | ||
}): Promise<void> => { | ||
const configuration = config.value; | ||
if (!configuration) return; | ||
const { environment, organizationId, userId, apiUrl } = configuration; | ||
await getApi(apiUrl).sendEvent({ | ||
...props, | ||
environment, | ||
organizationId, | ||
userId, | ||
}); | ||
}; |
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,8 @@ | ||
import { handleTourDocumentClick } from "./tour"; | ||
|
||
export const handleDocumentClick = (event: MouseEvent): void => { | ||
const eventTarget = event.target; | ||
if (!eventTarget || !(eventTarget instanceof Element)) return; | ||
|
||
handleTourDocumentClick(eventTarget); | ||
}; |
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,17 @@ | ||
interface Handler { | ||
type: string; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- this is correct type | ||
handler: (...args: any[]) => any; | ||
} | ||
|
||
let handlers: Handler[] = []; | ||
|
||
export const addHandlers = (handlersToAdd: Handler[]): void => { | ||
handlers.forEach(({ type, handler }) => { | ||
document.removeEventListener(type, handler, true); | ||
}); | ||
handlersToAdd.forEach(({ type, handler }) => { | ||
document.addEventListener(type, handler, true); | ||
}); | ||
handlers = handlersToAdd; | ||
}; |
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,104 @@ | ||
import { elementContains, getPathname, pathnameMatch, type Block } from "@flows/shared"; | ||
import { effect } from "@preact/signals-core"; | ||
import { blocks, pathname, type RunningTour, runningTours } from "../store"; | ||
import { sendEvent } from "./api"; | ||
|
||
export const updateTourState = ( | ||
tourBlockId: string, | ||
updateFn: (tour: RunningTour) => RunningTour, | ||
): void => { | ||
runningTours.value = runningTours.value.map((tour) => | ||
tour.blockId === tourBlockId ? updateFn(tour) : tour, | ||
); | ||
}; | ||
|
||
export const hideTour = (tourBlockId: string): void => { | ||
updateTourState(tourBlockId, (t) => ({ ...t, hidden: true })); | ||
}; | ||
|
||
export const previousTourStep = (tourBlock: Block, currentIndex: number): void => { | ||
const isFirstStep = currentIndex === 0; | ||
|
||
if (isFirstStep) return; | ||
const newIndex = currentIndex - 1; | ||
updateTourState(tourBlock.id, (t) => ({ ...t, currentBlockIndex: newIndex })); | ||
void sendEvent({ | ||
name: "tour-update", | ||
blockId: tourBlock.id, | ||
properties: { currentTourIndex: newIndex }, | ||
}); | ||
}; | ||
|
||
export const nextTourStep = (tourBlock: Block, currentIndex: number): void => { | ||
const isLastStep = currentIndex === (tourBlock.tourBlocks?.length ?? 1) - 1; | ||
|
||
if (isLastStep) { | ||
hideTour(tourBlock.id); | ||
void sendEvent({ name: "transition", blockId: tourBlock.id, propertyKey: "complete" }); | ||
} else { | ||
const newIndex = currentIndex + 1; | ||
updateTourState(tourBlock.id, (t) => ({ ...t, currentBlockIndex: newIndex })); | ||
void sendEvent({ | ||
name: "tour-update", | ||
blockId: tourBlock.id, | ||
properties: { currentTourIndex: newIndex }, | ||
}); | ||
} | ||
}; | ||
|
||
export const cancelTour = (tourBlockId: string): void => { | ||
hideTour(tourBlockId); | ||
void sendEvent({ name: "transition", blockId: tourBlockId, propertyKey: "cancel" }); | ||
}; | ||
|
||
export const handleTourDocumentClick = (eventTarget: Element): void => { | ||
const currentPathname = getPathname(); | ||
|
||
const blocksById = new Map(blocks.value.map((block) => [block.id, block])); | ||
runningTours.value.forEach((tour) => { | ||
const tourBlock = blocksById.get(tour.blockId); | ||
if (!tourBlock) return; | ||
const activeStep = tourBlock.tourBlocks?.at(tour.currentBlockIndex); | ||
if (!activeStep) return; | ||
const tourWait = activeStep.tourWait; | ||
if (!tourWait) return; | ||
|
||
if (tourWait.interaction === "click") { | ||
const pageMatch = pathnameMatch({ | ||
pathname: currentPathname, | ||
operator: tourWait.page?.operator, | ||
value: tourWait.page?.value, | ||
}); | ||
const clickMatch = elementContains({ eventTarget, value: tourWait.element }); | ||
if (clickMatch && pageMatch) { | ||
nextTourStep(tourBlock, tour.currentBlockIndex); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
effect(() => { | ||
const pathnameValue = pathname.value; | ||
const blocksValue = blocks.value; | ||
const runningToursValue = runningTours.value; | ||
|
||
const blocksById = new Map(blocksValue.map((block) => [block.id, block])); | ||
runningToursValue.forEach((tour) => { | ||
const tourBlock = blocksById.get(tour.blockId); | ||
if (!tourBlock) return; | ||
const activeStep = tourBlock.tourBlocks?.at(tour.currentBlockIndex); | ||
if (!activeStep) return; | ||
const tourWait = activeStep.tourWait; | ||
if (!tourWait) return; | ||
|
||
if (tourWait.interaction === "navigation") { | ||
const match = pathnameMatch({ | ||
pathname: pathnameValue, | ||
operator: tourWait.page?.operator, | ||
value: tourWait.page?.value, | ||
}); | ||
|
||
if (match) nextTourStep(tourBlock, tour.currentBlockIndex); | ||
} | ||
}); | ||
}); |
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