import { useParams } from 'common' import dayjs from 'dayjs' import Link from 'next/link' import { useEffect, useRef } from 'react' import { Badge, cn, Tabs_Shadcn_, TabsContent_Shadcn_, TabsList_Shadcn_, TabsTrigger_Shadcn_, Tooltip, TooltipContent, TooltipTrigger, } from 'ui' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { AdvisorWidget } from '@/components/interfaces/Home/AdvisorWidget' import { ClientLibrary } from '@/components/interfaces/Home/ClientLibrary' import { ExampleProject } from '@/components/interfaces/Home/ExampleProject' import { EXAMPLE_PROJECTS } from '@/components/interfaces/Home/Home.constants' import { NewProjectPanel } from '@/components/interfaces/Home/NewProjectPanel/NewProjectPanel' import { ProjectUsageSection } from '@/components/interfaces/Home/ProjectUsageSection' import { ServiceStatus } from '@/components/interfaces/Home/ServiceStatus' import { ProjectPausedState } from '@/components/layouts/ProjectLayout/PausedState/ProjectPausedState' import { ComputeBadgeWrapper } from '@/components/ui/ComputeBadgeWrapper' import { InlineLink } from '@/components/ui/InlineLink' import { ProjectUpgradeFailedBanner } from '@/components/ui/ProjectUpgradeFailedBanner' import { useBranchesQuery } from '@/data/branches/branches-query' import { useEdgeFunctionsQuery } from '@/data/edge-functions/edge-functions-query' import { useProjectDetailQuery } from '@/data/projects/project-detail-query' import { useReadReplicasQuery } from '@/data/read-replicas/replicas-query' import { useTablesQuery } from '@/data/tables/tables-query' import { useCustomContent } from '@/hooks/custom-content/useCustomContent' import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { useIsOrioleDb, useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { DOCS_URL, IS_PLATFORM, PROJECT_STATUS } from '@/lib/constants' import { useAppStateSnapshot } from '@/state/app-state' export const Home = () => { const { data: project } = useSelectedProjectQuery() const { data: organization } = useSelectedOrganizationQuery() const { data: parentProject } = useProjectDetailQuery({ ref: project?.parent_project_ref }) const isOrioleDb = useIsOrioleDb() const snap = useAppStateSnapshot() const { ref, enableBranching } = useParams() const { projectHomepageExampleProjects, projectHomepageClientLibraries: clientLibraries } = useCustomContent(['project_homepage:example_projects', 'project_homepage:client_libraries']) const { projectHomepageShowInstanceSize: showInstanceSize, projectHomepageShowExamples: showExamples, } = useIsFeatureEnabled(['project_homepage:show_instance_size', 'project_homepage:show_examples']) const hasShownEnableBranchingModalRef = useRef(false) const isPaused = project?.status === PROJECT_STATUS.INACTIVE const isNewProject = dayjs(project?.inserted_at).isAfter(dayjs().subtract(2, 'day')) useEffect(() => { if (enableBranching && !hasShownEnableBranchingModalRef.current) { hasShownEnableBranchingModalRef.current = true snap.setShowCreateBranchModal(true) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [enableBranching]) const { data: tablesData, isPending: isLoadingTables } = useTablesQuery({ projectRef: project?.ref, connectionString: project?.connectionString, schema: 'public', }) const { data: functionsData, isPending: isLoadingFunctions } = useEdgeFunctionsQuery({ projectRef: project?.ref, }) const { data: replicasData, isPending: isLoadingReplicas } = useReadReplicasQuery({ projectRef: project?.ref, }) const { data: branches } = useBranchesQuery({ projectRef: project?.parent_project_ref ?? project?.ref, }) const mainBranch = branches?.find((branch) => branch.is_default) const currentBranch = branches?.find((branch) => branch.project_ref === project?.ref) const isMainBranch = currentBranch?.name === mainBranch?.name let projectName = 'Welcome to your project' if (currentBranch && !isMainBranch) { projectName = currentBranch?.name } else if (project?.name) { projectName = project?.name } const tablesCount = Math.max(0, tablesData?.length ?? 0) const functionsCount = Math.max(0, functionsData?.length ?? 0) // [Joshen] JFYI minus 1 as the replicas endpoint returns the primary DB minimally const replicasCount = Math.max(0, (replicasData?.length ?? 1) - 1) if (isPaused) { return (
{tablesCount}
)}{functionsCount}
)}{replicasCount}
)}