Line data Source code
1 : import type { Ref } from 'vue'
2 :
3 : import { computed } from 'vue'
4 :
5 : import { useAppMode } from '@/composables/useAppMode'
6 :
7 2355 : const BUILDER_STEPS = [
8 : 'builder:inputs',
9 : 'builder:outputs',
10 : 'builder:arrange'
11 : ] as const
12 :
13 : export type BuilderStepId = (typeof BUILDER_STEPS)[number]
14 :
15 2355 : const ARRANGE_INDEX = BUILDER_STEPS.indexOf('builder:arrange')
16 :
17 118 : export function useBuilderSteps(options?: { hasOutputs?: Ref<boolean> }) {
18 118 : const { mode, isBuilderMode, setMode } = useAppMode()
19 :
20 118 : const activeStep = computed<BuilderStepId>(() => {
21 234 : if (isBuilderMode.value) {
22 234 : return mode.value as BuilderStepId
23 : }
24 0 : return 'builder:inputs'
25 : })
26 :
27 118 : const activeStepIndex = computed(() =>
28 : BUILDER_STEPS.indexOf(activeStep.value)
29 : )
30 :
31 118 : const isFirstStep = computed(() => activeStepIndex.value === 0)
32 :
33 118 : const isLastStep = computed(() => {
34 151 : if (!options?.hasOutputs?.value)
35 87 : return activeStepIndex.value >= ARRANGE_INDEX
36 64 : return activeStepIndex.value >= BUILDER_STEPS.length - 1
37 : })
38 :
39 118 : const isSelectStep = computed(
40 : () =>
41 : activeStep.value === 'builder:inputs' ||
42 : activeStep.value === 'builder:outputs'
43 : )
44 :
45 0 : function goBack() {
46 0 : if (isFirstStep.value) return
47 0 : setMode(BUILDER_STEPS[activeStepIndex.value - 1])
48 : }
49 :
50 2 : function goNext() {
51 2 : if (isLastStep.value) return
52 2 : setMode(BUILDER_STEPS[activeStepIndex.value + 1])
53 : }
54 :
55 118 : return {
56 : activeStep,
57 : activeStepIndex,
58 : isFirstStep,
59 : isLastStep,
60 : isSelectStep,
61 : navigateToStep: setMode,
62 : goBack,
63 : goNext
64 : }
65 : }
|