Files
supabase/apps/studio/components/ui/AiAssistantDropdown.tsx
Saxon FletcherandJoshen Lim 3b756e4d9f Chore/project secure (#45108)
<img width="2652" height="830" alt="image"
src="https://github.com/user-attachments/assets/3c3921e7-c255-4e59-a9c3-c5f97da87788"
/>

Adds a full screen alert behind a feature flag `projectNeedsSecuring`
that prompts for fixing RLS issues.

Adjusts a few other small styles to add more prominence to critical
advisor issues.

To test:

- Enable the flag
- Make sure you have a table with RLS disabled
- Open project home and note the fade in of full page review
- Click "copy prompt" or "fix" and note the prompt
- Click skip to home and refresh the page, note it doesn't appear
anymore


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Project-level security gate on project home with AI assistant prompts,
table details, per-project dismissible notice, and a new telemetry event
for CTA interactions.

* **Improvements**
* Stronger visual treatment for critical advisor items and advisor CTA
when critical issues exist.
* Assistant dropdown supports a copy-prompt callback; added
local-storage key and utilities/types to support project security
workflows.

* **Tests**
  * Added tests covering gate behavior, navigation, and dismissal logic.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-04-29 04:08:09 +00:00

203 lines
5.3 KiB
TypeScript

import { AiAssistantSource } from 'common/telemetry-constants'
import { Chatgpt, Claude } from 'icons'
import { Check, ChevronDown, Copy } from 'lucide-react'
import { ComponentProps, ReactNode, useEffect, useState } from 'react'
import {
AiIconAnimation,
Button,
cn,
copyToClipboard,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
Tooltip,
TooltipContent,
TooltipTrigger,
} from 'ui'
import { useTrack } from '@/lib/telemetry/track'
type TelemetrySource = AiAssistantSource
const EXTERNAL_AI_TOOLS = [
{
label: 'Ask ChatGPT',
url: 'https://chatgpt.com/',
promptParam: 'q',
icon: Chatgpt,
toolId: 'chatgpt' as const,
},
{
label: 'Ask Claude',
url: 'https://claude.ai/new',
promptParam: 'q',
icon: Claude,
toolId: 'claude' as const,
},
]
export interface AiAssistantDropdownItem {
label: string
icon?: ReactNode
onClick: () => void
}
export interface AiAssistantDropdownProps {
buildPrompt: () => string
label: string
iconOnly?: boolean
onOpenAssistant: () => void
onCopyPrompt?: () => void
telemetrySource?: TelemetrySource
size?: ComponentProps<typeof Button>['size']
type?: ComponentProps<typeof Button>['type']
disabled?: boolean
loading?: boolean
className?: string
tooltip?: string
copyLabel?: string
showExternalAI?: boolean
extraDropdownItems?: ReactNode
additionalDropdownItems?: AiAssistantDropdownItem[]
}
export function AiAssistantDropdown({
buildPrompt,
label,
iconOnly = false,
onOpenAssistant,
onCopyPrompt,
telemetrySource,
size = 'tiny',
type = 'default',
disabled = false,
loading = false,
className,
tooltip,
copyLabel = 'Copy prompt',
showExternalAI = false,
extraDropdownItems,
additionalDropdownItems,
}: AiAssistantDropdownProps) {
const track = useTrack()
const [showCopied, setShowCopied] = useState(false)
const [isOpen, setIsOpen] = useState(false)
useEffect(() => {
if (!showCopied) return
const timer = setTimeout(() => setShowCopied(false), 2000)
return () => clearTimeout(timer)
}, [showCopied])
const handleCopyPrompt = () => {
const prompt = buildPrompt()
copyToClipboard(prompt)
setShowCopied(true)
setIsOpen(false)
onCopyPrompt?.()
if (telemetrySource) {
track('ai_prompt_copied', { source: telemetrySource })
}
}
const handleOpenExternalAI = (tool: (typeof EXTERNAL_AI_TOOLS)[number]) => {
const prompt = buildPrompt()
window.open(
`${tool.url}?${tool.promptParam}=${encodeURIComponent(prompt)}`,
'_blank',
'noreferrer'
)
if (telemetrySource) {
track('ai_external_tool_clicked', { source: telemetrySource, tool: tool.toolId })
}
}
const handleOpenAssistant = () => {
onOpenAssistant()
if (telemetrySource) {
track('ai_assistant_dropdown_button_clicked', { source: telemetrySource })
}
}
const buttonContent = (
<div className={cn('flex items-center', iconOnly ? 'gap-0' : 'gap-0')}>
{/* Main button */}
<Button
type={type}
size={size}
disabled={disabled}
onClick={handleOpenAssistant}
icon={<AiIconAnimation size={iconOnly ? 16 : 14} loading={loading} />}
className={cn('rounded-r-none border-r-0', iconOnly && 'px-1.5', className)}
>
{!iconOnly && label}
</Button>
{/* Dropdown trigger */}
<DropdownMenu open={isOpen} onOpenChange={setIsOpen}>
<DropdownMenuTrigger asChild>
<Button
type={type}
size={size}
disabled={disabled}
className={cn('rounded-l-none px-1', iconOnly && 'px-1')}
icon={<ChevronDown size={12} />}
/>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-44">
{extraDropdownItems}
<DropdownMenuItem onClick={handleCopyPrompt} className="gap-2">
{showCopied ? <Check size={14} className="text-brand" /> : <Copy size={14} />}
{showCopied ? 'Copied!' : copyLabel}
</DropdownMenuItem>
{showExternalAI && (
<>
<DropdownMenuSeparator />
{EXTERNAL_AI_TOOLS.map((tool) => (
<DropdownMenuItem
key={tool.url}
className="gap-2"
onClick={() => handleOpenExternalAI(tool)}
>
<tool.icon size={14} />
{tool.label}
</DropdownMenuItem>
))}
</>
)}
{additionalDropdownItems && additionalDropdownItems.length > 0 && (
<>
<DropdownMenuSeparator />
{additionalDropdownItems.map((item, i) => (
<DropdownMenuItem key={i} onClick={item.onClick} className="gap-2">
{item.icon}
{item.label}
</DropdownMenuItem>
))}
</>
)}
</DropdownMenuContent>
</DropdownMenu>
</div>
)
// Wrap in tooltip for icon-only mode
if (iconOnly && tooltip) {
return (
<Tooltip>
<TooltipTrigger asChild>
<div className="inline-flex">{buttonContent}</div>
</TooltipTrigger>
<TooltipContent side="bottom">{tooltip}</TooltipContent>
</Tooltip>
)
}
return buttonContent
}