import { BarChart, Shield } from 'lucide-react' import { useCallback, useMemo } from 'react' import { useParams } from 'common' import { LINTER_LEVELS } from 'components/interfaces/Linter/Linter.constants' import { createLintSummaryPrompt } from 'components/interfaces/Linter/Linter.utils' import { SIDEBAR_KEYS } from 'components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider' import { ButtonTooltip } from 'components/ui/ButtonTooltip' import { Lint, useProjectLintsQuery } from 'data/lint/lint-query' import { useTrack } from 'lib/telemetry/track' import { useAdvisorStateSnapshot } from 'state/advisor-state' import { useAiAssistantStateSnapshot } from 'state/ai-assistant-state' import { useSidebarManagerSnapshot } from 'state/sidebar-manager-state' import { AiIconAnimation, Button, Card, CardContent, CardHeader, CardTitle } from 'ui' import { Row } from 'ui-patterns' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { Markdown } from '../Markdown' export const AdvisorSection = ({ showEmptyState = false }: { showEmptyState?: boolean }) => { const { ref: projectRef } = useParams() const { data: lints, isPending: isLoadingLints } = useProjectLintsQuery( { projectRef, }, { enabled: !showEmptyState, } ) const track = useTrack() const snap = useAiAssistantStateSnapshot() const { openSidebar } = useSidebarManagerSnapshot() const { setSelectedItem } = useAdvisorStateSnapshot() const errorLints: Lint[] = useMemo(() => { return lints?.filter((lint) => lint.level === LINTER_LEVELS.ERROR) ?? [] }, [lints]) const totalErrors = errorLints.length const titleContent = useMemo(() => { if (totalErrors === 0) return

Advisor found no issues

const issuesText = totalErrors === 1 ? 'issue' : 'issues' const numberDisplay = totalErrors.toString() return (

Advisor found {numberDisplay} {issuesText}

) }, [totalErrors]) const handleAskAssistant = useCallback(() => { openSidebar(SIDEBAR_KEYS.AI_ASSISTANT) track('advisor_assistant_button_clicked', { origin: 'homepage', issuesCount: totalErrors, }) }, [track, openSidebar, totalErrors]) const handleCardClick = useCallback( (lint: Lint) => { setSelectedItem(lint.cache_key, 'lint') openSidebar(SIDEBAR_KEYS.ADVISOR_PANEL) track('advisor_detail_opened', { origin: 'homepage', advisorSource: 'lint', advisorCategory: lint.categories[0], advisorType: lint.name, advisorLevel: lint.level, }) }, [track, setSelectedItem, openSidebar] ) if (showEmptyState) { return } return (
{isLoadingLints ? ( ) : (
{titleContent}
)} {isLoadingLints ? (
) : errorLints.length > 0 ? ( <> {errorLints.map((lint) => { return ( { handleCardClick(lint) }} >
{lint.categories[0] === 'SECURITY' ? ( ) : ( )} {lint.categories[0]}
} onClick={(e) => { e.stopPropagation() e.preventDefault() openSidebar(SIDEBAR_KEYS.AI_ASSISTANT) snap.newChat({ name: 'Summarize lint', initialInput: createLintSummaryPrompt(lint), }) track('advisor_assistant_button_clicked', { origin: 'homepage', advisorCategory: lint.categories[0], advisorType: lint.name, advisorLevel: lint.level, }) }} tooltip={{ content: { side: 'bottom', text: 'Help me fix this issue' }, }} />

{lint.title}

{lint.detail && lint.detail.replace(/\\`/g, '`')}
) })}
) : ( )}
) } function EmptyState() { return (

No security or performance errors found

) }