|
| 1 | +import React, { |
| 2 | + type ReactNode, |
| 3 | + type CSSProperties, |
| 4 | + memo, |
| 5 | + useMemo, |
| 6 | + Children, |
| 7 | + cloneElement, |
| 8 | + isValidElement, |
| 9 | +} from 'react'; |
| 10 | +import { type VariantProps, cva } from 'class-variance-authority'; |
| 11 | +import { LocalAudioTrack, RemoteAudioTrack } from 'livekit-client'; |
| 12 | +import { |
| 13 | + type AgentState, |
| 14 | + type TrackReferenceOrPlaceholder, |
| 15 | + useMultibandTrackVolume, |
| 16 | +} from '@livekit/components-react'; |
| 17 | +import { cn } from '@/lib/utils'; |
| 18 | +import { |
| 19 | + type Coordinate, |
| 20 | + useAgentAudioVisualizerGridAnimator, |
| 21 | +} from '@/hooks/agents-ui/use-agent-audio-visualizer-grid'; |
| 22 | + |
| 23 | +function cloneSingleChild( |
| 24 | + children: ReactNode | ReactNode[], |
| 25 | + props?: Record<string, unknown>, |
| 26 | + key?: unknown, |
| 27 | +) { |
| 28 | + return Children.map(children, (child) => { |
| 29 | + // Checking isValidElement is the safe way and avoids a typescript error too. |
| 30 | + if (isValidElement(child) && Children.only(children)) { |
| 31 | + const childProps = child.props as Record<string, unknown>; |
| 32 | + if (childProps.className) { |
| 33 | + // make sure we retain classnames of both passed props and child |
| 34 | + props ??= {}; |
| 35 | + props.className = cn(childProps.className as string, props.className as string); |
| 36 | + props.style = { |
| 37 | + ...(childProps.style as CSSProperties), |
| 38 | + ...(props.style as CSSProperties), |
| 39 | + }; |
| 40 | + } |
| 41 | + return cloneElement(child, { ...props, key: key ? String(key) : undefined }); |
| 42 | + } |
| 43 | + return child; |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +export const AgentAudioVisualizerGridVariants = cva( |
| 48 | + [ |
| 49 | + 'grid', |
| 50 | + '[&_>_*]:size-1 [&_>_*]:rounded-full', |
| 51 | + '[&_>_*]:bg-foreground/10 [&_>_[data-lk-highlighted=true]]:bg-foreground [&_>_[data-lk-highlighted=true]]:scale-125 [&_>_[data-lk-highlighted=true]]:shadow-[0px_0px_10px_2px_rgba(255,255,255,0.4)]', |
| 52 | + ], |
| 53 | + { |
| 54 | + variants: { |
| 55 | + size: { |
| 56 | + icon: ['gap-[2px] [&_>_*]:size-[4px]'], |
| 57 | + sm: ['gap-[4px] [&_>_*]:size-[4px]'], |
| 58 | + md: ['gap-[8px] [&_>_*]:size-[8px]'], |
| 59 | + lg: ['gap-[8px] [&_>_*]:size-[8px]'], |
| 60 | + xl: ['gap-[8px] [&_>_*]:size-[8px]'], |
| 61 | + }, |
| 62 | + }, |
| 63 | + defaultVariants: { |
| 64 | + size: 'md', |
| 65 | + }, |
| 66 | + }, |
| 67 | +); |
| 68 | + |
| 69 | +export interface GridOptions { |
| 70 | + radius?: number; |
| 71 | + interval?: number; |
| 72 | + rowCount?: number; |
| 73 | + columnCount?: number; |
| 74 | + transformer?: (index: number, rowCount: number, columnCount: number) => CSSProperties; |
| 75 | + className?: string; |
| 76 | + children?: ReactNode; |
| 77 | +} |
| 78 | + |
| 79 | +const sizeDefaults = { |
| 80 | + icon: 3, |
| 81 | + sm: 5, |
| 82 | + md: 5, |
| 83 | + lg: 5, |
| 84 | + xl: 5, |
| 85 | +}; |
| 86 | + |
| 87 | +function useGrid( |
| 88 | + size: VariantProps<typeof AgentAudioVisualizerGridVariants>['size'] = 'md', |
| 89 | + columnCount = sizeDefaults[size as keyof typeof sizeDefaults], |
| 90 | + rowCount = sizeDefaults[size as keyof typeof sizeDefaults], |
| 91 | +) { |
| 92 | + return useMemo(() => { |
| 93 | + const _columnCount = columnCount; |
| 94 | + const _rowCount = rowCount ?? columnCount; |
| 95 | + const items = new Array(_columnCount * _rowCount).fill(0).map((_, idx) => idx); |
| 96 | + |
| 97 | + return { columnCount: _columnCount, rowCount: _rowCount, items }; |
| 98 | + }, [columnCount, rowCount]); |
| 99 | +} |
| 100 | + |
| 101 | +interface GridCellProps { |
| 102 | + index: number; |
| 103 | + state: AgentState; |
| 104 | + interval: number; |
| 105 | + transformer?: (index: number, rowCount: number, columnCount: number) => CSSProperties; |
| 106 | + rowCount: number; |
| 107 | + columnCount: number; |
| 108 | + volumeBands: number[]; |
| 109 | + highlightedCoordinate: Coordinate; |
| 110 | + children: ReactNode; |
| 111 | +} |
| 112 | + |
| 113 | +const GridCell = memo(function GridCell({ |
| 114 | + index, |
| 115 | + state, |
| 116 | + interval, |
| 117 | + transformer, |
| 118 | + rowCount, |
| 119 | + columnCount, |
| 120 | + volumeBands, |
| 121 | + highlightedCoordinate, |
| 122 | + children, |
| 123 | +}: GridCellProps) { |
| 124 | + if (state === 'speaking') { |
| 125 | + const y = Math.floor(index / columnCount); |
| 126 | + const rowMidPoint = Math.floor(rowCount / 2); |
| 127 | + const volumeChunks = 1 / (rowMidPoint + 1); |
| 128 | + const distanceToMid = Math.abs(rowMidPoint - y); |
| 129 | + const threshold = distanceToMid * volumeChunks; |
| 130 | + const isHighlighted = volumeBands[index % columnCount] >= threshold; |
| 131 | + |
| 132 | + return cloneSingleChild(children, { |
| 133 | + 'data-lk-index': index, |
| 134 | + 'data-lk-highlighted': isHighlighted, |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + let transformerStyle: CSSProperties | undefined; |
| 139 | + if (transformer) { |
| 140 | + transformerStyle = transformer(index, rowCount, columnCount); |
| 141 | + } |
| 142 | + |
| 143 | + const isHighlighted = |
| 144 | + highlightedCoordinate.x === index % columnCount && |
| 145 | + highlightedCoordinate.y === Math.floor(index / columnCount); |
| 146 | + |
| 147 | + const transitionDurationInSeconds = interval / (isHighlighted ? 1000 : 100); |
| 148 | + |
| 149 | + return cloneSingleChild(children, { |
| 150 | + 'data-lk-index': index, |
| 151 | + 'data-lk-highlighted': isHighlighted, |
| 152 | + style: { |
| 153 | + transitionProperty: 'all', |
| 154 | + transitionDuration: `${transitionDurationInSeconds}s`, |
| 155 | + transitionTimingFunction: 'ease-out', |
| 156 | + ...transformerStyle, |
| 157 | + }, |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +export type AgentAudioVisualizerGridProps = GridOptions & { |
| 162 | + state: AgentState; |
| 163 | + audioTrack?: LocalAudioTrack | RemoteAudioTrack | TrackReferenceOrPlaceholder; |
| 164 | + className?: string; |
| 165 | + children?: ReactNode; |
| 166 | +} & VariantProps<typeof AgentAudioVisualizerGridVariants>; |
| 167 | + |
| 168 | +export function AgentAudioVisualizerGrid({ |
| 169 | + size = 'md', |
| 170 | + state, |
| 171 | + radius, |
| 172 | + rowCount: _rowCount = 5, |
| 173 | + columnCount: _columnCount = 5, |
| 174 | + transformer, |
| 175 | + interval = 100, |
| 176 | + className, |
| 177 | + children, |
| 178 | + audioTrack, |
| 179 | +}: AgentAudioVisualizerGridProps) { |
| 180 | + const { columnCount, rowCount, items } = useGrid(size, _columnCount, _rowCount); |
| 181 | + const highlightedCoordinate = useAgentAudioVisualizerGridAnimator( |
| 182 | + state, |
| 183 | + rowCount, |
| 184 | + columnCount, |
| 185 | + interval, |
| 186 | + radius, |
| 187 | + ); |
| 188 | + const volumeBands = useMultibandTrackVolume(audioTrack, { |
| 189 | + bands: columnCount, |
| 190 | + loPass: 100, |
| 191 | + hiPass: 200, |
| 192 | + }); |
| 193 | + |
| 194 | + return ( |
| 195 | + <div |
| 196 | + className={cn(AgentAudioVisualizerGridVariants({ size }), className)} |
| 197 | + style={{ gridTemplateColumns: `repeat(${columnCount}, 1fr)` }} |
| 198 | + > |
| 199 | + {items.map((idx) => ( |
| 200 | + <GridCell |
| 201 | + key={idx} |
| 202 | + index={idx} |
| 203 | + state={state} |
| 204 | + interval={interval} |
| 205 | + transformer={transformer} |
| 206 | + rowCount={rowCount} |
| 207 | + columnCount={columnCount} |
| 208 | + volumeBands={volumeBands} |
| 209 | + highlightedCoordinate={highlightedCoordinate} |
| 210 | + > |
| 211 | + {children ?? <div />} |
| 212 | + </GridCell> |
| 213 | + ))} |
| 214 | + </div> |
| 215 | + ); |
| 216 | +} |
0 commit comments