feat: show list name on header

This commit is contained in:
Jose Selesan
2026-10-19 12:46:40 -03:00
parent c374db1616
commit bba10361f0
6 changed files with 79 additions and 52 deletions

View File

@@ -35,6 +35,11 @@ const NEW_ITEM_INPUT_ID = 'new-item-input'
*/
export function focusNewItemInput(pathname: string): void {
if (pathname !== '/') return
refocusNewItemInput()
}
/** Puts the cursor back in the "add an item" input of the page on screen, if it has one. */
export function refocusNewItemInput(): void {
document.getElementById(NEW_ITEM_INPUT_ID)?.focus()
}

View File

@@ -1,6 +1,7 @@
import type { ListItem } from '@superlist/shared'
import { CheckIcon } from 'lucide-react'
import { refocusNewItemInput } from '@/components/items/create-item-form'
import { Card, CardContent } from '@/components/ui/card'
import { isOptimisticItem, type useUpdateItem } from '@/hooks/use-items'
import { cn } from '@/lib/utils'
@@ -21,7 +22,11 @@ export function ItemRow({ item, updateItem }: ItemRowProps) {
aria-pressed={item.completed}
// A placeholder has no server id yet, so it cannot be toggled.
disabled={updateItem.isPending || isOptimisticItem(item)}
onClick={() => updateItem.mutate({ itemId: item.id, completed: !item.completed })}
onClick={() => {
updateItem.mutate({ itemId: item.id, completed: !item.completed })
// Back to the input, so the next item can be typed right away.
refocusNewItemInput()
}}
className="flex w-full cursor-pointer items-center gap-3 p-3 text-left disabled:cursor-default disabled:opacity-60"
>
<span

View File

@@ -1,9 +1,17 @@
import { useRouterState } from '@tanstack/react-router'
import { SearchIcon } from 'lucide-react'
import type { ShoppingListSummary } from '@superlist/shared'
import { useQuery } from '@tanstack/react-query'
import { Link, useMatch, useRouterState } from '@tanstack/react-router'
import { ArrowLeftIcon, SearchIcon, SettingsIcon } from 'lucide-react'
import { ThemeToggle } from '@/components/theme-toggle'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Separator } from '@/components/ui/separator'
import { SidebarTrigger } from '@/components/ui/sidebar'
import { listQueryOptions } from '@/hooks/use-list'
import { listsQueryOptions, pickPrimaryList } from '@/hooks/use-lists'
type HeaderList = Pick<ShoppingListSummary, 'id' | 'name' | 'role' | 'isPrimary'>
function titleFor(pathname: string) {
if (pathname === '/') return 'Panel'
@@ -14,15 +22,34 @@ function titleFor(pathname: string) {
export function AppHeader() {
const pathname = useRouterState({ select: (state) => state.location.pathname })
const title = titleFor(pathname)
const listMatch = useMatch({ from: '/_authenticated/lists/$listId/', shouldThrow: false })
const homeMatch = useMatch({ from: '/_authenticated/', shouldThrow: false })
const listId = listMatch?.params.listId
// The list pages carry their name, badges and actions here instead of in the body;
// both route loaders have already cached the data, so these don't add requests.
const { data: openList } = useQuery({
...listQueryOptions(listId ?? ''),
enabled: Boolean(listId),
})
const { data: lists } = useQuery({ ...listsQueryOptions(), enabled: Boolean(homeMatch) })
const list: HeaderList | undefined = listId
? openList
: homeMatch && lists
? pickPrimaryList(lists)
: undefined
return (
<header className="bg-background/95 supports-[backdrop-filter]:bg-background/60 sticky top-0 z-10 flex h-16 shrink-0 items-center gap-2 border-b backdrop-blur">
<div className="flex w-full items-center gap-2 px-4">
<div className="flex w-full min-w-0 items-center gap-2 px-4">
<SidebarTrigger className="-ml-1 hidden md:inline-flex" />
<Separator orientation="vertical" className="mr-2 h-4" />
<h1 className="truncate text-sm font-medium">{title}</h1>
<div className="ml-auto flex items-center gap-1">
<Separator orientation="vertical" className="mr-2 hidden h-4 md:block" />
{list ? (
<ListHeading list={list} showBack={Boolean(listId)} />
) : (
<h1 className="truncate text-sm font-medium">{titleFor(pathname)}</h1>
)}
<div className="ml-auto flex shrink-0 items-center gap-1">
<button
type="button"
className="text-muted-foreground hover:bg-accent hover:text-accent-foreground mr-1 hidden items-center gap-2 rounded-md border px-3 py-1.5 text-xs md:flex"
@@ -33,9 +60,36 @@ export function AppHeader() {
⌘K
</kbd>
</button>
{list && (
<Button variant="outline" size="sm" asChild>
<Link to="/lists/$listId/settings" params={{ listId: list.id }}>
<SettingsIcon className="size-4" />
<span className="hidden sm:inline">Gestionar</span>
</Link>
</Button>
)}
<ThemeToggle />
</div>
</div>
</header>
)
}
function ListHeading({ list, showBack }: { list: HeaderList; showBack: boolean }) {
return (
<div className="flex min-w-0 items-center gap-2">
{showBack && (
<Button variant="ghost" size="icon" className="-ml-2 size-8 shrink-0" asChild>
<Link to="/lists" aria-label="Volver a listas">
<ArrowLeftIcon className="size-4" />
</Link>
</Button>
)}
<h1 className="truncate text-sm font-medium">{list.name}</h1>
{list.isPrimary && <Badge className="shrink-0">Principal</Badge>}
<Badge variant="secondary" className="hidden shrink-0 sm:inline-flex">
{list.role === 'owner' ? 'Propietario' : 'Colaborador'}
</Badge>
</div>
)
}