feat: show list name on header
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,11 @@ export function useLists() {
|
||||
return useQuery(listsQueryOptions())
|
||||
}
|
||||
|
||||
/** The list a user lands on: their primary one, falling back to the first. */
|
||||
export function pickPrimaryList(lists: ShoppingListSummary[]): ShoppingListSummary | undefined {
|
||||
return lists.find((list) => list.isPrimary) ?? lists[0]
|
||||
}
|
||||
|
||||
export function useCreateList() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
import type { ShoppingListSummary } from '@superlist/shared'
|
||||
import { createFileRoute, Link } from '@tanstack/react-router'
|
||||
import { PlusIcon, ShoppingCartIcon } from 'lucide-react'
|
||||
|
||||
import { CreateItemForm } from '@/components/items/create-item-form'
|
||||
import { ItemList } from '@/components/items/item-list'
|
||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { listsQueryOptions, useLists } from '@/hooks/use-lists'
|
||||
import { listsQueryOptions, pickPrimaryList, useLists } from '@/hooks/use-lists'
|
||||
import { ApiError } from '@/lib/api-client'
|
||||
|
||||
export const Route = createFileRoute('/_authenticated/')({
|
||||
@@ -17,11 +15,6 @@ export const Route = createFileRoute('/_authenticated/')({
|
||||
component: HomePage,
|
||||
})
|
||||
|
||||
/** The list a user lands on: their primary one, falling back to the first. */
|
||||
function pickPrimaryList(lists: ShoppingListSummary[]): ShoppingListSummary | undefined {
|
||||
return lists.find((list) => list.isPrimary) ?? lists[0]
|
||||
}
|
||||
|
||||
function HomePage() {
|
||||
const { data: lists, isPending, error } = useLists()
|
||||
|
||||
@@ -43,7 +36,6 @@ function HomePage() {
|
||||
if (isPending || !lists) {
|
||||
return (
|
||||
<div className="mx-auto flex w-full max-w-2xl flex-col gap-4">
|
||||
<Skeleton className="h-8 w-48" />
|
||||
<Skeleton className="h-10 w-full" />
|
||||
<Skeleton className="h-12 w-full" />
|
||||
<Skeleton className="h-12 w-full" />
|
||||
@@ -59,11 +51,6 @@ function HomePage() {
|
||||
|
||||
return (
|
||||
<div className="mx-auto flex w-full max-w-2xl flex-col gap-4 md:gap-6">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<h1 className="text-lg font-semibold">{primaryList.name}</h1>
|
||||
{primaryList.isPrimary && <Badge>Principal</Badge>}
|
||||
</div>
|
||||
|
||||
<CreateItemForm listId={primaryList.id} autoFocus />
|
||||
|
||||
<ItemList listId={primaryList.id} />
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { createFileRoute, Link } from '@tanstack/react-router'
|
||||
import { ArrowLeftIcon, SettingsIcon } from 'lucide-react'
|
||||
import { createFileRoute } from '@tanstack/react-router'
|
||||
|
||||
import { CreateItemForm } from '@/components/items/create-item-form'
|
||||
import { ItemList } from '@/components/items/item-list'
|
||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { itemsQueryOptions } from '@/hooks/use-items'
|
||||
import { listQueryOptions, useList } from '@/hooks/use-list'
|
||||
@@ -43,7 +40,6 @@ function ListItemsPage() {
|
||||
if (isPending || !list) {
|
||||
return (
|
||||
<div className="mx-auto flex w-full max-w-2xl flex-col gap-4">
|
||||
<Skeleton className="h-8 w-48" />
|
||||
<Skeleton className="h-10 w-full" />
|
||||
<Skeleton className="h-12 w-full" />
|
||||
<Skeleton className="h-12 w-full" />
|
||||
@@ -53,31 +49,6 @@ function ListItemsPage() {
|
||||
|
||||
return (
|
||||
<div className="mx-auto flex w-full max-w-2xl flex-col gap-4 md:gap-6">
|
||||
<div className="flex flex-col gap-2">
|
||||
<Link
|
||||
to="/lists"
|
||||
className="text-muted-foreground hover:text-foreground inline-flex items-center gap-1 text-sm"
|
||||
>
|
||||
<ArrowLeftIcon className="size-4" />
|
||||
Listas
|
||||
</Link>
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<h1 className="text-lg font-semibold break-words">{list.name}</h1>
|
||||
{list.isPrimary && <Badge>Principal</Badge>}
|
||||
<Badge variant="secondary">
|
||||
{list.role === 'owner' ? 'Propietario' : 'Colaborador'}
|
||||
</Badge>
|
||||
</div>
|
||||
<Button variant="outline" size="sm" asChild>
|
||||
<Link to="/lists/$listId/settings" params={{ listId: list.id }}>
|
||||
<SettingsIcon className="size-4" />
|
||||
Gestionar
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<CreateItemForm listId={list.id} autoFocus />
|
||||
|
||||
<ItemList listId={list.id} />
|
||||
|
||||
Reference in New Issue
Block a user