import { useParams } from 'common' 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 dayjs from 'dayjs' import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject' import { Archive, Database, GitBranch } from 'lucide-react' import { useMemo } from 'react' import { cn, Skeleton } from 'ui' import { TimestampInfo } from 'ui-patterns' import { ServiceStatus } from './ServiceStatus' import { EMPTY_ARR } from '@/lib/void' export const ActivityStats = () => { const { ref } = useParams() const { data: project } = useSelectedProjectQuery() const { data: branchesData, isPending: isLoadingBranches } = useBranchesQuery({ projectRef: project?.parent_project_ref ?? project?.ref, }) 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, 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, }) 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]) return (
} 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

) } /> } 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

) } />
) }