|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { |
| 4 | + type CSSProperties, |
| 5 | + Children, |
| 6 | + type ReactNode, |
| 7 | + cloneElement, |
| 8 | + isValidElement, |
| 9 | + useMemo, |
| 10 | +} from 'react'; |
| 11 | +import { type VariantProps, cva } from 'class-variance-authority'; |
| 12 | +import { type LocalAudioTrack, type RemoteAudioTrack } from 'livekit-client'; |
| 13 | +import { |
| 14 | + type AgentState, |
| 15 | + type TrackReferenceOrPlaceholder, |
| 16 | + useMultibandTrackVolume, |
| 17 | +} from '@livekit/components-react'; |
| 18 | +import { useAgentAudioVisualizerBarAnimator } from '@/hooks/agents-ui/use-agent-audio-visualizer-bar'; |
| 19 | +import { cn } from '@/lib/utils'; |
| 20 | + |
| 21 | +function cloneSingleChild( |
| 22 | + children: ReactNode | ReactNode[], |
| 23 | + props?: Record<string, unknown>, |
| 24 | + key?: unknown |
| 25 | +) { |
| 26 | + return Children.map(children, (child) => { |
| 27 | + // Checking isValidElement is the safe way and avoids a typescript error too. |
| 28 | + if (isValidElement(child) && Children.only(children)) { |
| 29 | + const childProps = child.props as Record<string, unknown>; |
| 30 | + if (childProps.className) { |
| 31 | + // make sure we retain classnames of both passed props and child |
| 32 | + props ??= {}; |
| 33 | + props.className = cn(childProps.className as string, props.className as string); |
| 34 | + props.style = { |
| 35 | + ...(childProps.style as CSSProperties), |
| 36 | + ...(props.style as CSSProperties), |
| 37 | + }; |
| 38 | + } |
| 39 | + return cloneElement(child, { ...props, key: key ? String(key) : undefined }); |
| 40 | + } |
| 41 | + return child; |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +export const AgentAudioVisualizerBarVariants = cva( |
| 46 | + [ |
| 47 | + 'relative flex items-center justify-center', |
| 48 | + '[&_>_*]:rounded-full [&_>_*]:transition-colors [&_>_*]:duration-250 [&_>_*]:ease-linear', |
| 49 | + '[&_>_*]:bg-transparent [&_>_*]:data-[lk-highlighted=true]:bg-current', |
| 50 | + ], |
| 51 | + { |
| 52 | + variants: { |
| 53 | + size: { |
| 54 | + icon: ['h-[24px] gap-[2px]', '[&_>_*]:w-[4px] [&_>_*]:min-h-[4px]'], |
| 55 | + sm: ['h-[56px] gap-[4px]', '[&_>_*]:w-[8px] [&_>_*]:min-h-[8px]'], |
| 56 | + md: ['h-[112px] gap-[8px]', '[&_>_*]:w-[16px] [&_>_*]:min-h-[16px]'], |
| 57 | + lg: ['h-[224px] gap-[16px]', '[&_>_*]:w-[32px] [&_>_*]:min-h-[32px]'], |
| 58 | + xl: ['h-[448px] gap-[32px]', '[&_>_*]:w-[64px] [&_>_*]:min-h-[64px]'], |
| 59 | + }, |
| 60 | + }, |
| 61 | + defaultVariants: { |
| 62 | + size: 'md', |
| 63 | + }, |
| 64 | + } |
| 65 | +); |
| 66 | + |
| 67 | +export interface AgentAudioVisualizerBarProps { |
| 68 | + state?: AgentState; |
| 69 | + barCount?: number; |
| 70 | + audioTrack?: LocalAudioTrack | RemoteAudioTrack | TrackReferenceOrPlaceholder; |
| 71 | + className?: string; |
| 72 | + children?: ReactNode | ReactNode[]; |
| 73 | +} |
| 74 | + |
| 75 | +export function AgentAudioVisualizerBar({ |
| 76 | + size = 'md', |
| 77 | + state = 'connecting', |
| 78 | + barCount, |
| 79 | + audioTrack, |
| 80 | + className, |
| 81 | + children, |
| 82 | +}: AgentAudioVisualizerBarProps & VariantProps<typeof AgentAudioVisualizerBarVariants>) { |
| 83 | + const _barCount = useMemo(() => { |
| 84 | + if (barCount) { |
| 85 | + return barCount; |
| 86 | + } |
| 87 | + switch (size) { |
| 88 | + case 'icon': |
| 89 | + case 'sm': |
| 90 | + return 3; |
| 91 | + default: |
| 92 | + return 5; |
| 93 | + } |
| 94 | + }, [barCount, size]); |
| 95 | + |
| 96 | + const volumeBands = useMultibandTrackVolume(audioTrack, { |
| 97 | + bands: _barCount, |
| 98 | + loPass: 100, |
| 99 | + hiPass: 200, |
| 100 | + }); |
| 101 | + |
| 102 | + const sequencerInterval = useMemo(() => { |
| 103 | + switch (state) { |
| 104 | + case 'connecting': |
| 105 | + return 2000 / _barCount; |
| 106 | + case 'initializing': |
| 107 | + return 2000; |
| 108 | + case 'listening': |
| 109 | + return 500; |
| 110 | + case 'thinking': |
| 111 | + return 150; |
| 112 | + default: |
| 113 | + return 1000; |
| 114 | + } |
| 115 | + }, [state, _barCount]); |
| 116 | + |
| 117 | + const highlightedIndices = useAgentAudioVisualizerBarAnimator( |
| 118 | + state, |
| 119 | + _barCount, |
| 120 | + sequencerInterval |
| 121 | + ); |
| 122 | + |
| 123 | + const bands = useMemo( |
| 124 | + () => (state === 'speaking' ? volumeBands : new Array(_barCount).fill(0)), |
| 125 | + [state, volumeBands, _barCount] |
| 126 | + ); |
| 127 | + |
| 128 | + return ( |
| 129 | + <div className={cn(AgentAudioVisualizerBarVariants({ size }), className)}> |
| 130 | + {bands.map((band: number, idx: number) => |
| 131 | + children ? ( |
| 132 | + <React.Fragment key={idx}> |
| 133 | + {cloneSingleChild(children, { |
| 134 | + 'data-lk-index': idx, |
| 135 | + 'data-lk-highlighted': highlightedIndices.includes(idx), |
| 136 | + style: { height: `${band * 100}%` }, |
| 137 | + })} |
| 138 | + </React.Fragment> |
| 139 | + ) : ( |
| 140 | + <div |
| 141 | + key={idx} |
| 142 | + data-lk-index={idx} |
| 143 | + data-lk-highlighted={highlightedIndices.includes(idx)} |
| 144 | + style={{ height: `${band * 100}%` }} |
| 145 | + /> |
| 146 | + ) |
| 147 | + )} |
| 148 | + </div> |
| 149 | + ); |
| 150 | +} |
0 commit comments