|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed } from 'vue' |
| 3 | +import { Phone, ArrowRight } from 'lucide-vue-next' |
| 4 | +
|
| 5 | +interface Step { |
| 6 | + digit?: string |
| 7 | + label?: string |
| 8 | + action?: string |
| 9 | + flow?: string |
| 10 | +} |
| 11 | +
|
| 12 | +interface TreeNode { |
| 13 | + step: Step |
| 14 | + children: TreeNode[] |
| 15 | +} |
| 16 | +
|
| 17 | +const props = defineProps<{ |
| 18 | + steps: Step[] |
| 19 | +}>() |
| 20 | +
|
| 21 | +function isFlowStep(step: Step) { |
| 22 | + return step.action === 'flow_start' || (step.action === 'goto_flow' && step.flow) |
| 23 | +} |
| 24 | +
|
| 25 | +// Build a tree from the flat step list. |
| 26 | +// flow_start / goto_flow (with flow field) → creates a group, children nested under it. |
| 27 | +// submenu → creates a node, children nested under it. |
| 28 | +// parent → leaf, pops one level. |
| 29 | +// everything else → leaf node. |
| 30 | +const tree = computed(() => { |
| 31 | + const root: TreeNode[] = [] |
| 32 | + const stack: TreeNode[][] = [root] |
| 33 | +
|
| 34 | + for (const step of props.steps) { |
| 35 | + const current = stack[stack.length - 1] |
| 36 | +
|
| 37 | + if (isFlowStep(step)) { |
| 38 | + // Flow nodes nest all subsequent steps as children |
| 39 | + const node: TreeNode = { step, children: [] } |
| 40 | + current.push(node) |
| 41 | + stack.push(node.children) |
| 42 | + } else if (step.action === 'submenu') { |
| 43 | + const node: TreeNode = { step, children: [] } |
| 44 | + current.push(node) |
| 45 | + stack.push(node.children) |
| 46 | + } else if (step.action === 'parent') { |
| 47 | + current.push({ step, children: [] }) |
| 48 | + if (stack.length > 1) stack.pop() |
| 49 | + } else { |
| 50 | + current.push({ step, children: [] }) |
| 51 | + } |
| 52 | + } |
| 53 | +
|
| 54 | + return root |
| 55 | +}) |
| 56 | +</script> |
| 57 | + |
| 58 | +<template> |
| 59 | + <div class="ivr-path-tree"> |
| 60 | + <TreeNodes :nodes="tree" :depth="0" /> |
| 61 | + </div> |
| 62 | +</template> |
| 63 | + |
| 64 | +<script lang="ts"> |
| 65 | +import { defineComponent, h, type PropType } from 'vue' |
| 66 | +
|
| 67 | +interface TNode { |
| 68 | + step: { digit?: string; label?: string; action?: string; flow?: string } |
| 69 | + children: TNode[] |
| 70 | +} |
| 71 | +
|
| 72 | +const TreeNodes = defineComponent({ |
| 73 | + name: 'TreeNodes', |
| 74 | + props: { |
| 75 | + nodes: { type: Array as PropType<TNode[]>, required: true }, |
| 76 | + depth: { type: Number, default: 0 } |
| 77 | + }, |
| 78 | + setup(props) { |
| 79 | + return () => { |
| 80 | + if (!props.nodes.length) return null |
| 81 | +
|
| 82 | + return h('div', { class: 'flex flex-col gap-1' }, |
| 83 | + props.nodes.map((node, idx) => { |
| 84 | + const isFlow = node.step.action === 'flow_start' || |
| 85 | + (node.step.action === 'goto_flow' && node.step.flow) |
| 86 | + const isGotoFlow = node.step.action === 'goto_flow' && node.step.flow |
| 87 | + const hasChildren = node.children.length > 0 |
| 88 | +
|
| 89 | + // The row content |
| 90 | + let rowContent |
| 91 | + if (isFlow) { |
| 92 | + const flowParts = [] |
| 93 | +
|
| 94 | + // For goto_flow that has a digit, show the digit badge first |
| 95 | + if (isGotoFlow && node.step.digit) { |
| 96 | + flowParts.push( |
| 97 | + h('div', { |
| 98 | + class: 'flex items-center justify-center h-6 w-6 rounded border bg-muted text-xs font-mono font-bold shrink-0' |
| 99 | + }, node.step.digit) |
| 100 | + ) |
| 101 | + flowParts.push( |
| 102 | + h(ArrowRight, { class: 'h-3.5 w-3.5 text-muted-foreground shrink-0' }) |
| 103 | + ) |
| 104 | + } |
| 105 | +
|
| 106 | + flowParts.push( |
| 107 | + h('div', { |
| 108 | + class: 'flex items-center justify-center h-6 w-6 rounded-full bg-primary shrink-0' |
| 109 | + }, [h(Phone, { class: 'h-3 w-3 text-primary-foreground' })]) |
| 110 | + ) |
| 111 | + flowParts.push( |
| 112 | + h('span', { class: 'font-semibold text-sm' }, node.step.flow || '?') |
| 113 | + ) |
| 114 | +
|
| 115 | + rowContent = h('div', { class: 'flex items-center gap-2 py-1' }, flowParts) |
| 116 | + } else if (node.step.action === 'submenu') { |
| 117 | + rowContent = h('div', { class: 'flex items-center gap-2 py-1' }, [ |
| 118 | + h('div', { |
| 119 | + class: 'flex items-center justify-center h-6 w-6 rounded border bg-muted text-xs font-mono font-bold shrink-0' |
| 120 | + }, node.step.digit || '?'), |
| 121 | + h('span', { class: 'text-sm font-medium' }, node.step.label || '-'), |
| 122 | + ]) |
| 123 | + } else { |
| 124 | + rowContent = h('div', { class: 'flex items-center gap-2 py-1' }, [ |
| 125 | + h('div', { |
| 126 | + class: 'flex items-center justify-center h-6 w-6 rounded border bg-muted text-xs font-mono font-bold shrink-0' |
| 127 | + }, node.step.digit || '?'), |
| 128 | + h('span', { class: 'text-sm' }, node.step.label || '-'), |
| 129 | + ]) |
| 130 | + } |
| 131 | +
|
| 132 | + // Build the node with optional children |
| 133 | + const elements = [rowContent] |
| 134 | +
|
| 135 | + if (hasChildren) { |
| 136 | + elements.push( |
| 137 | + h('div', { |
| 138 | + class: 'ml-3 pl-4 border-l-2 border-muted-foreground/30' |
| 139 | + }, [ |
| 140 | + h(TreeNodes, { nodes: node.children, depth: props.depth + 1 }) |
| 141 | + ]) |
| 142 | + ) |
| 143 | + } |
| 144 | +
|
| 145 | + return h('div', { key: idx }, elements) |
| 146 | + }) |
| 147 | + ) |
| 148 | + } |
| 149 | + } |
| 150 | +}) |
| 151 | +</script> |
0 commit comments