Files
supabase/apps/studio/components/interfaces/HomeNew/ActivityStats.tsx
Joshen Lim 6d6510c337 Fix new home page recent branch stat (#42497)
## Context

Small fix for the new home page "Recent branch" stat to add some
truncate

Before:
<img width="500" height="198" alt="image"
src="https://github.com/user-attachments/assets/f6a73934-52ea-4ed2-853d-3e4b0da054ab"
/>


After:
<img width="500" height="343" alt="image"
src="https://github.com/user-attachments/assets/9ede0378-b410-41f5-9f9d-e4273e76fcdd"
/>

Unrelated but I also set a max width to the project and branch dropdowns
in the layout header
<img width="950" height="69" alt="image"
src="https://github.com/user-attachments/assets/d9fc52f6-50c8-4adb-9a79-da3b64eb7a7f"
/>


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Style**
* Enhanced display of branch and project names with improved truncation
and wider max-widths.
* Added hover tooltips (title attributes) for branch and project names
to improve accessibility.
* Refined text overflow handling and styling across dropdowns and stat
components.
* **Tests**
* Adjusted clipboard test timing to await the resolved promise before
asserting write() was called.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-05 19:10:08 +08:00

165 lines
5.7 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 { parseMigrationVersion } from 'lib/migration-utils'
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'
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, isPending: isLoadingMigrations } = useMigrationsQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
})
const latestMigration = useMemo<DatabaseMigration | undefined>(
() => (migrationsData ?? [])[0],
[migrationsData]
)
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])
const [versionLabel, versionTimestamp] = useMemo(() => {
const version = latestMigration?.version
const versionDayjs = parseMigrationVersion(version)
if (versionDayjs) {
return [versionDayjs.fromNow(), versionDayjs.toISOString()]
}
return [undefined, undefined]
}, [latestMigration])
const hasValidVersion = versionLabel && versionTimestamp
const versionLabelText = migrationsData && migrationsData.length > 0 ? 'Unknown' : 'No migrations'
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" />
) : hasValidVersion ? (
<TimestampInfo
className="text-base"
label={versionLabel}
utcTimestamp={versionTimestamp}
/>
) : (
<p className="text-foreground-lighter">{versionLabelText}</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>
)
}