import { PermissionAction } from '@supabase/shared-types/out/constants'
import { ChevronRight, FilePlus, Plus } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useState } from 'react'
import { IS_PLATFORM, useParams } from 'common'
import { CreateWarehouseCollectionModal } from 'components/interfaces/DataWarehouse/CreateWarehouseCollection'
import { WarehouseMenuItem } from 'components/interfaces/DataWarehouse/WarehouseMenuItem'
import SavedQueriesItem from 'components/interfaces/Settings/Logs/Logs.SavedQueriesItem'
import { LogsSidebarItem } from 'components/interfaces/Settings/Logs/SidebarV2/SidebarItem'
import { useWarehouseCollectionsQuery } from 'data/analytics/warehouse-collections-query'
import { useWarehouseTenantQuery } from 'data/analytics/warehouse-tenant-query'
import { useContentQuery } from 'data/content/content-query'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { useCurrentOrgPlan } from 'hooks/misc/useCurrentOrgPlan'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useFlag } from 'hooks/ui/useFlag'
import {
Button,
Collapsible_Shadcn_,
CollapsibleContent_Shadcn_,
CollapsibleTrigger_Shadcn_,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
Separator,
Tooltip,
TooltipContent,
TooltipTrigger,
} from 'ui'
import {
InnerSideBarEmptyPanel,
InnerSideBarFilters,
InnerSideBarFilterSearchInput,
InnerSideMenuItem,
} from 'ui-patterns/InnerSideMenu'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
const SupaIcon = ({ className }: { className?: string }) => {
return (
)
}
export function SidebarCollapsible({
children,
title,
defaultOpen,
}: {
children: React.ReactNode
title: string
defaultOpen?: boolean
}) {
return (
{title}
{children}
)
}
export function LogsSidebarMenuV2() {
const router = useRouter()
const { ref } = useParams() as { ref: string }
const warehouseEnabled = useFlag('warehouse')
const [searchText, setSearchText] = useState('')
const [createCollectionOpen, setCreateCollectionOpen] = useState(false)
const canCreateCollection = useCheckPermissions(PermissionAction.ANALYTICS_WRITE, 'logflare')
const { data: tenantData } = useWarehouseTenantQuery({ projectRef: ref })
const {
projectAuthAll: authEnabled,
projectStorageAll: storageEnabled,
realtimeAll: realtimeEnabled,
} = useIsFeatureEnabled(['project_storage:all', 'project_auth:all', 'realtime:all'])
const { data: whCollections, isLoading: whCollectionsLoading } = useWarehouseCollectionsQuery(
{ projectRef: ref },
{ enabled: IS_PLATFORM && warehouseEnabled && !!tenantData }
)
const { plan: orgPlan, isLoading: isOrgPlanLoading } = useCurrentOrgPlan()
const isFreePlan = !isOrgPlanLoading && orgPlan?.id === 'free'
const { data: savedQueriesRes, isLoading: savedQueriesLoading } = useContentQuery({
projectRef: ref,
type: 'log_sql',
})
const savedQueries = [...(savedQueriesRes?.content ?? [])]
.filter((c) => c.type === 'log_sql')
.sort((a, b) => a.name.localeCompare(b.name))
function isActive(path: string) {
return router.asPath.includes(path)
}
const BASE_COLLECTIONS = [
{
name: 'API Gateway',
key: 'edge-logs',
url: `/project/${ref}/logs/edge-logs`,
items: [],
},
{
name: 'Postgres',
key: 'postgres-logs',
url: `/project/${ref}/logs/postgres-logs`,
items: [],
},
{
name: 'PostgREST',
key: 'postgrest-logs',
url: `/project/${ref}/logs/postgrest-logs`,
items: [],
},
IS_PLATFORM
? {
name: isFreePlan ? 'Pooler' : 'Shared Pooler',
key: 'pooler-logs',
url: `/project/${ref}/logs/pooler-logs`,
items: [],
}
: null,
!isFreePlan && IS_PLATFORM
? {
name: 'Dedicated Pooler',
key: 'dedicated-pooler-logs',
url: `/project/${ref}/logs/dedicated-pooler-logs`,
items: [],
}
: null,
authEnabled
? {
name: 'Auth',
key: 'auth-logs',
url: `/project/${ref}/logs/auth-logs`,
items: [],
}
: null,
storageEnabled
? {
name: 'Storage',
key: 'storage-logs',
url: `/project/${ref}/logs/storage-logs`,
items: [],
}
: null,
realtimeEnabled
? {
name: 'Realtime',
key: 'realtime-logs',
url: `/project/${ref}/logs/realtime-logs`,
items: [],
}
: null,
{
name: 'Edge Functions',
key: 'edge-functions-logs',
url: `/project/${ref}/logs/edge-functions-logs`,
items: [],
},
{
name: 'Cron',
key: 'pg_cron',
url: `/project/${ref}/logs/pgcron-logs`,
items: [],
},
].filter((x) => x !== null)
const OPERATIONAL_COLLECTIONS = IS_PLATFORM
? [
{
name: 'Postgres Version Upgrade',
key: 'pg-upgrade-logs',
url: `/project/${ref}/logs/pg-upgrade-logs`,
items: [],
},
]
: []
const filteredLogs = BASE_COLLECTIONS.filter((collection) => {
return collection?.name.toLowerCase().includes(searchText.toLowerCase())
})
const filteredOperationalLogs = OPERATIONAL_COLLECTIONS.filter((collection) => {
return collection?.name.toLowerCase().includes(searchText.toLowerCase())
})
const filteredWarehouse = whCollections?.filter((collection) => {
return collection.name.toLowerCase().includes(searchText.toLowerCase())
})
return (
setSearchText(e.target.value)}
>
{warehouseEnabled ? (
}
className="w-[26px]"
/>
Create query
{!canCreateCollection && (
You need additional permissions to create a collection
)}
) : (
}
className="w-[26px]"
onClick={() => router.push(`/project/${ref}/logs/explorer`)}
/>
)}
Templates
{filteredLogs.map((collection) => {
const isItemActive = isActive(collection.url)
return (
}
label={collection.name}
/>
)
})}
{whCollectionsLoading && warehouseEnabled ? (
) : filteredWarehouse?.length ? (
{filteredWarehouse.map((collection) => (
))}
) : null}
{OPERATIONAL_COLLECTIONS.length > 0 && (
<>
{filteredOperationalLogs.map((collection) => (
}
label={collection.name}
/>
))}
>
)}
{savedQueriesLoading && (
)}
{savedQueries.length === 0 && (
Create query
}
/>
)}
{savedQueries.map((query) => (
))}
)
}