mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 2 of a 6-PR stack migrating all `next/router` and `next/compat/router` imports in `apps/studio` onto TanStack Router hooks and removing the router shims. | Part | PR | Scope | | --- | --- | --- | | 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`, `data/prefetchers/**` | | **2** | **#54054 ← this PR** | **`components/ui/**`** | | 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` | | 4 | #54056 | Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | 6 | #54058 | Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks | Migrates all 15 `apps/studio/components/ui/**` files off `next/router`. **Changed:** - Replaced `useRouter` across `AIAssistantPanel` (`AIAssistant`, `ModelSelector`), `Charts/ComposedChartHandler`, `EditorPanel`, `ErrorBoundary` (`ClientSideExceptionHandler`, `GlobalErrorBoundaryState`, `InsertBeforeRemoveChildErrorHandler`), `GlobalShortcuts/ShortcutsReferenceSheet`, `HelpPanel` (`HelpOptionsList`, `HelpPanel`), `ProductMenu/ProductMenuShortcuts`, `QueryBlock/EditQueryButton`, and `ResourceExhaustionWarningBanner` with TanStack's `useNavigate`, `useLocation`, `useRoutePattern`, and `useParams` from `common` - Updated `AIAssistant.test.tsx`, `HelpOptionsList.test.tsx`, and `ProductMenuShortcuts.test.tsx` to use TanStack Router instead of `next/router` mocks **Simplified:** - `EditQueryButton` and `ShortcutsReferenceSheet` match against `useRoutePattern()` instead of the real pathname, as `router.pathname` did before. A ref or slug containing `sql` or `advisors` no longer misfires. - Removed the unused `restrictToRoutes` field from `ResourceExhaustionWarningBanner` (no warning sets it) - `AIAssistant` clears the handoff param with `navigate({ to: '.' })`, dropping a pathname subscription and an `AnyRouter` cast - `ProductMenuShortcuts` inlines its handler (`useShortcut` always calls the latest callback) - Moved the `AdvisorsNavigationShortcuts.test.tsx` fix into this PR so it passes on its own - Tests use the shared `createTestRouter` and `customRender` router Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] AI Assistant panel links, model selector navigation, and "Edit in SQL Editor" (`EditQueryButton`) navigate cleanly - [ ] Keyboard shortcuts in `ProductMenuShortcuts` and the shortcuts reference sheet (`?`) match the active section and navigate as expected - [ ] Error boundary recovery ("Reload page" / navigation actions) and Help panel links still work - [ ] Resource exhaustion warning banner matches project routes as before --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
207 lines
7.1 KiB
TypeScript
207 lines
7.1 KiB
TypeScript
import { useBreakpoint } from 'common'
|
|
import { ArrowUpIcon, Loader2Icon, SquareIcon } from 'lucide-react'
|
|
import { AnimatePresence, motion } from 'motion/react'
|
|
import { ChangeEvent, FormEvent, forwardRef, KeyboardEvent, memo, ReactNode, useRef } from 'react'
|
|
import { Button, cn, ExpandingTextArea } from 'ui'
|
|
|
|
import { ButtonTooltip } from '../ButtonTooltip'
|
|
import { formatAttachedSnippets } from './AIAssistant.utils'
|
|
import { SnippetRow } from './SnippetRow'
|
|
import { type SqlSnippet } from '@/state/ai-assistant-state'
|
|
|
|
export interface FormProps {
|
|
/* The ref for the textarea, optional. Exposed for the CommandsPopover to attach events. */
|
|
textAreaRef?: React.RefObject<HTMLTextAreaElement | null>
|
|
/* The loading state of the form */
|
|
loading: boolean
|
|
/* The disabled state of the form */
|
|
disabled?: boolean
|
|
/* The value of the textarea */
|
|
value?: string
|
|
/* The function to handle the value change */
|
|
onValueChange: (value: ChangeEvent<HTMLTextAreaElement>) => void
|
|
/**
|
|
* If true, include SQL snippets in the message sent to onSubmit
|
|
*/
|
|
includeSnippetsInMessage?: boolean
|
|
/**
|
|
* The function to handle the form submission
|
|
*/
|
|
onSubmit: (message: string) => void
|
|
/**
|
|
* The function to handle stopping the stream
|
|
*/
|
|
onStop?: () => void
|
|
/* The placeholder of the textarea */
|
|
placeholder?: string
|
|
/* SQL snippets to display above the form - can be strings or objects with label and content */
|
|
sqlSnippets?: SqlSnippet[]
|
|
/* Function to handle removing a SQL snippet */
|
|
onRemoveSnippet?: (index: number) => void
|
|
/* Additional class name for the snippets container */
|
|
snippetsClassName?: string
|
|
/* Additional class name for the form wrapper */
|
|
className?: string
|
|
/* If currently editing an existing message */
|
|
isEditing?: boolean
|
|
/* Icon for the submit button when not loading, defaults to an up arrow */
|
|
submitIcon?: ReactNode
|
|
/* Label for the submit button when not loading, used for both aria-label and tooltip text, defaults to "Send message" */
|
|
submitLabel?: string
|
|
/**
|
|
* An additional action shown beside the main submit button, transitioned in/out with
|
|
* Motion. Pass undefined to hide it (e.g. when the input doesn't match its condition).
|
|
*/
|
|
secondaryAction?: {
|
|
label: string
|
|
onClick: () => void
|
|
}
|
|
}
|
|
|
|
const AssistantChatFormComponent = forwardRef<HTMLFormElement, FormProps>(
|
|
(
|
|
{
|
|
loading = false,
|
|
disabled = false,
|
|
value = '',
|
|
textAreaRef,
|
|
onValueChange,
|
|
onSubmit,
|
|
onStop,
|
|
placeholder,
|
|
sqlSnippets,
|
|
onRemoveSnippet,
|
|
snippetsClassName,
|
|
includeSnippetsInMessage = false,
|
|
className,
|
|
isEditing = false,
|
|
submitIcon = <ArrowUpIcon />,
|
|
submitLabel = 'Send message',
|
|
secondaryAction,
|
|
...props
|
|
},
|
|
_ref
|
|
) => {
|
|
const formRef = useRef<HTMLFormElement>(null)
|
|
const isMobile = useBreakpoint('md')
|
|
|
|
const handleSubmit = (event?: FormEvent<HTMLFormElement>) => {
|
|
if (event) event.preventDefault()
|
|
if (disabled || !value || (loading && !isEditing)) return
|
|
|
|
let finalMessage = value
|
|
if (includeSnippetsInMessage && sqlSnippets && sqlSnippets.length > 0) {
|
|
finalMessage = [value, formatAttachedSnippets(sqlSnippets)].filter(Boolean).join('\n\n')
|
|
}
|
|
|
|
onSubmit(finalMessage)
|
|
}
|
|
|
|
const handleKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
|
|
if (event.key === 'Enter' && !event.shiftKey) {
|
|
event.preventDefault()
|
|
handleSubmit()
|
|
} else if (event.key === 'Escape') {
|
|
event.currentTarget.blur()
|
|
}
|
|
}
|
|
|
|
const canSubmit = !disabled && !loading && !!value
|
|
|
|
return (
|
|
<div className="w-full">
|
|
<form
|
|
id="assistant-chat"
|
|
ref={formRef}
|
|
{...props}
|
|
onSubmit={handleSubmit}
|
|
className={cn('relative rounded-lg', className)}
|
|
>
|
|
{sqlSnippets && sqlSnippets.length > 0 && (
|
|
<SnippetRow
|
|
snippets={sqlSnippets}
|
|
onRemoveSnippet={onRemoveSnippet}
|
|
className="absolute top-0 left-0 right-0 px-1.5 py-1.5"
|
|
/>
|
|
)}
|
|
<ExpandingTextArea
|
|
autoFocus={!isMobile}
|
|
ref={textAreaRef}
|
|
disabled={disabled}
|
|
className={cn(
|
|
'text-sm pr-10 pb-9 max-h-64 rounded-lg',
|
|
sqlSnippets && sqlSnippets.length > 0 && 'pt-10'
|
|
)}
|
|
placeholder={placeholder}
|
|
spellCheck={false}
|
|
rows={3}
|
|
value={value}
|
|
onChange={(event) => onValueChange(event)}
|
|
onKeyDown={handleKeyDown}
|
|
/>
|
|
<div className="absolute inset-x-2 bottom-2 flex items-center justify-end pointer-events-none">
|
|
<div className="flex gap-x-1.5 items-center pointer-events-auto">
|
|
<AnimatePresence>
|
|
{!loading && secondaryAction && (
|
|
<motion.div
|
|
key="secondary-action"
|
|
initial={{ opacity: 0, scale: 0.8 }}
|
|
animate={{ opacity: 1, scale: 1 }}
|
|
exit={{ opacity: 0, scale: 0.8 }}
|
|
transition={{ duration: 0.15 }}
|
|
>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
aria-label={secondaryAction.label}
|
|
onClick={secondaryAction.onClick}
|
|
className="h-7 rounded-full"
|
|
>
|
|
{secondaryAction.label}
|
|
</Button>
|
|
</motion.div>
|
|
)}
|
|
</AnimatePresence>
|
|
{loading ? (
|
|
onStop ? (
|
|
<ButtonTooltip
|
|
variant="outline"
|
|
aria-label="Stop response"
|
|
icon={<SquareIcon fill="currentColor" className="scale-75" />}
|
|
onClick={onStop}
|
|
className="w-7 h-7 rounded-full p-0 text-center flex items-center justify-center"
|
|
tooltip={{ content: { side: 'top', text: 'Stop response' } }}
|
|
/>
|
|
) : (
|
|
<Loader2Icon
|
|
size={22}
|
|
className="animate-spin size-7 text-muted"
|
|
strokeWidth={1}
|
|
/>
|
|
)
|
|
) : (
|
|
<ButtonTooltip
|
|
type="submit"
|
|
variant={canSubmit ? 'primary' : 'default'}
|
|
aria-label={submitLabel}
|
|
icon={submitIcon}
|
|
disabled={!canSubmit}
|
|
className={cn(
|
|
'w-7 h-7 rounded-full p-0 text-center flex items-center justify-center',
|
|
!canSubmit ? 'opacity-50' : 'opacity-100'
|
|
)}
|
|
tooltip={{ content: { side: 'top', text: submitLabel } }}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
)
|
|
}
|
|
)
|
|
|
|
AssistantChatFormComponent.displayName = 'AssistantChatFormComponent'
|
|
|
|
export const AssistantChatForm = memo(AssistantChatFormComponent)
|