// @ts-nocheck
import { useDebounce } from '@uidotdev/usehooks'
import { useParams } from 'common'
import { Home, Plus } from 'lucide-react'
import { useState } from 'react'
import {
Command,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
DropdownMenuCheckboxItem,
DropdownMenuPortal,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
SQL_ICON,
} from 'ui'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
import { DEPRECATED_REPORTS } from './Reports.constants'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { useContentQuery } from '@/data/content/content-query'
import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import { Metric, METRIC_CATEGORIES, METRICS } from '@/lib/constants/metrics'
import { editorPanelState } from '@/state/editor-panel-state'
import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
import type { Dashboards } from '@/types'
interface MetricOptionsProps {
config?: Dashboards.Content
handleChartSelection: ({
metric,
isAddingChart,
}: {
metric: Metric
isAddingChart: boolean
}) => void
}
export const MetricOptions = ({ config, handleChartSelection }: MetricOptionsProps) => {
const { ref: projectRef } = useParams()
const { data: selectedOrganization } = useSelectedOrganizationQuery()
const { openSidebar } = useSidebarManagerSnapshot()
const [search, setSearch] = useState('')
const { projectAuthAll: authEnabled, projectStorageAll: storageEnabled } = useIsFeatureEnabled([
'project_auth:all',
'project_storage:all',
])
const metricCategories = Object.values(METRIC_CATEGORIES).filter(({ key }) => {
if (key === 'api_auth') return authEnabled
if (key === 'api_storage') return storageEnabled
return true
})
const { mutate: sendEvent } = useSendEventMutation()
const debouncedSearch = useDebounce(search, 300)
const {
data,
isPending: isLoading,
refetch,
} = useContentQuery({
projectRef,
type: 'sql',
name: debouncedSearch.length === 0 ? undefined : debouncedSearch,
})
const snippets = data?.content
return (
<>
{metricCategories.map((cat) => {
return (
{cat.icon ? cat.icon() : }
{cat.label}
{METRICS.filter(
(metric) =>
!DEPRECATED_REPORTS.includes(metric.key) && metric?.category?.key === cat.key
).map((metric) => {
return (
x.attribute === metric.key)}
onCheckedChange={(e) => handleChartSelection({ metric, isAddingChart: e })}
>
{metric.label}
)
})}
)
})}
{
if (open) refetch()
}}
>
SQL Snippets
{isLoading ? (
) : !snippets?.length ? (
No snippets found
) : null}
{snippets?.map((snippet) => (
{
if (!config?.layout.find((x) => x.id === snippet.id)) {
handleChartSelection({
metric: {
id: snippet.id,
key: `snippet_${snippet.id}`,
label: snippet.name,
},
isAddingChart: true,
})
sendEvent({
action: 'custom_report_add_sql_block_clicked',
groups: {
project: projectRef ?? 'Unknown',
organization: selectedOrganization?.slug ?? 'Unknown',
},
})
}
}}
>
{snippet.name}
))}
{
editorPanelState.openAsNew()
openSidebar(SIDEBAR_KEYS.EDITOR_PANEL)
}}
>
>
)
}