diff --git a/apps/web/src/components/items/create-item-form.tsx b/apps/web/src/components/items/create-item-form.tsx index 6cd2fb2..8aa2d4a 100644 --- a/apps/web/src/components/items/create-item-form.tsx +++ b/apps/web/src/components/items/create-item-form.tsx @@ -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() } diff --git a/apps/web/src/components/items/item-row.tsx b/apps/web/src/components/items/item-row.tsx index a62cef8..660374a 100644 --- a/apps/web/src/components/items/item-row.tsx +++ b/apps/web/src/components/items/item-row.tsx @@ -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" > 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 (
-
+
- -

{title}

-
+ + {list ? ( + + ) : ( +

{titleFor(pathname)}

+ )} +
+ {list && ( + + )}
) } + +function ListHeading({ list, showBack }: { list: HeaderList; showBack: boolean }) { + return ( +
+ {showBack && ( + + )} +

{list.name}

+ {list.isPrimary && Principal} + + {list.role === 'owner' ? 'Propietario' : 'Colaborador'} + +
+ ) +} diff --git a/apps/web/src/hooks/use-lists.ts b/apps/web/src/hooks/use-lists.ts index 9e83249..52febe3 100644 --- a/apps/web/src/hooks/use-lists.ts +++ b/apps/web/src/hooks/use-lists.ts @@ -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() diff --git a/apps/web/src/routes/_authenticated/index.tsx b/apps/web/src/routes/_authenticated/index.tsx index 564993b..c99b566 100644 --- a/apps/web/src/routes/_authenticated/index.tsx +++ b/apps/web/src/routes/_authenticated/index.tsx @@ -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 (
- @@ -59,11 +51,6 @@ function HomePage() { return (
-
-

{primaryList.name}

- {primaryList.isPrimary && Principal} -
- diff --git a/apps/web/src/routes/_authenticated/lists/$listId/index.tsx b/apps/web/src/routes/_authenticated/lists/$listId/index.tsx index 7719e28..4821b5d 100644 --- a/apps/web/src/routes/_authenticated/lists/$listId/index.tsx +++ b/apps/web/src/routes/_authenticated/lists/$listId/index.tsx @@ -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 (
- @@ -53,31 +49,6 @@ function ListItemsPage() { return (
-
- - - Listas - -
-
-

{list.name}

- {list.isPrimary && Principal} - - {list.role === 'owner' ? 'Propietario' : 'Colaborador'} - -
- -
-
-