Files
supabase/apps/studio/components/layouts/AppLayout/ProjectDropdown.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

139 lines
5.0 KiB
TypeScript

import { ParsedUrlQuery } from 'querystring'
import { useParams } from 'common'
import { OrganizationProjectSelector } from 'components/ui/OrganizationProjectSelector'
import { useProjectDetailQuery } from 'data/projects/project-detail-query'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import { IS_PLATFORM } from 'lib/constants'
import { Box, Check, ChevronsUpDown, Plus } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useState } from 'react'
import { Badge, Button, cn, CommandGroup_Shadcn_, CommandItem_Shadcn_ } from 'ui'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
export const sanitizeRoute = (route: string, routerQueries: ParsedUrlQuery) => {
const queryArray = Object.entries(routerQueries)
if (queryArray.length > 1) {
// [Joshen] Ideally we shouldn't use hard coded numbers, but temp workaround
// for storage bucket route since its longer
const isStorageBucketRoute = 'bucketId' in routerQueries
const isSecurityAdvisorRoute = 'preset' in routerQueries
return route
.split('/')
.slice(0, isStorageBucketRoute || isSecurityAdvisorRoute ? 5 : 4)
.join('/')
} else {
return route
}
}
export const ProjectDropdown = () => {
const router = useRouter()
const { ref } = useParams()
const { data: project, isPending: isLoadingProject } = useSelectedProjectQuery()
const { data: selectedOrganization } = useSelectedOrganizationQuery()
const isBranch = project?.parentRef !== project?.ref
const { data: parentProject, isPending: isLoadingParentProject } = useProjectDetailQuery(
{ ref: project?.parent_project_ref },
{ enabled: isBranch }
)
const selectedProject = parentProject ?? project
const projectCreationEnabled = useIsFeatureEnabled('projects:create')
const [open, setOpen] = useState(false)
if (isLoadingProject || (isBranch && isLoadingParentProject) || !selectedProject) {
return <ShimmeringLoader className="w-[90px]" />
}
return IS_PLATFORM ? (
<>
<Link
href={`/project/${project?.ref}`}
className="flex items-center gap-2 flex-shrink-0 text-sm"
>
<Box size={14} strokeWidth={1.5} className="text-foreground-lighter" />
<span
title={selectedProject.name}
className="text-foreground max-w-32 lg:max-w-64 truncate"
>
{selectedProject.name}
</span>
</Link>
<OrganizationProjectSelector
open={open}
setOpen={setOpen}
selectedRef={ref}
onSelect={(project) => {
const sanitizedRoute = sanitizeRoute(router.route, router.query)
const href = sanitizedRoute?.replace('[ref]', project.ref) ?? `/project/${project.ref}`
router.push(href)
}}
renderTrigger={() => (
<Button
type="text"
size="tiny"
className={cn('px-1.5 py-4 [&_svg]:w-5 [&_svg]:h-5 ml-1')}
iconRight={<ChevronsUpDown strokeWidth={1.5} />}
/>
)}
renderRow={(project) => {
// [Joshen] Temp while we're interim between v1 and v2 billing
const sanitizedRoute = sanitizeRoute(router.route, router.query)
const href = sanitizedRoute?.replace('[ref]', project.ref) ?? `/project/${project.ref}`
const isSelected = project.ref === ref
const isPaused = project.status === 'INACTIVE'
return (
<Link href={href} className="w-full flex items-center justify-between">
<span className={cn('truncate', isSelected ? 'max-w-60' : 'max-w-64')}>
{project.name}
{isPaused && <Badge className="ml-2">Paused</Badge>}
</span>
{isSelected && <Check size={16} />}
</Link>
)
}}
renderActions={() => {
return (
projectCreationEnabled && (
<CommandGroup_Shadcn_>
<CommandItem_Shadcn_
className="cursor-pointer w-full"
onSelect={() => {
setOpen(false)
router.push(`/new/${selectedOrganization?.slug}`)
}}
onClick={() => setOpen(false)}
>
<Link
href={`/new/${selectedOrganization?.slug}`}
onClick={() => {
setOpen(false)
}}
className="w-full flex items-center gap-2"
>
<Plus size={14} strokeWidth={1.5} />
<p>New project</p>
</Link>
</CommandItem_Shadcn_>
</CommandGroup_Shadcn_>
)
)
}}
/>
</>
) : (
<Button type="text">
<span className="text-sm">{selectedProject?.name}</span>
</Button>
)
}