Files
supabase/apps/www/components/AIDemo/Panel.tsx
Brian-AnandDanny White f1082e28c7 chore: migrate Framer Motion imports to Motion (#51520)
## Problem

Closes #31221.
The applications and shared UI packages still import the former
`framer-motion` package instead of Motion's React entry point.

## Solution

Replace runtime imports, type imports, and three test mocks with
`motion/react` across the consuming workspaces.
Replace the seven direct dependency declarations with `motion:
^11.18.2`.
Preserve the existing animation engine version and all other dependency
resolutions, including the asynchronous `domMax` feature loader.
Historical blog examples remain as published; `framer-motion` remains a
transitive implementation dependency of Motion.

## Review instructions

1. Run `pnpm install --frozen-lockfile` and check the seven dependency
replacements in the lockfile.
2. Open the WWW Auth page and Docs homepage at desktop and mobile
widths, with normal and reduced motion preferences.
3. Open the mobile navigation, expand Product on WWW, and close the menu
using Escape and the close button.
4. Check the existing animation components, shared form validation, and
Studio tests for unchanged behavior.

Validation:

- Studio: 731 test files, 8,334 passing tests and one expected failure,
with `--maxWorkers=4`.
- Shared UI, UI patterns, WWW, and UI library: 615 passing tests.
- `pnpm run typecheck --concurrency=1`: all 16 repository tasks pass.
- All six application source lint scripts and lint ratchets pass;
generated Docs example copies are excluded from local lint.
- Repository Prettier source check, lockfile formatting, and case hazard
check pass.
- WWW: 10 existing end-to-end checks across nine pages.
- Docs: 5 existing guide link and accessibility checks across Database
and Auth.
- WWW and Docs mobile navigation pass at desktop/mobile sizes with
normal/reduced motion; SQL-to-REST translation tabs exercise the
asynchronous feature loader without browser errors.
- Frozen-lockfile installation passes; all existing package and snapshot
resolutions are unchanged.
- Learn and UI library command palettes open, filter results, and close
with Escape without browser errors.
- Independent code review finds no actionable defects.
- `npm run build -- --filter=www --filter=docs --filter=studio
--filter=learn --filter=library --concurrency=1 --continue=always`: all
five affected applications build successfully (9 tasks including
prerequisites); asset uploads disabled for local validation.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
(no docs topics changed)

---------

Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>
2026-10-14 07:16:00 +00:00

216 lines
7.0 KiB
TypeScript

// components/AIDemoPanel.tsx
import { AnimatePresence, motion } from 'motion/react'
import Image from 'next/image'
import errorGif from 'public/images/ai/error.gif'
import { useEffect, useRef, useState } from 'react'
import { AiIconAnimation, cn, Input } from 'ui'
import styles from './assistant.module.css'
interface DemoMessage {
id: string
role: 'user' | 'assistant'
content: string | React.ReactNode
render?: React.ReactNode
createdAt: Date
}
export const AIDemoPanel = ({ incomingMessages = [] }: { incomingMessages?: DemoMessage[] }) => {
const [messages, setMessages] = useState<DemoMessage[]>([])
const [isComplete, setIsComplete] = useState(false)
const [pendingMessages, setPendingMessages] = useState<DemoMessage[]>([])
const [input, setInput] = useState('')
const [showFade, setShowFade] = useState(false)
const messagesEndRef = useRef<HTMLDivElement>(null)
const scrollContainerRef = useRef<HTMLDivElement>(null)
const scrollToBottom = () => {
const container = messagesEndRef.current?.closest('.overflow-y-auto')
if (container) {
container.scrollTo({
top: container.scrollHeight,
behavior: 'smooth',
})
}
}
const handleScroll = () => {
const container = scrollContainerRef.current
if (container) {
const scrollPercentage =
(container.scrollTop / (container.scrollHeight - container.clientHeight)) * 100
const isScrollable = container.scrollHeight > container.clientHeight
const isAtBottom = scrollPercentage >= 100
setShowFade(isScrollable && !isAtBottom)
}
}
useEffect(() => {
scrollToBottom()
handleScroll()
}, [messages])
useEffect(() => {
const container = scrollContainerRef.current
if (container) {
container.addEventListener('scroll', handleScroll)
handleScroll()
}
return () => {
if (container) {
container.removeEventListener('scroll', handleScroll)
}
}
}, [])
useEffect(() => {
if (pendingMessages.length > 0) {
const timer = setTimeout(() => {
setMessages((prev) => [...prev, pendingMessages[0]])
setPendingMessages((prev) => prev.slice(1))
}, 3000)
return () => clearTimeout(timer)
} else if (!isComplete) {
setIsComplete(true)
}
}, [isComplete, pendingMessages])
useEffect(() => {
if (incomingMessages.length > 0) {
setMessages((prev) => [...prev, ...pendingMessages, incomingMessages[0]])
setPendingMessages(incomingMessages.slice(1))
}
}, [incomingMessages])
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault()
if (!input.trim()) return
const userMessage: DemoMessage = {
id: Date.now().toString(),
role: 'user',
content: input,
createdAt: new Date(),
}
const assistantMessage: DemoMessage = {
id: (Date.now() + 1).toString(),
role: 'assistant',
content: (
<div className="rounded-md overflow-hidden relative">
<Image
src={errorGif}
alt="Try the assistant in your dashboard for real results"
className="w-full grayscale"
/>
</div>
),
createdAt: new Date(),
}
const followUp: DemoMessage = {
id: (Date.now() + 1).toString(),
role: 'assistant',
content: "Sorry, custom queries aren't supported in this demo.",
createdAt: new Date(),
}
setMessages((prev) => [...prev, userMessage])
setPendingMessages((prev) => [...prev, assistantMessage, followUp])
setInput('')
}
return (
<div className="w-full shadow-2xl relative h-full bg-surface-75 border border-text-light/25 rounded-md overflow-hidden">
<div className="absolute inset-0 flex flex-col">
{/* Header */}
<div className="z-30 border-b border-b-muted flex items-center gap-x-3 px-5 h-[46px]">
<AiIconAnimation loading={false} allowHoverEffect />
<div className="text-sm flex-1">Assistant</div>
</div>
{/* Messages */}
<div
ref={scrollContainerRef}
className="flex-1 overflow-y-auto p-5 flex flex-col [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]"
>
{messages.map((message, index) => (
<>
<motion.div
key={index}
layout="position"
initial={{ y: 5, opacity: 0, scale: 0.99 }}
animate={{ y: 0, opacity: 1, scale: 1 }}
className={cn(
'text-foreground-light text-sm mb-6',
message.role === 'user'
? 'px-5 py-3 rounded-lg bg-background-muted/80 w-fit self-end max-w-64'
: ''
)}
>
{message.content}
{message.render && <div className="mt-4">{message.render}</div>}
</motion.div>
</>
))}
{messages.length > 0 && messages[messages.length - 1].role === 'user' && (
<motion.div className="text-foreground-lighter text-sm flex gap-1.5 items-center">
<span>Thinking</span>
<div className="flex gap-1">
<motion.span
animate={{ opacity: [0, 1, 0] }}
transition={{ duration: 1.5, repeat: Infinity, delay: 0 }}
>
.
</motion.span>
<motion.span
animate={{ opacity: [0, 1, 0] }}
transition={{ duration: 1.5, repeat: Infinity, delay: 0.3 }}
>
.
</motion.span>
<motion.span
animate={{ opacity: [0, 1, 0] }}
transition={{ duration: 1.5, repeat: Infinity, delay: 0.6 }}
>
.
</motion.span>
</div>
</motion.div>
)}
<div ref={messagesEndRef} />
</div>
{/* Input */}
<AnimatePresence>
{showFade && (
<motion.div
initial={{ opacity: 0 }}
exit={{ opacity: 0 }}
animate={{ opacity: 1 }}
className="pointer-events-none z-10 -mt-24"
>
<div className="h-24 w-full bg-linear-to-t from-background muted to-transparent" />
</motion.div>
)}
</AnimatePresence>
<div className="p-5 pt-0 relative z-10">
<form onSubmit={handleSubmit} className={cn('rounded-md', styles['border-gradient'])}>
<Input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Ask your data anything..."
className={cn(
'w-full bg-background-muted rounded-md [&>textarea]:border [&>textarea]:rounded-md [&>textarea]:outline-hidden! [&>textarea]:ring-offset-0! [&>textarea]:ring-0! focus:outline-hidden'
)}
/>
</form>
</div>
</div>
</div>
)
}