import { useParams } from 'common' import dayjs from 'dayjs' import { Archive, Cpu, Database, GitBranch, Github } from 'lucide-react' import { useMemo } from 'react' import { cn, Skeleton } from 'ui' import { TimestampInfo } from 'ui-patterns' import { HighAvailabilityBadge } from './HighAvailabilityBadge' import { ServiceStatus } from './ServiceStatus' import { ComputeBadgeWrapper } from '@/components/ui/ComputeBadgeWrapper' import { SingleStat } from '@/components/ui/SingleStat' import { useBranchesQuery } from '@/data/branches/branches-query' import { useBackupsQuery } from '@/data/database/backups-query' import { DatabaseMigration, useMigrationsQuery } from '@/data/database/migrations-query' import { useGitHubConnectionsQuery } from '@/data/integrations/github-connections-query' import { useResourceWarningsQuery } from '@/data/usage/resource-warnings-query' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { PROJECT_STATUS } from '@/lib/constants' import { EMPTY_ARR } from '@/lib/void' export const ActivityStats = () => { const { ref } = useParams() const { data: project } = useSelectedProjectQuery() const { data: organization } = useSelectedOrganizationQuery() const { data: resourceWarnings } = useResourceWarningsQuery({ slug: organization?.slug }) const projectResourceWarnings = resourceWarnings?.find((warning) => warning.project === ref) const parentProjectRef = project?.parent_project_ref ?? project?.ref const { data: branchesData, isPending: isLoadingBranches } = useBranchesQuery({ projectRef: parentProjectRef, }) const isDefaultProject = project?.parent_project_ref === undefined const currentBranch = useMemo( () => (branchesData ?? []).find((b) => b.project_ref === ref), [branchesData, ref] ) const latestNonDefaultBranch = useMemo(() => { const list = (branchesData ?? []).filter((b) => !b.is_default) if (list.length === 0) return undefined return list .slice() .sort( (a, b) => new Date(b.created_at ?? b.updated_at).valueOf() - new Date(a.created_at ?? a.updated_at).valueOf() )[0] }, [branchesData]) const { data: migrationsData = EMPTY_ARR, isPending: isLoadingMigrations } = useMigrationsQuery({ projectRef: project?.ref, projectStatus: project?.status, connectionString: project?.connectionString, }) const latestMigration = useMemo( () => migrationsData[0], [migrationsData] ) const migrationLabelText = migrationsData.length === 0 ? 'No migrations' : (latestMigration?.name ?? 'Unknown') const { data: backupsData, isPending: isLoadingBackups } = useBackupsQuery({ projectRef: project?.ref, projectStatus: project?.status, }) const latestBackup = useMemo(() => { const list = backupsData?.backups ?? [] if (list.length === 0) return undefined return list .slice() .sort((a, b) => new Date(b.inserted_at).valueOf() - new Date(a.inserted_at).valueOf())[0] }, [backupsData]) const { data: githubConnections, isPending: isLoadingGithubConnections } = useGitHubConnectionsQuery({ organizationId: organization?.id }, { enabled: !!organization?.id }) const githubConnection = githubConnections?.find( (connection) => connection.project.ref === parentProjectRef ) const isProjectComingUp = [PROJECT_STATUS.COMING_UP, PROJECT_STATUS.UNKNOWN].includes( project?.status ?? PROJECT_STATUS.UNKNOWN ) const githubLabelText = githubConnection?.repository.name ? githubConnection.repository.name : isProjectComingUp ? 'Waiting for project...' : 'No repository connected' const integrationsPath = parentProjectRef ? `/project/${parentProjectRef}/settings/integrations` : undefined return (
} label={Compute} value={
{project?.infra_compute_size ? ( ) : (

Unknown

)} {project?.high_availability && }
} /> } label={GitHub} value={ isLoadingGithubConnections ? ( ) : (

{githubLabelText}

) } /> } label={{isDefaultProject ? 'Recent branch' : 'Branch Created'}} trackingProperties={{ stat_type: 'branches', stat_value: branchesData?.length ?? 0, }} value={ isLoadingBranches ? ( ) : isDefaultProject ? (

{latestNonDefaultBranch?.name ?? 'No branches'}

) : currentBranch?.created_at ? ( ) : (

Unknown

) } /> } label={Last migration} trackingProperties={{ stat_type: 'migrations', stat_value: migrationsData?.length ?? 0, }} value={ isLoadingMigrations ? ( ) : (

{migrationLabelText}

) } /> } label={Last backup} trackingProperties={{ stat_type: 'backups', stat_value: backupsData?.backups?.length ?? 0, }} value={ isLoadingBackups ? ( ) : backupsData?.pitr_enabled ? (

PITR enabled

) : latestBackup ? ( ) : (

No backups

) } />
) }