Files
CharisandJoshen Lim f5ff10e195 refactor,tests(support form) (#39410)
* refactor: refactor support form

Refactor support form to make it easier to maintain:
- Split up large components into smaller components and hooks
- Lift state up so we don't have to do complex child/parent
state-syncing via useEffect
- Use nuqs parsing for consistent serialization/deserialization of
support form prefilled fields

* test: support form

Add comprehensive tests for support form

* fix(support form): project and org empty state

* Nit clean up

* More clean up

* cleannnn

* fix(support form): allow case-insensitive category in url

* clean(support form tests): remove unused param

* fix(support form): incorrect logic for sending affected services in payload

* clean(support form): use NO_ORG_MARKER and NO_PROJECT_MARKER instead of strings

* fix(support form): don't show upgrade cta if already on enterprise

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-10-14 11:04:33 +08:00

104 lines
4.1 KiB
TypeScript

import { AnimatePresence, motion } from 'framer-motion'
import { MessageSquare } from 'lucide-react'
import Link from 'next/link'
import { useCallback, useEffect, useState } from 'react'
// End of third-party imports
import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
import { Button } from 'ui'
import { NO_ORG_MARKER, NO_PROJECT_MARKER } from './SupportForm.utils'
interface AIAssistantOptionProps {
projectRef?: string | null
organizationSlug?: string | null
isCondensed?: boolean
}
export const AIAssistantOption = ({
projectRef,
organizationSlug,
isCondensed = false,
}: AIAssistantOptionProps) => {
const { mutate: sendEvent } = useSendEventMutation()
const [isVisible, setIsVisible] = useState(isCondensed)
useEffect(() => {
const timer = setTimeout(() => setIsVisible(true), 800)
return () => clearTimeout(timer)
}, [])
const onAiAssistantClicked = useCallback(() => {
sendEvent({
action: 'ai_assistant_in_support_form_clicked',
groups: {
project: projectRef === null || projectRef === NO_PROJECT_MARKER ? undefined : projectRef,
organization:
organizationSlug === null || organizationSlug === NO_ORG_MARKER
? undefined
: organizationSlug,
},
})
}, [projectRef, organizationSlug, sendEvent])
// If no specific project selected, use the wildcard route
const aiLink = `/project/${projectRef !== NO_PROJECT_MARKER ? projectRef : '_'}?aiAssistantPanelOpen=true&slug=${organizationSlug}`
if (!organizationSlug || organizationSlug === NO_ORG_MARKER) return null
return (
<AnimatePresence initial={false}>
{isVisible && (
<motion.div
initial={{ height: 0, opacity: 0 }}
animate={{ height: 'auto', opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
className="w-full overflow-hidden border rounded-md relative"
>
<div className={`flex flex-col xl:flex-row ${isCondensed ? 'py-4 px-5' : 'py-8 px-10'}`}>
<div className="flex flex-col gap-3 z-[2] flex-shrink-0 w-full">
<div>
<p className="text-sm text-foreground">Try the AI Assistant</p>
<p className="text-sm text-foreground-lighter">
Ask the AI assistant to help you with your support issue
</p>
</div>
<div>
<Link href={aiLink} onClick={onAiAssistantClicked}>
<Button size="tiny" type="default">
Ask AI assistant
</Button>
</Link>
</div>
</div>
{!isCondensed && (
<div className="absolute z-[1] scale-75 -right-24 top-0">
<div className="relative grow flex flex-col gap-3 w-[400px]">
<div className="flex items-start gap-3 pl-12">
<div className="w-8 h-8 rounded-full bg-background-surface-300 flex items-center justify-center">
<MessageSquare size={16} className="text-foreground-lighter" />
</div>
<div className="flex-1 bg-background-surface-200 rounded-lg p-4 max-w-[280px]">
<p className="text-sm text-foreground-lighter">
Hi! I'm your AI assistant. How can I help you today?
</p>
</div>
</div>
<div className="flex items-start justify-end gap-3 pr-10">
<div className="bg-background-surface-200 rounded-lg p-4 max-w-[280px]">
<p className="text-sm text-foreground-lighter">
I can help you with database queries, API endpoints, or any other technical
questions you might have.
</p>
</div>
</div>
</div>
<div className="absolute -inset-2 bg-gradient-to-l from-transparent via-background-200 via-[95%] to-background-200 to-[100%] z-[1]" />
</div>
)}
</div>
</motion.div>
)}
</AnimatePresence>
)
}