From 09cedc7106380c105f2ad6de734b2deea103a757 Mon Sep 17 00:00:00 2001 From: Chia1104 Date: Sun, 15 Nov 2026 02:00:19 +0800 Subject: [PATCH 1/4] feat(www): site modes with a hobby media log A mode is a URL space with its own frame. Tech pages move into the `(tech)` route group; `/hobby` gets its own frame, palette (`@chia/themes/hobby.css`, keyed off `data-site-mode`) and a mode switch in the header, the tech footer and the command menu. The reader palette applies to the tech mode only. The hobby pages read a new media log: `chia_media_entry`, catalogue clients for TMDB, AniList and IGDB, a `media` oRPC module that snapshots a work and copies its cover into storage, and a dash page to manage it. Co-Authored-By: Claude Fable 5.2 --- apps/AGENTS.md | 3 +- apps/dash/.next/dev/types/link.d.ts | 1 + apps/dash/.next/dev/types/routes.d.ts | 6 +- apps/dash/__tests__/media-form.test.ts | 57 + .../(workspace)/(operator)/media/layout.tsx | 12 + .../app/(workspace)/(operator)/media/page.tsx | 24 + .../src/components/assets/file-explorer.tsx | 3 +- apps/dash/src/components/assets/utils.ts | 2 - apps/dash/src/components/media/add-drawer.tsx | 209 + apps/dash/src/components/media/entry-form.tsx | 374 + apps/dash/src/components/media/form.ts | 158 + .../src/components/media/media-manager.tsx | 323 + apps/dash/src/shared/routes.tsx | 28 +- apps/service/.env.example | 7 + apps/www/.next/dev/types/link.d.ts | 24 +- apps/www/.next/dev/types/routes.d.ts | 15 +- apps/www/DESIGN.md | 11 + apps/www/__tests__/libs/palette.test.ts | 14 +- apps/www/__tests__/shared/routes.test.ts | 7 +- .../(blog)/[type]/[slug]/error.ts | 0 .../(blog)/[type]/[slug]/llm.md/route.ts | 0 .../(blog)/[type]/[slug]/not-found.ts | 0 .../(blog)/[type]/[slug]/opengraph-image.tsx | 0 .../(blog)/[type]/[slug]/page.tsx | 0 .../{ => (tech)}/(blog)/[type]/error.ts | 0 .../{ => (tech)}/(blog)/[type]/not-found.ts | 0 .../(blog)/[type]/opengraph-image.tsx | 0 .../{ => (tech)}/(blog)/[type]/page.tsx | 0 .../[locale]/{ => (tech)}/(blog)/layout.tsx | 0 .../{ => (tech)}/(blog)/tags/[slug]/page.tsx | 0 .../{ => (tech)}/(blog)/tags/page.tsx | 0 .../{ => (tech)}/contact/opengraph-image.tsx | 0 .../[locale]/{ => (tech)}/contact/page.tsx | 0 .../src/app/[locale]/{ => (tech)}/error.ts | 0 apps/www/src/app/[locale]/(tech)/layout.tsx | 12 + .../app/[locale]/{ => (tech)}/not-found.ts | 0 .../src/app/[locale]/{ => (tech)}/page.tsx | 0 .../[locale]/{ => (tech)}/projects/error.ts | 0 .../[locale]/{ => (tech)}/projects/layout.tsx | 0 .../projects/loading-fallbacks.tsx | 0 .../{ => (tech)}/projects/loading.tsx | 0 .../[locale]/{ => (tech)}/projects/page.tsx | 0 .../src/app/[locale]/hobby/[kind]/page.tsx | 80 + apps/www/src/app/[locale]/hobby/layout.tsx | 12 + apps/www/src/app/[locale]/hobby/page.tsx | 67 + apps/www/src/app/global-not-found.tsx | 5 +- apps/www/src/app/sitemap.ts | 25 +- .../www/src/components/commons/app-layout.tsx | 28 +- .../src/components/commons/command-menu.tsx | 178 + apps/www/src/components/commons/copyright.tsx | 16 + apps/www/src/components/commons/footer.tsx | 39 +- .../src/components/commons/mode-switch.tsx | 39 + apps/www/src/components/commons/nav-menu.tsx | 171 +- apps/www/src/components/commons/settings.tsx | 11 +- .../www/src/components/commons/site-shell.tsx | 7 +- .../www/src/components/commons/tech-frame.tsx | 32 + .../www/src/components/hobby/hobby-footer.tsx | 65 + apps/www/src/components/hobby/hobby-frame.tsx | 31 + apps/www/src/components/hobby/hobby-nav.tsx | 74 + apps/www/src/components/hobby/media-card.tsx | 98 + .../src/components/hobby/media-section.tsx | 53 + apps/www/src/libs/orpc/client.rsc.ts | 1 + apps/www/src/libs/palette.ts | 5 +- apps/www/src/shared/media.ts | 33 + apps/www/src/shared/routes/index.tsx | 179 +- apps/www/src/shared/site-mode.ts | 16 + apps/www/src/styles/globals.css | 1 + .../20261114172203_media_entry/migration.sql | 21 + .../20261114172203_media_entry/snapshot.json | 6827 +++++++++++++++++ packages/db/package.json | 8 + packages/db/src/libs/media/index.ts | 117 + packages/db/src/libs/validator/media.ts | 31 + packages/db/src/schemas/media.schema.ts | 53 + packages/db/src/schemas/relations.ts | 2 + packages/db/src/schemas/schema.ts | 7 + packages/db/src/types.ts | 28 + packages/i18n/www/en-US.json | 38 +- packages/i18n/www/zh-TW.json | 38 +- packages/integrations/AGENTS.md | 2 +- packages/integrations/anilist/index.spec.ts | 62 + packages/integrations/anilist/index.ts | 107 + packages/integrations/igdb/env.ts | 17 + packages/integrations/igdb/index.spec.ts | 64 + packages/integrations/igdb/index.ts | 121 + packages/integrations/package.json | 12 + packages/integrations/s3/s3.service.ts | 17 + packages/integrations/tmdb/env.ts | 15 + packages/integrations/tmdb/index.spec.ts | 82 + packages/integrations/tmdb/index.ts | 112 + packages/integrations/turbo.json | 6 + packages/services/AGENTS.md | 1 + .../media/__tests__/media.route.test.ts | 297 + packages/services/media/catalogue.service.ts | 196 + packages/services/media/media.contract.ts | 162 + packages/services/media/media.route.ts | 166 + packages/services/media/poster.service.ts | 50 + packages/services/router.contract.ts | 2 + packages/services/router.ts | 2 + packages/themes/AGENTS.md | 1 + packages/themes/hobby.css | 56 + packages/themes/package.json | 6 +- packages/utils/src/config/index.ts | 5 + 102 files changed, 10958 insertions(+), 329 deletions(-) create mode 100644 apps/dash/__tests__/media-form.test.ts create mode 100644 apps/dash/src/app/(workspace)/(operator)/media/layout.tsx create mode 100644 apps/dash/src/app/(workspace)/(operator)/media/page.tsx create mode 100644 apps/dash/src/components/media/add-drawer.tsx create mode 100644 apps/dash/src/components/media/entry-form.tsx create mode 100644 apps/dash/src/components/media/form.ts create mode 100644 apps/dash/src/components/media/media-manager.tsx rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/[slug]/error.ts (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/[slug]/llm.md/route.ts (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/[slug]/not-found.ts (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/[slug]/opengraph-image.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/[slug]/page.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/error.ts (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/not-found.ts (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/opengraph-image.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/[type]/page.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/layout.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/tags/[slug]/page.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/(blog)/tags/page.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/contact/opengraph-image.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/contact/page.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/error.ts (100%) create mode 100644 apps/www/src/app/[locale]/(tech)/layout.tsx rename apps/www/src/app/[locale]/{ => (tech)}/not-found.ts (100%) rename apps/www/src/app/[locale]/{ => (tech)}/page.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/projects/error.ts (100%) rename apps/www/src/app/[locale]/{ => (tech)}/projects/layout.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/projects/loading-fallbacks.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/projects/loading.tsx (100%) rename apps/www/src/app/[locale]/{ => (tech)}/projects/page.tsx (100%) create mode 100644 apps/www/src/app/[locale]/hobby/[kind]/page.tsx create mode 100644 apps/www/src/app/[locale]/hobby/layout.tsx create mode 100644 apps/www/src/app/[locale]/hobby/page.tsx create mode 100644 apps/www/src/components/commons/command-menu.tsx create mode 100644 apps/www/src/components/commons/copyright.tsx create mode 100644 apps/www/src/components/commons/mode-switch.tsx create mode 100644 apps/www/src/components/commons/tech-frame.tsx create mode 100644 apps/www/src/components/hobby/hobby-footer.tsx create mode 100644 apps/www/src/components/hobby/hobby-frame.tsx create mode 100644 apps/www/src/components/hobby/hobby-nav.tsx create mode 100644 apps/www/src/components/hobby/media-card.tsx create mode 100644 apps/www/src/components/hobby/media-section.tsx create mode 100644 apps/www/src/shared/media.ts create mode 100644 apps/www/src/shared/site-mode.ts create mode 100644 packages/db/.drizzle/migrations/20261114172203_media_entry/migration.sql create mode 100644 packages/db/.drizzle/migrations/20261114172203_media_entry/snapshot.json create mode 100644 packages/db/src/libs/media/index.ts create mode 100644 packages/db/src/libs/validator/media.ts create mode 100644 packages/db/src/schemas/media.schema.ts create mode 100644 packages/integrations/anilist/index.spec.ts create mode 100644 packages/integrations/anilist/index.ts create mode 100644 packages/integrations/igdb/env.ts create mode 100644 packages/integrations/igdb/index.spec.ts create mode 100644 packages/integrations/igdb/index.ts create mode 100644 packages/integrations/tmdb/env.ts create mode 100644 packages/integrations/tmdb/index.spec.ts create mode 100644 packages/integrations/tmdb/index.ts create mode 100644 packages/services/media/__tests__/media.route.test.ts create mode 100644 packages/services/media/catalogue.service.ts create mode 100644 packages/services/media/media.contract.ts create mode 100644 packages/services/media/media.route.ts create mode 100644 packages/services/media/poster.service.ts create mode 100644 packages/themes/hobby.css diff --git a/apps/AGENTS.md b/apps/AGENTS.md index 3e3217e89..08ef273ef 100644 --- a/apps/AGENTS.md +++ b/apps/AGENTS.md @@ -10,7 +10,8 @@ Local ports: `www` 3000, `dash` 3001, `service` 3005, `workflow` 3008. `apps/fun ## `www` - `src/libs/orpc/client.rsc.ts` is server-only and may attach `CH_API_KEY`; the browser client must never receive an API key. -- Content pages have no revalidate timer. `src/libs/orpc/client.rsc.ts` tags each server read with `WwwCacheTag` or `wwwFeedCacheTag`, and `workflow` invalidates those tags through the Vercel API, one five-minute window per tag; nothing calls into www. A write that changes what www renders must end in `scheduleFeedRevalidationStep` (in a workflow) or `workflow.startSiteRevalidation(tag)`. +- A mode is a URL space with its own frame, so the route decides it and nothing stores it. `src/app/[locale]/layout.tsx` renders the document and what every mode shares (providers, the page container, the chat dock); tech pages live under `(tech)` with the ruled frame, hobby pages under `hobby` with theirs. A frame that brings its own palette marks itself with `data-site-mode`: `@chia/themes/hobby.css` keys off it through `:root:has()` and the reader palette steps aside there. +- Content pages have no revalidate timer. `src/libs/orpc/client.rsc.ts` tags each server read with `WwwCacheTag` or `wwwFeedCacheTag`, and `workflow` invalidates those tags through the Vercel API, one five-minute window per tag; nothing calls into www. A write that changes what www renders must end in `scheduleFeedRevalidationStep` (in a workflow) or `workflow.startSiteRevalidation(tag)`. Hobby pages read only `media.list`, tagged `WwwCacheTag.Media`. - The blog navigation and related posts load in the browser, so no cached page depends on a post other than its own. Home and projects keep a timer because Spotify and GitHub send no change event. - Pages set canonical, hreflang and `og:url` through `localizedMetadata` in `src/libs/i18n/alternates.ts`; the middleware sends no alternate links. Every URL the site emits (links, sitemap, RSS, `feedUrl`) leaves the default locale unprefixed, because `/zh-TW/…` redirects. - Browser requests use the Better Auth session cookie and may access only public or caller-owned procedures. Shared reads (`@chia/services/shared/shared-reads`) go without credentials so a CDN can serve them. diff --git a/apps/dash/.next/dev/types/link.d.ts b/apps/dash/.next/dev/types/link.d.ts index 177195414..1f636c3a0 100644 --- a/apps/dash/.next/dev/types/link.d.ts +++ b/apps/dash/.next/dev/types/link.d.ts @@ -45,6 +45,7 @@ declare namespace __next_route_internal_types__ { | `/feed/notes` // ../../../src/app/(workspace)/(operator)/feed/(manage)/notes/page.tsx | `/feed/posts` // ../../../src/app/(workspace)/(operator)/feed/(manage)/posts/page.tsx | `/icon.png` // ../../../src/app/icon.png + | `/media` // ../../../src/app/(workspace)/(operator)/media/page.tsx | `/memory` // ../../../src/app/(workspace)/(operator)/memory/page.tsx | `/profile` // ../../../src/app/(workspace)/(operator)/profile/page.tsx | `/rag` // ../../../src/app/(workspace)/(operator)/rag/page.tsx diff --git a/apps/dash/.next/dev/types/routes.d.ts b/apps/dash/.next/dev/types/routes.d.ts index 2c5705143..6e9c17a2a 100644 --- a/apps/dash/.next/dev/types/routes.d.ts +++ b/apps/dash/.next/dev/types/routes.d.ts @@ -1,10 +1,10 @@ // This file is generated automatically by Next.js // Do not edit this file manually -type AppRoutes = "/" | "/agents" | "/api-keys" | "/assets" | "/auth/callback/sent-success" | "/auth/login" | "/feed" | "/feed/draft/[id]" | "/feed/drafts" | "/feed/edit/[id]" | "/feed/notes" | "/feed/posts" | "/memory" | "/profile" | "/rag" | "/rag/chunks" | "/rag/maintenance" | "/rag/runs" | "/reports" | "/reports/[id]" | "/settings" | "/settings/spotify" | "/tags" | "/users" +type AppRoutes = "/" | "/agents" | "/api-keys" | "/assets" | "/auth/callback/sent-success" | "/auth/login" | "/feed" | "/feed/draft/[id]" | "/feed/drafts" | "/feed/edit/[id]" | "/feed/notes" | "/feed/posts" | "/media" | "/memory" | "/profile" | "/rag" | "/rag/chunks" | "/rag/maintenance" | "/rag/runs" | "/reports" | "/reports/[id]" | "/settings" | "/settings/spotify" | "/tags" | "/users" type AppRouteHandlerRoutes = "/api/v1/health" | "/favicon.ico" | "/icon.png" | "/robots.txt" type PageRoutes = never -type LayoutRoutes = "/" | "/agents" | "/auth" | "/feed" | "/memory" | "/profile" | "/rag" | "/reports" | "/settings" | "/tags" +type LayoutRoutes = "/" | "/agents" | "/auth" | "/feed" | "/media" | "/memory" | "/profile" | "/rag" | "/reports" | "/settings" | "/tags" type RedirectRoutes = never type RewriteRoutes = never type Routes = AppRoutes | PageRoutes | LayoutRoutes | RedirectRoutes | RewriteRoutes | AppRouteHandlerRoutes @@ -27,6 +27,7 @@ interface ParamMap { "/feed/notes": {} "/feed/posts": {} "/icon.png": {} + "/media": {} "/memory": {} "/profile": {} "/rag": {} @@ -50,6 +51,7 @@ interface LayoutSlotMap { "/agents": never "/auth": never "/feed": never + "/media": never "/memory": never "/profile": never "/rag": never diff --git a/apps/dash/__tests__/media-form.test.ts b/apps/dash/__tests__/media-form.test.ts new file mode 100644 index 000000000..fdcd9ff43 --- /dev/null +++ b/apps/dash/__tests__/media-form.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, it } from "vitest"; + +import { Locale, MediaStatus } from "@chia/db/types"; + +import { mediaFormSchema } from "@/components/media/form"; +import type { MediaFormInput } from "@/components/media/form"; + +const fields = (overrides: Partial = {}): MediaFormInput => ({ + status: MediaStatus.Completed, + rating: 8, + favorite: false, + startedOn: "", + finishedOn: "2026-11-01", + originalTitle: " 葬送のフリーレン ", + releaseYear: 2023, + translations: { + [Locale.ZhTW]: { title: "葬送的芙莉蓮", comment: " 很好看 " }, + [Locale.En]: { title: "", comment: "ignored without a title" }, + }, + ...overrides, +}); + +describe("mediaFormSchema", () => { + it("turns blank fields into nulls and drops a locale without a title", () => { + expect(mediaFormSchema.parse(fields())).toEqual({ + status: MediaStatus.Completed, + rating: 8, + favorite: false, + startedOn: null, + finishedOn: "2026-11-01", + originalTitle: "葬送のフリーレン", + releaseYear: 2023, + translations: { + [Locale.ZhTW]: { title: "葬送的芙莉蓮", comment: "很好看" }, + }, + }); + }); + + it("requires a title in at least one locale", () => { + const result = mediaFormSchema.safeParse( + fields({ + translations: { + [Locale.ZhTW]: { title: " ", comment: "" }, + [Locale.En]: { title: "", comment: "" }, + }, + }) + ); + expect(result.success).toBe(false); + }); + + it("refuses a finish date before the start date", () => { + const result = mediaFormSchema.safeParse( + fields({ startedOn: "2026-11-02" }) + ); + expect(result.error?.issues[0]?.path).toEqual(["finishedOn"]); + }); +}); diff --git a/apps/dash/src/app/(workspace)/(operator)/media/layout.tsx b/apps/dash/src/app/(workspace)/(operator)/media/layout.tsx new file mode 100644 index 000000000..f1b78ee1b --- /dev/null +++ b/apps/dash/src/app/(workspace)/(operator)/media/layout.tsx @@ -0,0 +1,12 @@ +import type { ReactNode } from "react"; +import { ViewTransition } from "react"; + +const Layout = ({ children }: { children: ReactNode }) => { + return ( + +
{children}
+
+ ); +}; + +export default Layout; diff --git a/apps/dash/src/app/(workspace)/(operator)/media/page.tsx b/apps/dash/src/app/(workspace)/(operator)/media/page.tsx new file mode 100644 index 000000000..1e1224708 --- /dev/null +++ b/apps/dash/src/app/(workspace)/(operator)/media/page.tsx @@ -0,0 +1,24 @@ +import "server-only"; +import { Suspense } from "react"; + +import { Spinner } from "@heroui/react"; + +import { MediaManager } from "@/components/media/media-manager"; + +export const dynamic = "force-dynamic"; + +const MediaPage = () => ( +
+

Media log

+ + + + }> + + +
+); + +export default MediaPage; diff --git a/apps/dash/src/components/assets/file-explorer.tsx b/apps/dash/src/components/assets/file-explorer.tsx index b7e78616b..3c1a9d570 100644 --- a/apps/dash/src/components/assets/file-explorer.tsx +++ b/apps/dash/src/components/assets/file-explorer.tsx @@ -7,6 +7,8 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { FolderOpen, Info } from "lucide-react"; import { toast } from "sonner"; +import { STORAGE_BASE_URL } from "@chia/utils/config"; + import { orpc } from "@/libs/orpc/client"; import { DeleteConfirmModal } from "./delete-confirm-modal"; @@ -14,7 +16,6 @@ import { ExplorerToolbar } from "./explorer-toolbar"; import { FileDetailPanel } from "./file-detail-panel"; import { FileTreeDrawer } from "./file-tree-drawer"; import { FileTreePanel } from "./file-tree-panel"; -import { STORAGE_BASE_URL } from "./utils"; export const FileExplorer = () => { const [selectedPath, setSelectedPath] = useState(null); diff --git a/apps/dash/src/components/assets/utils.ts b/apps/dash/src/components/assets/utils.ts index a4d5855ac..f8467af7c 100644 --- a/apps/dash/src/components/assets/utils.ts +++ b/apps/dash/src/components/assets/utils.ts @@ -1,5 +1,3 @@ -export const STORAGE_BASE_URL = "https://storage.chia1104.dev"; - export function formatBytes(bytes: number, decimals = 2): string { if (bytes === 0) return "0 Bytes"; const k = 1024; diff --git a/apps/dash/src/components/media/add-drawer.tsx b/apps/dash/src/components/media/add-drawer.tsx new file mode 100644 index 000000000..2073eb601 --- /dev/null +++ b/apps/dash/src/components/media/add-drawer.tsx @@ -0,0 +1,209 @@ +"use client"; + +import { useState } from "react"; + +import { + Button, + Drawer, + Input, + Label, + ListBox, + Select, + Spinner, + TextField, +} from "@heroui/react"; +import { useDebouncedValue } from "@tanstack/react-pacer"; +import { useMutation, useQuery } from "@tanstack/react-query"; +import { toast } from "sonner"; + +import { MediaStatus } from "@chia/db/types"; +import type { MediaKind } from "@chia/db/types"; +import { isEnumValue } from "@chia/utils/is"; +import { Image } from "@chiastack/ui/image"; + +import { DrawerPanel } from "@/components/commons/drawer-panel"; +import { orpc } from "@/libs/orpc/client"; + +import { KIND_LABEL, STATUS_OPTIONS } from "./form"; +import type { MediaCandidate, MediaEntryView } from "./form"; + +const SEARCH_DEBOUNCE_MS = 400; +const MIN_QUERY_CHARS = 2; + +const SOURCE_LABEL = { + "tmdb:movie": "TMDB", + "tmdb:tv": "TMDB", + anilist: "AniList", + igdb: "IGDB", +} satisfies Record; + +/** + * Searches the catalogues of `kind` and logs the picked work. The catalogue supplies the titles + * and cover, so only the status is asked here; `onAdded` opens the new entry for the rest. + */ +export const AddDrawer = ({ + isOpen, + kind, + onAdded, + onClose, +}: { + isOpen: boolean; + kind: MediaKind; + onAdded: (entry: MediaEntryView) => void; + onClose: () => void; +}) => { + const [query, setQuery] = useState(""); + const [status, setStatus] = useState(MediaStatus.Completed); + const [debouncedQuery] = useDebouncedValue(query.trim(), { + wait: SEARCH_DEBOUNCE_MS, + }); + const canSearch = debouncedQuery.length >= MIN_QUERY_CHARS; + + const search = useQuery( + orpc.media.search.queryOptions({ + input: { kind, query: debouncedQuery }, + enabled: isOpen && canSearch, + retry: false, + }) + ); + + const create = useMutation( + orpc.media.create.mutationOptions({ + onSuccess({ entry }) { + setQuery(""); + onAdded(entry); + toast.success("Logged; add your rating and dates"); + }, + onError(error) { + toast.error(error.message); + }, + }) + ); + + const candidates = canSearch ? (search.data?.items ?? []) : []; + + return ( + !open && onClose()}> + + + + {`Log ${KIND_LABEL[kind].toLowerCase()}`} + + +
+ + + + + +
+ + {search.isError ? ( +

+ The catalogue did not answer: {search.error.message} +

+ ) : null} + {canSearch && search.isFetching ? ( +
+ +
+ ) : null} + {canSearch && search.isSuccess && candidates.length === 0 ? ( +

+ Nothing matches “{debouncedQuery}”. Try the original title. +

+ ) : null} + +
    + {candidates.map((candidate) => ( +
  • +
    + {candidate.posterUrl ? ( + + + + ) : null} +
    +
    + + {candidate.title} + + + {[ + candidate.releaseYear, + candidate.originalTitle === candidate.title + ? null + : candidate.originalTitle, + SOURCE_LABEL[candidate.source], + ] + .filter((part) => part !== null) + .join(" · ")} + +
    + +
  • + ))} +
+
+
+
+ ); +}; diff --git a/apps/dash/src/components/media/entry-form.tsx b/apps/dash/src/components/media/entry-form.tsx new file mode 100644 index 000000000..9d29cc158 --- /dev/null +++ b/apps/dash/src/components/media/entry-form.tsx @@ -0,0 +1,374 @@ +"use client"; + +import { + Button, + Calendar, + DateField, + DatePicker, + Description, + FieldError, + Form, + Input, + Label, + ListBox, + NumberField, + Select, + Switch, + Tabs, + TextArea, + TextField, +} from "@heroui/react"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { parseDate } from "@internationalized/date"; +import { Controller, useForm } from "react-hook-form"; +import type { Control } from "react-hook-form"; + +import { Locale, MediaStatus } from "@chia/db/types"; +import { + MEDIA_COMMENT_MAX_CHARS, + MEDIA_RATING_MAX, +} from "@chia/db/validator/media"; +import { isEnumValue } from "@chia/utils/is"; + +import { LOCALES, STATUS_OPTIONS, formValuesOf, mediaFormSchema } from "./form"; +import type { + MediaEntryUpdate, + MediaEntryView, + MediaFormInput, + MediaFormOutput, +} from "./form"; + +const LOCALE_LABEL = { + [Locale.ZhTW]: "中文", + [Locale.En]: "English", +} satisfies Record; + +type FormControl = Control; + +/** Fields sit on the drawer's overlay surface; `secondary` is the variant that stays visible there in dark mode. */ +const FIELD_VARIANT = "secondary"; + +const DateInput = ({ + control, + description, + isDisabled, + label, + name, +}: { + control: FormControl; + description: string; + isDisabled: boolean; + label: string; + name: "startedOn" | "finishedOn"; +}) => ( + ( + field.onChange(date?.toString() ?? "")} + value={field.value === "" ? null : parseDate(field.value)}> + + + + {(segment) => } + + + + + + + + {description} + {fieldState.error?.message} + + + + + + + + + + + + + {(day) => {day}} + + + {(date) => } + + + + + {({ year }) => ( + + )} + + + + + + )} + /> +); + +/** An emptied number field reports `NaN`; the stored value for "not set" is `null`. */ +const NullableNumberInput = ({ + control, + description, + isDisabled, + label, + maxValue, + minValue, + name, +}: { + control: FormControl; + description: string; + isDisabled: boolean; + label: string; + maxValue: number; + minValue: number; + name: "rating" | "releaseYear"; +}) => ( + ( + field.onChange(Number.isNaN(next) ? null : next)} + step={1} + value={field.value ?? Number.NaN} + variant={FIELD_VARIANT}> + + + + + + + {description} + {fieldState.error?.message} + + )} + /> +); + +export const EntryForm = ({ + entry, + isPending, + onSubmit, +}: { + entry: MediaEntryView; + isPending: boolean; + onSubmit: (update: MediaEntryUpdate) => void; +}) => { + const { control, formState, handleSubmit } = useForm< + MediaFormInput, + unknown, + MediaFormOutput + >({ + resolver: zodResolver(mediaFormSchema), + defaultValues: formValuesOf(entry), + }); + + const submit = handleSubmit((update) => onSubmit(update)); + const translationsError = formState.errors.translations?.message; + + return ( +
+
+ ( + + )} + /> + + ( + + +
+ + + Shown with a sticker and on the favorites shelf. + +
+ + + +
+
+ )} + /> +
+ +
+ + +
+ + + + + {LOCALES.map((locale) => ( + + {LOCALE_LABEL[locale]} + + + ))} + + + {LOCALES.map((locale) => ( + + ( + + + + + Leave blank to show the other locale's title. + + {fieldState.error?.message} + + )} + /> + ( + + +