import { MousePointerClick, X } from 'lucide-react' import { useEffect, useMemo, useState } from 'react' import { toast } from 'sonner' import { Loading } from 'components/ui/Loading' import { useWarehouseQueryQuery } from 'data/analytics/warehouse-query' import useSingleLog from 'hooks/analytics/useSingleLog' import { Button, CodeBlock, TabsContent_Shadcn_, TabsList_Shadcn_, TabsTrigger_Shadcn_, Tabs_Shadcn_, cn, } from 'ui' import AuthSelectionRenderer from './LogSelectionRenderers/AuthSelectionRenderer' import DatabaseApiSelectionRender from './LogSelectionRenderers/DatabaseApiSelectionRender' import DatabasePostgresSelectionRender from './LogSelectionRenderers/DatabasePostgresSelectionRender' import DefaultExplorerSelectionRenderer from './LogSelectionRenderers/DefaultExplorerSelectionRenderer' import DefaultPreviewSelectionRenderer from './LogSelectionRenderers/DefaultPreviewSelectionRenderer' import FunctionInvocationSelectionRender from './LogSelectionRenderers/FunctionInvocationSelectionRender' import FunctionLogsSelectionRender from './LogSelectionRenderers/FunctionLogsSelectionRender' import type { LogData, LogsEndpointParams, QueryType } from './Logs.types' import { isDefaultLogPreviewFormat, isUnixMicro, unixMicroToIsoTimestamp } from './Logs.utils' import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader' export interface LogSelectionProps { log: LogData | null onClose: () => void queryType?: QueryType projectRef: string params: Partial collectionName?: string } const LogSelection = ({ projectRef, log: partialLog, onClose, queryType, params = {}, collectionName, }: LogSelectionProps) => { const { logData: fullLog, isLoading } = useSingleLog( projectRef, queryType, params, partialLog?.id ) const [sql, setSql] = useState('') const warehouseQueryEnabled = queryType === 'warehouse' const { refetch: refetchWarehouseData, data: warehouseQueryData, isLoading: warehouseQueryLoading, } = useWarehouseQueryQuery( { ref: projectRef, sql, }, { enabled: queryType === 'warehouse', onError: (error) => { toast.error(error.message) }, } ) useEffect(() => { const newSql = `select id, timestamp, event_message, metadata from \`${collectionName}\` where id = '${partialLog?.id}' and timestamp > '2024-01-01' limit 1` setSql(newSql) }, [collectionName, partialLog?.id]) useEffect(() => { if (queryType === 'warehouse') { refetchWarehouseData() } }, [ warehouseQueryData, collectionName, projectRef, partialLog?.id, refetchWarehouseData, queryType, ]) const Formatter = () => { switch (queryType) { case 'warehouse': if (!warehouseQueryData) return null return case 'api': if (!fullLog) return null if (!fullLog.metadata) return return case 'database': if (!fullLog) return null if (!fullLog.metadata) return return case 'fn_edge': if (!fullLog) return null if (!fullLog.metadata) return return case 'functions': if (!fullLog) return null if (!fullLog.metadata) return return case 'auth': if (!fullLog) return null if (!fullLog.metadata) return return default: if (queryType && fullLog && isDefaultLogPreviewFormat(fullLog)) { return } if (queryType && !fullLog) { return null } if (!partialLog) return null return } } const selectionText = useMemo(() => { if (fullLog && queryType) { return `Log ID ${fullLog.id}\n Log Timestamp (UTC) ${isUnixMicro(fullLog.timestamp) ? unixMicroToIsoTimestamp(fullLog.timestamp) : fullLog.timestamp}\n Log Event Message ${fullLog.event_message}\n Log Metadata ${JSON.stringify(fullLog.metadata, null, 2)} ` } return JSON.stringify(fullLog || partialLog, null, 2) }, [fullLog, partialLog, queryType]) const rawLog = useMemo(() => { if (queryType === 'warehouse') { return warehouseQueryData?.result?.[0] || {} } return fullLog || partialLog }, [queryType, warehouseQueryData, fullLog, partialLog]) return (

Select an Event

{queryType === 'warehouse' ? 'Select an Event to view the complete JSON payload' : 'Select an Event to view the code snippet (pretty view) or complete JSON payload (raw view).'}

Details Raw
{isLoading || (warehouseQueryEnabled && warehouseQueryLoading) ? (
) : ( <> {isLoading && 'Loading...'} {JSON.stringify(rawLog, null, 2)} )}
) } export default LogSelection