@istmx/ui 0.1.8 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +115 -1
- package/bin/istmx.mjs +53 -12
- package/package.json +2 -2
- package/registry/ai-chat-block/ai-chat-block.tsx +460 -0
- package/registry/ai-chat-block/ai-chat-input.tsx +690 -0
- package/registry/ai-chat-block/attachment.tsx +605 -0
- package/registry/ai-chat-block/chat-code-block.tsx +50 -0
- package/registry/ai-chat-block/chat-composer.tsx +51 -0
- package/registry/ai-chat-block/chat-logo.tsx +18 -0
- package/registry/ai-chat-block/chat-message.tsx +161 -0
- package/registry/ai-chat-block/chat-sidebar.tsx +273 -0
- package/registry/ai-chat-block/chat-user-avatar.tsx +9 -0
- package/registry/ai-chat-block/code-highlighter.tsx +357 -0
- package/registry/ai-chat-block/default-attachments.tsx +42 -0
- package/registry/ai-chat-block/index.ts +16 -0
- package/registry/ai-chat-block/message-content.tsx +287 -0
- package/registry/ai-chat-block/message-list.tsx +93 -0
- package/registry/ai-chat-block/send-button.tsx +102 -0
- package/registry/ai-chat-block/types.ts +71 -0
- package/registry/ai-chat-block/utils.ts +6 -0
- package/registry/ai-chat-block/voice-button.tsx +66 -0
- package/registry/ai-chat-input/ai-chat-input.tsx +690 -0
- package/registry/ai-chat-input/attachment.tsx +605 -0
- package/registry/ai-chat-input/send-button.tsx +102 -0
- package/registry/ai-chat-input/types.ts +71 -0
- package/registry/ai-chat-input/utils.ts +6 -0
- package/registry/ai-chat-input/voice-button.tsx +66 -0
- package/registry/button.tsx +4 -7
- package/registry/streaming-text.tsx +278 -0
- package/registry/text-reveal.tsx +307 -0
- package/registry/image-effects-card.tsx +0 -197
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useId, useRef, useState, useSyncExternalStore } from 'react'
|
|
4
|
+
import type { CSSProperties, RefObject } from 'react'
|
|
5
|
+
import { createPortal } from 'react-dom'
|
|
6
|
+
import { AnimatePresence, motion, useReducedMotion } from 'motion/react'
|
|
7
|
+
import {
|
|
8
|
+
IconArrowRight,
|
|
9
|
+
IconFile,
|
|
10
|
+
IconHeadphones,
|
|
11
|
+
IconPaperclip,
|
|
12
|
+
IconPhoto,
|
|
13
|
+
IconVideo,
|
|
14
|
+
IconX,
|
|
15
|
+
IconZoomIn,
|
|
16
|
+
} from '@tabler/icons-react'
|
|
17
|
+
import { cn } from './utils'
|
|
18
|
+
import type { AttachmentOption, ChatAttachmentKind } from './types'
|
|
19
|
+
|
|
20
|
+
type AnimatedAccentBorderProps = {
|
|
21
|
+
color: string
|
|
22
|
+
duration?: number
|
|
23
|
+
intensity?: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function AnimatedAccentBorder({
|
|
27
|
+
color,
|
|
28
|
+
duration = 5,
|
|
29
|
+
intensity = 1,
|
|
30
|
+
}: AnimatedAccentBorderProps) {
|
|
31
|
+
const borderRef = useRef<SVGSVGElement>(null)
|
|
32
|
+
const glowFilterId = `${useId().replace(/:/g, '')}-organic-glow`
|
|
33
|
+
const [size, setSize] = useState({ width: 0, height: 0 })
|
|
34
|
+
const reduceMotion = useReducedMotion()
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const border = borderRef.current
|
|
38
|
+
if (!border) return
|
|
39
|
+
|
|
40
|
+
const observer = new ResizeObserver(() => {
|
|
41
|
+
const { width, height } = border.getBoundingClientRect()
|
|
42
|
+
setSize((current) =>
|
|
43
|
+
Math.abs(current.width - width) < 0.5 &&
|
|
44
|
+
Math.abs(current.height - height) < 0.5
|
|
45
|
+
? current
|
|
46
|
+
: { width, height },
|
|
47
|
+
)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
observer.observe(border)
|
|
51
|
+
return () => observer.disconnect()
|
|
52
|
+
}, [])
|
|
53
|
+
|
|
54
|
+
const measuredWidth = Math.max(1, size.width)
|
|
55
|
+
const measuredHeight = Math.max(1, size.height)
|
|
56
|
+
const width = Math.max(0, measuredWidth - 1)
|
|
57
|
+
const height = Math.max(0, measuredHeight - 1)
|
|
58
|
+
const radius = Math.min(15.5, width / 2, height / 2)
|
|
59
|
+
const pathLength = Math.max(
|
|
60
|
+
0,
|
|
61
|
+
2 * (width + height - 4 * radius) + 2 * Math.PI * radius,
|
|
62
|
+
)
|
|
63
|
+
const dashLength = pathLength * 0.22
|
|
64
|
+
const highlightColor = `color-mix(in srgb, ${color} 62%, white)`
|
|
65
|
+
const animate = reduceMotion ? undefined : { strokeDashoffset: -pathLength }
|
|
66
|
+
const transition = {
|
|
67
|
+
duration: Math.max(1, duration),
|
|
68
|
+
ease: 'linear' as const,
|
|
69
|
+
repeat: Infinity,
|
|
70
|
+
}
|
|
71
|
+
const shape = {
|
|
72
|
+
x: 0.5,
|
|
73
|
+
y: 0.5,
|
|
74
|
+
width,
|
|
75
|
+
height,
|
|
76
|
+
rx: radius,
|
|
77
|
+
fill: 'none',
|
|
78
|
+
}
|
|
79
|
+
const animatedShape = {
|
|
80
|
+
...shape,
|
|
81
|
+
strokeDasharray: `${dashLength} ${pathLength - dashLength}`,
|
|
82
|
+
strokeLinecap: 'round' as const,
|
|
83
|
+
initial: { strokeDashoffset: 0 },
|
|
84
|
+
animate,
|
|
85
|
+
transition,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<svg
|
|
90
|
+
ref={borderRef}
|
|
91
|
+
aria-hidden="true"
|
|
92
|
+
viewBox={`0 0 ${measuredWidth} ${measuredHeight}`}
|
|
93
|
+
className="pointer-events-none absolute -top-px -left-px z-20 overflow-visible"
|
|
94
|
+
style={{
|
|
95
|
+
width: 'calc(100% + 2px)',
|
|
96
|
+
height: 'calc(100% + 2px)',
|
|
97
|
+
opacity: Math.min(1, Math.max(0, intensity)),
|
|
98
|
+
}}
|
|
99
|
+
>
|
|
100
|
+
<defs>
|
|
101
|
+
<filter
|
|
102
|
+
id={glowFilterId}
|
|
103
|
+
x="-35%"
|
|
104
|
+
y="-60%"
|
|
105
|
+
width="170%"
|
|
106
|
+
height="220%"
|
|
107
|
+
colorInterpolationFilters="sRGB"
|
|
108
|
+
>
|
|
109
|
+
<feTurbulence
|
|
110
|
+
type="fractalNoise"
|
|
111
|
+
baseFrequency="0.01 0.08"
|
|
112
|
+
numOctaves="2"
|
|
113
|
+
seed="7"
|
|
114
|
+
result="edgeNoise"
|
|
115
|
+
/>
|
|
116
|
+
<feDisplacementMap
|
|
117
|
+
in="SourceGraphic"
|
|
118
|
+
in2="edgeNoise"
|
|
119
|
+
scale="18"
|
|
120
|
+
xChannelSelector="R"
|
|
121
|
+
yChannelSelector="G"
|
|
122
|
+
result="unevenEdge"
|
|
123
|
+
/>
|
|
124
|
+
<feGaussianBlur in="unevenEdge" stdDeviation="10" />
|
|
125
|
+
</filter>
|
|
126
|
+
</defs>
|
|
127
|
+
<rect {...shape} stroke={color} strokeWidth="1.5" opacity="0.38" />
|
|
128
|
+
<rect
|
|
129
|
+
{...shape}
|
|
130
|
+
stroke={color}
|
|
131
|
+
strokeWidth="8"
|
|
132
|
+
filter={`url(#${glowFilterId})`}
|
|
133
|
+
opacity="0.55"
|
|
134
|
+
/>
|
|
135
|
+
<motion.rect
|
|
136
|
+
{...animatedShape}
|
|
137
|
+
stroke={color}
|
|
138
|
+
strokeWidth="14"
|
|
139
|
+
filter={`url(#${glowFilterId})`}
|
|
140
|
+
opacity="0.7"
|
|
141
|
+
/>
|
|
142
|
+
<motion.rect
|
|
143
|
+
{...animatedShape}
|
|
144
|
+
stroke={color}
|
|
145
|
+
strokeWidth="8"
|
|
146
|
+
filter={`url(#${glowFilterId})`}
|
|
147
|
+
opacity="0.9"
|
|
148
|
+
/>
|
|
149
|
+
<motion.rect
|
|
150
|
+
{...animatedShape}
|
|
151
|
+
stroke={highlightColor}
|
|
152
|
+
strokeWidth="3.5"
|
|
153
|
+
/>
|
|
154
|
+
</svg>
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const subscribeToNothing = () => () => {}
|
|
159
|
+
const getClientSnapshot = () => true
|
|
160
|
+
const getServerSnapshot = () => false
|
|
161
|
+
|
|
162
|
+
export type AttachmentDialogProps = {
|
|
163
|
+
id?: string
|
|
164
|
+
anchorRef: RefObject<HTMLButtonElement | null>
|
|
165
|
+
open: boolean
|
|
166
|
+
title?: string
|
|
167
|
+
description?: string
|
|
168
|
+
options: readonly AttachmentOption[]
|
|
169
|
+
onSelect: (option: AttachmentOption) => void
|
|
170
|
+
onClose: () => void
|
|
171
|
+
accentColor?: string
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export default function AttachmentDialog({
|
|
175
|
+
id,
|
|
176
|
+
anchorRef,
|
|
177
|
+
open,
|
|
178
|
+
title = 'Add to message',
|
|
179
|
+
description = 'Choose a file type to attach.',
|
|
180
|
+
options,
|
|
181
|
+
onSelect,
|
|
182
|
+
onClose,
|
|
183
|
+
accentColor,
|
|
184
|
+
}: AttachmentDialogProps) {
|
|
185
|
+
const panelRef = useRef<HTMLDivElement>(null)
|
|
186
|
+
const returnFocusRef = useRef<HTMLElement | null>(null)
|
|
187
|
+
const onCloseRef = useRef(onClose)
|
|
188
|
+
const generatedId = useId().replace(/:/g, '')
|
|
189
|
+
const titleId = `${id ?? generatedId}-title`
|
|
190
|
+
const descriptionId = `${id ?? generatedId}-description`
|
|
191
|
+
const mounted = useSyncExternalStore(
|
|
192
|
+
subscribeToNothing,
|
|
193
|
+
getClientSnapshot,
|
|
194
|
+
getServerSnapshot,
|
|
195
|
+
)
|
|
196
|
+
const [position, setPosition] = useState<{
|
|
197
|
+
left: number
|
|
198
|
+
top?: number
|
|
199
|
+
bottom?: number
|
|
200
|
+
} | null>(null)
|
|
201
|
+
const reduceMotion = useReducedMotion()
|
|
202
|
+
|
|
203
|
+
useEffect(() => {
|
|
204
|
+
onCloseRef.current = onClose
|
|
205
|
+
}, [onClose])
|
|
206
|
+
|
|
207
|
+
useEffect(() => {
|
|
208
|
+
if (!open) return
|
|
209
|
+
|
|
210
|
+
returnFocusRef.current = document.activeElement as HTMLElement | null
|
|
211
|
+
const updatePosition = () => {
|
|
212
|
+
const anchor = anchorRef.current
|
|
213
|
+
if (!anchor) return
|
|
214
|
+
const rect = anchor.getBoundingClientRect()
|
|
215
|
+
const width = Math.min(304, window.innerWidth - 24)
|
|
216
|
+
const panelHeight = Math.min(
|
|
217
|
+
panelRef.current?.offsetHeight ?? 288,
|
|
218
|
+
window.innerHeight * 0.5,
|
|
219
|
+
)
|
|
220
|
+
const left = Math.max(
|
|
221
|
+
12,
|
|
222
|
+
Math.min(rect.left, window.innerWidth - width - 12),
|
|
223
|
+
)
|
|
224
|
+
if (rect.top >= panelHeight + 20) {
|
|
225
|
+
setPosition({ left, bottom: window.innerHeight - rect.top + 8 })
|
|
226
|
+
} else {
|
|
227
|
+
setPosition({
|
|
228
|
+
left,
|
|
229
|
+
top: Math.min(rect.bottom + 8, window.innerHeight - panelHeight - 12),
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const frame = window.requestAnimationFrame(() => {
|
|
235
|
+
updatePosition()
|
|
236
|
+
panelRef.current
|
|
237
|
+
?.querySelector<HTMLButtonElement>('button:not(:disabled)')
|
|
238
|
+
?.focus()
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
window.addEventListener('resize', updatePosition)
|
|
242
|
+
window.addEventListener('scroll', updatePosition, true)
|
|
243
|
+
|
|
244
|
+
function onPointerDown(event: PointerEvent) {
|
|
245
|
+
const target = event.target
|
|
246
|
+
if (
|
|
247
|
+
target instanceof Node &&
|
|
248
|
+
!panelRef.current?.contains(target) &&
|
|
249
|
+
!anchorRef.current?.contains(target)
|
|
250
|
+
) {
|
|
251
|
+
onCloseRef.current()
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function onKeyDown(event: KeyboardEvent) {
|
|
256
|
+
if (event.key === 'Escape') {
|
|
257
|
+
event.preventDefault()
|
|
258
|
+
onCloseRef.current()
|
|
259
|
+
return
|
|
260
|
+
}
|
|
261
|
+
if (event.key !== 'Tab') return
|
|
262
|
+
|
|
263
|
+
const focusable = Array.from(
|
|
264
|
+
panelRef.current?.querySelectorAll<HTMLElement>(
|
|
265
|
+
'button:not(:disabled), [href], input:not(:disabled), [tabindex]:not([tabindex="-1"])',
|
|
266
|
+
) ?? [],
|
|
267
|
+
)
|
|
268
|
+
if (!focusable.length) return
|
|
269
|
+
const first = focusable[0]
|
|
270
|
+
const last = focusable[focusable.length - 1]
|
|
271
|
+
if (event.shiftKey && document.activeElement === first) {
|
|
272
|
+
event.preventDefault()
|
|
273
|
+
last.focus()
|
|
274
|
+
} else if (!event.shiftKey && document.activeElement === last) {
|
|
275
|
+
event.preventDefault()
|
|
276
|
+
first.focus()
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
document.addEventListener('keydown', onKeyDown)
|
|
281
|
+
document.addEventListener('pointerdown', onPointerDown)
|
|
282
|
+
return () => {
|
|
283
|
+
window.cancelAnimationFrame(frame)
|
|
284
|
+
document.removeEventListener('keydown', onKeyDown)
|
|
285
|
+
document.removeEventListener('pointerdown', onPointerDown)
|
|
286
|
+
window.removeEventListener('resize', updatePosition)
|
|
287
|
+
window.removeEventListener('scroll', updatePosition, true)
|
|
288
|
+
returnFocusRef.current?.focus()
|
|
289
|
+
}
|
|
290
|
+
}, [open, anchorRef])
|
|
291
|
+
|
|
292
|
+
const dialog = (
|
|
293
|
+
<AnimatePresence>
|
|
294
|
+
{open && position ? (
|
|
295
|
+
<motion.div
|
|
296
|
+
key="attachment-dialog-popover"
|
|
297
|
+
initial={{ opacity: 0 }}
|
|
298
|
+
animate={{ opacity: 1 }}
|
|
299
|
+
exit={{ opacity: 0 }}
|
|
300
|
+
transition={{ duration: reduceMotion ? 0.08 : 0.2 }}
|
|
301
|
+
style={{
|
|
302
|
+
left: position.left,
|
|
303
|
+
top: position.top,
|
|
304
|
+
bottom: position.bottom,
|
|
305
|
+
}}
|
|
306
|
+
className="fixed z-[100]"
|
|
307
|
+
>
|
|
308
|
+
<motion.div
|
|
309
|
+
ref={panelRef}
|
|
310
|
+
id={id ?? `${generatedId}-attachment-dialog`}
|
|
311
|
+
role="dialog"
|
|
312
|
+
aria-modal="true"
|
|
313
|
+
aria-labelledby={titleId}
|
|
314
|
+
aria-describedby={descriptionId}
|
|
315
|
+
initial={
|
|
316
|
+
reduceMotion ? { opacity: 0 } : { opacity: 0, x: 28, y: 8 }
|
|
317
|
+
}
|
|
318
|
+
animate={{ opacity: 1, x: 0, y: 0 }}
|
|
319
|
+
exit={reduceMotion ? { opacity: 0 } : { opacity: 0, x: 20 }}
|
|
320
|
+
transition={
|
|
321
|
+
reduceMotion
|
|
322
|
+
? { duration: 0.08 }
|
|
323
|
+
: { type: 'spring', stiffness: 390, damping: 34, mass: 0.75 }
|
|
324
|
+
}
|
|
325
|
+
style={
|
|
326
|
+
{ '--chat-accent': accentColor ?? '#0369a1' } as CSSProperties
|
|
327
|
+
}
|
|
328
|
+
className="text-foreground relative flex max-h-[min(50vh,18rem)] w-[min(19rem,calc(100vw-1.5rem))] flex-col rounded-2xl border border-transparent shadow-[0_18px_54px_-16px_rgba(0,0,0,0.65)]"
|
|
329
|
+
>
|
|
330
|
+
<div className="bg-background text-foreground relative z-10 flex max-h-[min(50vh,18rem)] w-full flex-col overflow-hidden rounded-2xl">
|
|
331
|
+
<div className="border-border/60 flex items-start gap-2.5 border-b px-3 py-3 sm:px-4">
|
|
332
|
+
<span className="border-border/70 bg-surface/60 text-muted flex size-8 shrink-0 items-center justify-center rounded-lg border">
|
|
333
|
+
<IconPaperclip size={17} stroke={1.7} aria-hidden="true" />
|
|
334
|
+
</span>
|
|
335
|
+
<div className="min-w-0 flex-1">
|
|
336
|
+
<h2
|
|
337
|
+
id={titleId}
|
|
338
|
+
className="font-display text-[13px] font-semibold tracking-tight"
|
|
339
|
+
>
|
|
340
|
+
{title}
|
|
341
|
+
</h2>
|
|
342
|
+
<p
|
|
343
|
+
id={descriptionId}
|
|
344
|
+
className="text-muted mt-0.5 text-[11px] leading-4"
|
|
345
|
+
>
|
|
346
|
+
{description}
|
|
347
|
+
</p>
|
|
348
|
+
</div>
|
|
349
|
+
<button
|
|
350
|
+
type="button"
|
|
351
|
+
onClick={onClose}
|
|
352
|
+
aria-label="Close attachment options"
|
|
353
|
+
className="text-muted hover:text-foreground focus-visible:outline-foreground flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-lg transition-colors focus-visible:outline-2 focus-visible:outline-offset-2"
|
|
354
|
+
>
|
|
355
|
+
<IconX size={16} stroke={1.7} aria-hidden="true" />
|
|
356
|
+
</button>
|
|
357
|
+
</div>
|
|
358
|
+
|
|
359
|
+
<div className="min-h-0 [scrollbar-width:none] overflow-y-auto overscroll-contain p-2 [&::-webkit-scrollbar]:hidden">
|
|
360
|
+
{options.map((option) => (
|
|
361
|
+
<button
|
|
362
|
+
key={option.id}
|
|
363
|
+
type="button"
|
|
364
|
+
disabled={option.disabled}
|
|
365
|
+
onClick={() => onSelect(option)}
|
|
366
|
+
className="group focus-visible:outline-foreground hover:bg-surface/70 focus-visible:bg-surface/70 flex w-full cursor-pointer items-center gap-2.5 rounded-lg px-2 py-2 text-left transition-colors focus-visible:outline-2 focus-visible:outline-offset-[-2px] disabled:cursor-not-allowed disabled:opacity-45 sm:px-2.5"
|
|
367
|
+
>
|
|
368
|
+
<span className="border-border/70 bg-surface/45 text-muted flex size-8 shrink-0 items-center justify-center rounded-lg border transition-colors group-hover:border-[var(--chat-accent,var(--foreground))]/40 group-hover:text-[var(--chat-accent,var(--foreground))]">
|
|
369
|
+
{option.icon ?? (
|
|
370
|
+
<IconPaperclip
|
|
371
|
+
size={17}
|
|
372
|
+
stroke={1.7}
|
|
373
|
+
aria-hidden="true"
|
|
374
|
+
/>
|
|
375
|
+
)}
|
|
376
|
+
</span>
|
|
377
|
+
<span className="min-w-0 flex-1">
|
|
378
|
+
<span className="font-display block text-xs font-medium">
|
|
379
|
+
{option.label}
|
|
380
|
+
</span>
|
|
381
|
+
<span className="text-muted mt-0.5 block text-[10px] leading-3.5">
|
|
382
|
+
{option.description ?? option.accept}
|
|
383
|
+
</span>
|
|
384
|
+
</span>
|
|
385
|
+
<IconArrowRight
|
|
386
|
+
size={15}
|
|
387
|
+
stroke={1.6}
|
|
388
|
+
aria-hidden="true"
|
|
389
|
+
className="text-muted/45 shrink-0 transition-transform group-hover:translate-x-0.5 group-hover:text-[var(--chat-accent,var(--foreground))]"
|
|
390
|
+
/>
|
|
391
|
+
</button>
|
|
392
|
+
))}
|
|
393
|
+
</div>
|
|
394
|
+
<div className="border-border/50 text-muted border-t px-3 py-2 font-mono text-[9px] tracking-[0.12em] uppercase sm:px-4">
|
|
395
|
+
{options.length} attachment{' '}
|
|
396
|
+
{options.length === 1 ? 'option' : 'options'}
|
|
397
|
+
</div>
|
|
398
|
+
</div>
|
|
399
|
+
<AnimatedAccentBorder color={accentColor ?? '#0369a1'} />
|
|
400
|
+
</motion.div>
|
|
401
|
+
</motion.div>
|
|
402
|
+
) : null}
|
|
403
|
+
</AnimatePresence>
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
return mounted ? createPortal(dialog, document.body) : null
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
type AttachmentPreviewProps = {
|
|
410
|
+
file: File
|
|
411
|
+
name: string
|
|
412
|
+
kind: ChatAttachmentKind
|
|
413
|
+
src?: string
|
|
414
|
+
thumbnailClassName?: string
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function PreviewIcon({ kind }: { kind: ChatAttachmentKind }) {
|
|
418
|
+
const Icon =
|
|
419
|
+
kind === 'image'
|
|
420
|
+
? IconPhoto
|
|
421
|
+
: kind === 'video'
|
|
422
|
+
? IconVideo
|
|
423
|
+
: kind === 'audio'
|
|
424
|
+
? IconHeadphones
|
|
425
|
+
: IconFile
|
|
426
|
+
return <Icon size={18} stroke={1.7} aria-hidden="true" />
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export function AttachmentPreview({
|
|
430
|
+
file,
|
|
431
|
+
name,
|
|
432
|
+
kind,
|
|
433
|
+
src,
|
|
434
|
+
thumbnailClassName,
|
|
435
|
+
}: AttachmentPreviewProps) {
|
|
436
|
+
const [open, setOpen] = useState(false)
|
|
437
|
+
const [previewUrl, setPreviewUrl] = useState(src ?? '')
|
|
438
|
+
const fileUrlRef = useRef('')
|
|
439
|
+
const thumbnailRef = useRef<HTMLImageElement>(null)
|
|
440
|
+
const closeRef = useRef<HTMLButtonElement>(null)
|
|
441
|
+
const triggerRef = useRef<HTMLButtonElement>(null)
|
|
442
|
+
const reduceMotion = useReducedMotion()
|
|
443
|
+
|
|
444
|
+
useEffect(() => {
|
|
445
|
+
if (src) return
|
|
446
|
+
const url = URL.createObjectURL(file)
|
|
447
|
+
fileUrlRef.current = url
|
|
448
|
+
if (thumbnailRef.current) thumbnailRef.current.src = url
|
|
449
|
+
return () => {
|
|
450
|
+
fileUrlRef.current = ''
|
|
451
|
+
URL.revokeObjectURL(url)
|
|
452
|
+
}
|
|
453
|
+
}, [file, src])
|
|
454
|
+
|
|
455
|
+
useEffect(() => {
|
|
456
|
+
if (!open) return
|
|
457
|
+
closeRef.current?.focus()
|
|
458
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
459
|
+
if (event.key === 'Escape') setOpen(false)
|
|
460
|
+
}
|
|
461
|
+
const trigger = triggerRef.current
|
|
462
|
+
document.addEventListener('keydown', onKeyDown)
|
|
463
|
+
return () => {
|
|
464
|
+
document.removeEventListener('keydown', onKeyDown)
|
|
465
|
+
trigger?.focus()
|
|
466
|
+
}
|
|
467
|
+
}, [open])
|
|
468
|
+
|
|
469
|
+
const dialog = (
|
|
470
|
+
<AnimatePresence>
|
|
471
|
+
{open ? (
|
|
472
|
+
<motion.div
|
|
473
|
+
className="fixed inset-0 z-[500] flex items-center justify-center p-3 sm:p-8"
|
|
474
|
+
initial={{ opacity: 0 }}
|
|
475
|
+
animate={{ opacity: 1 }}
|
|
476
|
+
exit={{ opacity: 0 }}
|
|
477
|
+
transition={{ duration: reduceMotion ? 0.08 : 0.18 }}
|
|
478
|
+
role="presentation"
|
|
479
|
+
onMouseDown={(event) => {
|
|
480
|
+
if (event.target === event.currentTarget) setOpen(false)
|
|
481
|
+
}}
|
|
482
|
+
>
|
|
483
|
+
<div
|
|
484
|
+
className="absolute inset-0 bg-black/80 backdrop-blur-sm"
|
|
485
|
+
aria-hidden="true"
|
|
486
|
+
/>
|
|
487
|
+
<motion.section
|
|
488
|
+
role="dialog"
|
|
489
|
+
aria-modal="true"
|
|
490
|
+
aria-label={`Preview: ${name}`}
|
|
491
|
+
className="border-border/70 bg-background relative z-10 flex max-h-[92dvh] w-full max-w-5xl flex-col overflow-hidden rounded-2xl border shadow-2xl"
|
|
492
|
+
initial={reduceMotion ? { scale: 1 } : { scale: 0.96, y: 12 }}
|
|
493
|
+
animate={{ scale: 1, y: 0 }}
|
|
494
|
+
exit={reduceMotion ? { scale: 1 } : { scale: 0.98, y: 8 }}
|
|
495
|
+
transition={
|
|
496
|
+
reduceMotion
|
|
497
|
+
? { duration: 0.08 }
|
|
498
|
+
: { type: 'spring', stiffness: 360, damping: 30 }
|
|
499
|
+
}
|
|
500
|
+
>
|
|
501
|
+
<div className="border-border/60 flex min-h-12 items-center gap-3 border-b px-3 py-2.5 sm:px-4">
|
|
502
|
+
<span className="text-muted bg-surface/70 flex size-8 shrink-0 items-center justify-center rounded-lg">
|
|
503
|
+
<PreviewIcon kind={kind} />
|
|
504
|
+
</span>
|
|
505
|
+
<span className="min-w-0 flex-1 truncate text-sm font-medium">
|
|
506
|
+
{name}
|
|
507
|
+
</span>
|
|
508
|
+
<button
|
|
509
|
+
ref={closeRef}
|
|
510
|
+
type="button"
|
|
511
|
+
onClick={() => setOpen(false)}
|
|
512
|
+
aria-label="Close file preview"
|
|
513
|
+
className="text-muted hover:bg-surface hover:text-foreground flex size-8 shrink-0 cursor-pointer items-center justify-center rounded-lg transition-colors"
|
|
514
|
+
>
|
|
515
|
+
<IconX size={17} stroke={1.7} aria-hidden="true" />
|
|
516
|
+
</button>
|
|
517
|
+
</div>
|
|
518
|
+
<div className="flex min-h-0 flex-1 items-center justify-center overflow-auto bg-black/20 p-3 sm:p-6">
|
|
519
|
+
{kind === 'image' && previewUrl ? (
|
|
520
|
+
// eslint-disable-next-line @next/next/no-img-element
|
|
521
|
+
<img
|
|
522
|
+
src={previewUrl}
|
|
523
|
+
alt={name}
|
|
524
|
+
className="max-h-[calc(92dvh-8rem)] max-w-full rounded-lg object-contain"
|
|
525
|
+
/>
|
|
526
|
+
) : kind === 'video' && previewUrl ? (
|
|
527
|
+
<video
|
|
528
|
+
src={previewUrl}
|
|
529
|
+
controls
|
|
530
|
+
playsInline
|
|
531
|
+
className="max-h-[calc(92dvh-8rem)] max-w-full rounded-lg"
|
|
532
|
+
/>
|
|
533
|
+
) : kind === 'audio' && previewUrl ? (
|
|
534
|
+
<audio src={previewUrl} controls className="w-full max-w-xl" />
|
|
535
|
+
) : file.type === 'application/pdf' && previewUrl ? (
|
|
536
|
+
<iframe
|
|
537
|
+
title={`Preview: ${name}`}
|
|
538
|
+
src={previewUrl}
|
|
539
|
+
className="h-[min(72vh,48rem)] w-full rounded-lg border-0"
|
|
540
|
+
/>
|
|
541
|
+
) : (
|
|
542
|
+
<div className="text-muted flex flex-col items-center gap-3 py-12 text-center">
|
|
543
|
+
<span className="bg-surface/70 flex size-14 items-center justify-center rounded-2xl">
|
|
544
|
+
<PreviewIcon kind={kind} />
|
|
545
|
+
</span>
|
|
546
|
+
<p className="max-w-sm text-sm">
|
|
547
|
+
A preview isn’t available for this file type.
|
|
548
|
+
</p>
|
|
549
|
+
<a
|
|
550
|
+
href={previewUrl}
|
|
551
|
+
download={name}
|
|
552
|
+
className="text-foreground bg-surface hover:bg-surface/80 rounded-lg px-3 py-2 text-xs font-medium"
|
|
553
|
+
>
|
|
554
|
+
Download file
|
|
555
|
+
</a>
|
|
556
|
+
</div>
|
|
557
|
+
)}
|
|
558
|
+
</div>
|
|
559
|
+
</motion.section>
|
|
560
|
+
</motion.div>
|
|
561
|
+
) : null}
|
|
562
|
+
</AnimatePresence>
|
|
563
|
+
)
|
|
564
|
+
|
|
565
|
+
return (
|
|
566
|
+
<>
|
|
567
|
+
<button
|
|
568
|
+
ref={triggerRef}
|
|
569
|
+
type="button"
|
|
570
|
+
onClick={() => {
|
|
571
|
+
setPreviewUrl(src ?? fileUrlRef.current)
|
|
572
|
+
setOpen(true)
|
|
573
|
+
}}
|
|
574
|
+
aria-label={`Preview ${name}`}
|
|
575
|
+
title={`Preview ${name}`}
|
|
576
|
+
className={cn(
|
|
577
|
+
'group/preview border-border/60 bg-background/70 text-muted relative flex shrink-0 cursor-zoom-in items-center justify-center overflow-hidden rounded-lg border',
|
|
578
|
+
thumbnailClassName,
|
|
579
|
+
)}
|
|
580
|
+
>
|
|
581
|
+
{kind === 'image' && previewUrl ? (
|
|
582
|
+
// eslint-disable-next-line @next/next/no-img-element
|
|
583
|
+
<img
|
|
584
|
+
ref={thumbnailRef}
|
|
585
|
+
src={previewUrl}
|
|
586
|
+
alt=""
|
|
587
|
+
aria-hidden="true"
|
|
588
|
+
className="size-full object-cover"
|
|
589
|
+
/>
|
|
590
|
+
) : (
|
|
591
|
+
<PreviewIcon kind={kind} />
|
|
592
|
+
)}
|
|
593
|
+
<span
|
|
594
|
+
aria-hidden="true"
|
|
595
|
+
className="absolute inset-0 flex items-center justify-center bg-black/45 text-white opacity-0 transition-opacity duration-150 group-hover/preview:opacity-100 group-focus-visible/preview:opacity-100"
|
|
596
|
+
>
|
|
597
|
+
<IconZoomIn size={16} stroke={1.8} />
|
|
598
|
+
</span>
|
|
599
|
+
</button>
|
|
600
|
+
{typeof document !== 'undefined'
|
|
601
|
+
? createPortal(dialog, document.body)
|
|
602
|
+
: null}
|
|
603
|
+
</>
|
|
604
|
+
)
|
|
605
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useMemo, useState } from 'react'
|
|
4
|
+
import { IconCheck, IconCopy, IconFileCode } from '@tabler/icons-react'
|
|
5
|
+
import { tokenizeCode, TokenSpan } from './code-highlighter'
|
|
6
|
+
import { cn } from './utils'
|
|
7
|
+
|
|
8
|
+
export default function ChatCodeBlock({
|
|
9
|
+
children,
|
|
10
|
+
label = 'code',
|
|
11
|
+
className,
|
|
12
|
+
}: {
|
|
13
|
+
children: string
|
|
14
|
+
label?: string
|
|
15
|
+
className?: string
|
|
16
|
+
}) {
|
|
17
|
+
const [copied, setCopied] = useState(false)
|
|
18
|
+
const lines = useMemo(() => tokenizeCode(children), [children])
|
|
19
|
+
|
|
20
|
+
async function copyCode() {
|
|
21
|
+
try {
|
|
22
|
+
await navigator.clipboard.writeText(children)
|
|
23
|
+
setCopied(true)
|
|
24
|
+
window.setTimeout(() => setCopied(false), 1600)
|
|
25
|
+
} catch {
|
|
26
|
+
setCopied(false)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<div className={cn('border-border/70 my-3 overflow-hidden rounded-xl border bg-[#09090b] text-left', className)}>
|
|
32
|
+
<div className="flex items-center justify-between border-b border-neutral-800/80 bg-[#0d0d10]/95 px-3.5 py-2">
|
|
33
|
+
<span className="flex items-center gap-2 font-mono text-[11px] text-neutral-400">
|
|
34
|
+
<IconFileCode size={14} className="text-neutral-500" aria-hidden="true" />
|
|
35
|
+
{label}
|
|
36
|
+
</span>
|
|
37
|
+
<button type="button" onClick={copyCode} aria-label={copied ? 'Code copied' : 'Copy code'} className="flex size-7 cursor-pointer items-center justify-center rounded text-neutral-500 transition-colors hover:text-neutral-200">
|
|
38
|
+
{copied ? <IconCheck size={14} className="text-emerald-400" aria-hidden="true" /> : <IconCopy size={14} aria-hidden="true" />}
|
|
39
|
+
</button>
|
|
40
|
+
</div>
|
|
41
|
+
<pre className="max-w-full overflow-x-auto p-4 font-mono text-[13px] leading-[1.7] antialiased select-text">
|
|
42
|
+
<code>{lines.map((line, lineIndex) => (
|
|
43
|
+
<span key={`line-${lineIndex}`} className="block min-h-[1.7em] whitespace-pre">
|
|
44
|
+
{line.length ? line.map((token, tokenIndex) => <TokenSpan key={`${lineIndex}-${tokenIndex}`} token={token} />) : <> </>}
|
|
45
|
+
</span>
|
|
46
|
+
))}</code>
|
|
47
|
+
</pre>
|
|
48
|
+
</div>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import AiChatInput from './ai-chat-input'
|
|
4
|
+
import type { AiChatInputAction, AiChatInputAttachment } from './ai-chat-input'
|
|
5
|
+
|
|
6
|
+
export type ChatComposerProps = {
|
|
7
|
+
actions: readonly AiChatInputAction[]
|
|
8
|
+
disabled?: boolean
|
|
9
|
+
busy?: boolean
|
|
10
|
+
maxAttachments?: number
|
|
11
|
+
maxFileSizeBytes?: number
|
|
12
|
+
showVoice?: boolean
|
|
13
|
+
placeholder?: string
|
|
14
|
+
accentColor?: string
|
|
15
|
+
onSubmit: (message: string, attachments: AiChatInputAttachment[]) => void
|
|
16
|
+
onStop?: () => void
|
|
17
|
+
onListeningChange?: (listening: boolean) => void
|
|
18
|
+
onAttachmentError?: (message: string) => void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default function ChatComposer({
|
|
22
|
+
actions,
|
|
23
|
+
disabled,
|
|
24
|
+
busy,
|
|
25
|
+
maxAttachments,
|
|
26
|
+
maxFileSizeBytes,
|
|
27
|
+
showVoice,
|
|
28
|
+
placeholder,
|
|
29
|
+
accentColor,
|
|
30
|
+
onSubmit,
|
|
31
|
+
onStop,
|
|
32
|
+
onListeningChange,
|
|
33
|
+
onAttachmentError,
|
|
34
|
+
}: ChatComposerProps) {
|
|
35
|
+
return (
|
|
36
|
+
<AiChatInput
|
|
37
|
+
actions={actions}
|
|
38
|
+
disabled={disabled}
|
|
39
|
+
busy={busy}
|
|
40
|
+
maxAttachments={maxAttachments}
|
|
41
|
+
maxFileSizeBytes={maxFileSizeBytes}
|
|
42
|
+
showVoice={showVoice}
|
|
43
|
+
placeholder={placeholder}
|
|
44
|
+
glow={accentColor ? { color: accentColor } : {}}
|
|
45
|
+
onSubmit={onSubmit}
|
|
46
|
+
onStop={onStop}
|
|
47
|
+
onListeningChange={onListeningChange}
|
|
48
|
+
onAttachmentError={onAttachmentError}
|
|
49
|
+
/>
|
|
50
|
+
)
|
|
51
|
+
}
|