Files
Joshen Lim 9568584fcc Show last migration's name instead of timestamp in new home page (#43115)
## Context

In the new project home page, we have a stat for "Last migration" which
we're showing _when_ the last migration was applied. However:
- The timestamp for the migration is derived from the "version" column
of the migration (in the `supabase_migrations` table) which afaik is
derived from the migration's file name
- It'll be alright if the migration was generated via the CLI, but we
can't really enforce the name of the migration file if say they were
generated via AI, so this is technically a point of flakiness
- Reckon that it's more value to show _what_ was the last migration
rather than _when_ so opting to change the value here to show the name
of the last migration instead

### Before
<img width="620" height="311" alt="image"
src="https://github.com/user-attachments/assets/6876acb6-91d2-4ae3-8ce8-98375658c12c"
/>

### After
<img width="582" height="322" alt="image"
src="https://github.com/user-attachments/assets/a40f6635-2068-4edb-a91a-ccf03d8e4d3c"
/>
2026-02-25 23:20:23 +08:00

148 lines
5.2 KiB
TypeScript

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<DatabaseMigration | undefined>(
() => 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 (
<div className="@container">
<div className="grid grid-cols-1 @md:grid-cols-2 gap-2 @md:gap-6 flex-wrap">
<ServiceStatus />
<SingleStat
href={`/project/${ref}/database/migrations`}
icon={<Database size={18} strokeWidth={1.5} className="text-foreground" />}
label={<span>Last migration</span>}
trackingProperties={{
stat_type: 'migrations',
stat_value: migrationsData?.length ?? 0,
}}
value={
isLoadingMigrations ? (
<Skeleton className="h-6 w-24" />
) : (
<p className={!!latestMigration ? 'text-foreground' : 'text-foreground-lighter'}>
{migrationLabelText}
</p>
)
}
/>
<SingleStat
href={`/project/${ref}/database/backups/scheduled`}
icon={<Archive size={18} strokeWidth={1.5} className="text-foreground" />}
label={<span>Last backup</span>}
trackingProperties={{
stat_type: 'backups',
stat_value: backupsData?.backups?.length ?? 0,
}}
value={
isLoadingBackups ? (
<Skeleton className="h-6 w-24" />
) : backupsData?.pitr_enabled ? (
<p>PITR enabled</p>
) : latestBackup ? (
<TimestampInfo
className="text-base"
displayAs="utc"
label={dayjs(latestBackup.inserted_at).fromNow()}
utcTimestamp={latestBackup.inserted_at}
/>
) : (
<p className="text-foreground-lighter">No backups</p>
)
}
/>
<SingleStat
href={`/project/${ref}/branches`}
icon={<GitBranch size={18} strokeWidth={1.5} className="text-foreground" />}
label={<span>{isDefaultProject ? 'Recent branch' : 'Branch Created'}</span>}
trackingProperties={{
stat_type: 'branches',
stat_value: branchesData?.length ?? 0,
}}
value={
isLoadingBranches ? (
<Skeleton className="h-6 w-24" />
) : isDefaultProject ? (
<p
className={cn(
'truncate',
!latestNonDefaultBranch && 'text-foreground-lighter truncate'
)}
title={latestNonDefaultBranch?.name ?? 'No branches'}
>
{latestNonDefaultBranch?.name ?? 'No branches'}
</p>
) : currentBranch?.created_at ? (
<TimestampInfo
className="text-base"
label={dayjs(currentBranch.created_at).fromNow()}
utcTimestamp={currentBranch.created_at}
/>
) : (
<p className="text-foreground-lighter">Unknown</p>
)
}
/>
</div>
</div>
)
}