Repository navigation
feat(web): live gallery — Eden Treaty + TanStack Query #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89dcc0a
feat(web): live gallery — Eden Treaty + TanStack Query
deckyfx 6016207
fix(web): address CodeRabbit review on PR #10
deckyfx 296fabb
fix(web): address second CodeRabbit pass on PR #10
deckyfx 1ec880b
fix(web): third CodeRabbit pass — AssetImage src reset + API_TARGET s…
deckyfx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { useEffect, useState, type CSSProperties } from "react"; | ||
|
|
||
| /** | ||
| * An asset image that degrades gracefully. A metadata row can outlive its stored | ||
| * bytes (the API answers `404` from `/assets/:id/file`), so on a load error we | ||
| * render an "unavailable" placeholder in the same box instead of the browser's | ||
| * broken-image glyph. Shared by the gallery grid and the detail view so both | ||
| * gate on actual file availability, not just the presence of a metadata row. | ||
| */ | ||
| export function AssetImage({ | ||
| src, | ||
| alt, | ||
| className, | ||
| style, | ||
| loading, | ||
| }: { | ||
| src: string; | ||
| alt: string; | ||
| className?: string; | ||
| style?: CSSProperties; | ||
| loading?: "lazy" | "eager"; | ||
| }) { | ||
| const [failed, setFailed] = useState(false); | ||
|
|
||
| // Reset on src change so a reused instance retries the new asset instead of | ||
| // staying stuck on the placeholder from a previous failure. | ||
| useEffect(() => { | ||
| setFailed(false); | ||
| }, [src]); | ||
|
|
||
| if (failed) { | ||
| return ( | ||
| <div className={className} style={style} role="img" aria-label={`${alt} (image unavailable)`}> | ||
| <span className="flex h-full w-full items-center justify-center text-[11px] text-muted"> | ||
| Image unavailable | ||
| </span> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <img | ||
| src={src} | ||
| alt={alt} | ||
| loading={loading} | ||
| className={className} | ||
| style={style} | ||
| onError={() => setFailed(true)} | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| import { StrictMode } from "react"; | ||
| import { createRoot } from "react-dom/client"; | ||
| import { QueryClientProvider } from "@tanstack/react-query"; | ||
| import { RouterProvider } from "@tanstack/react-router"; | ||
|
|
||
| import { queryClient } from "./lib/query"; | ||
| import { router } from "./router"; | ||
|
|
||
| const rootEl = document.getElementById("root"); | ||
| if (!rootEl) throw new Error("#root element not found"); | ||
|
|
||
| createRoot(rootEl).render( | ||
| <StrictMode> | ||
| <RouterProvider router={router} /> | ||
| <QueryClientProvider client={queryClient}> | ||
| <RouterProvider router={router} /> | ||
| </QueryClientProvider> | ||
| </StrictMode>, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { treaty } from "@elysiajs/eden"; | ||
|
|
||
| import type { App } from "@bunbooru/api"; | ||
|
|
||
| /** | ||
| * Type-safe API client (Eden Treaty) — the ONLY way the frontend talks to the | ||
| * API. Never call `fetch` directly: Eden derives request/response types from the | ||
| * server's {@link App} type, so a route rename or payload change is a compile | ||
| * error here, not a runtime 404. | ||
| * | ||
| * Base = `window.location.origin` (same-origin): in prod the API serves this SPA | ||
| * on one origin; in dev the web server proxies `/api/*` to the API. Either way | ||
| * there's no CORS. The SSR fallback origin is never actually used in this SPA. | ||
| */ | ||
| export const api = treaty<App>( | ||
| typeof window !== "undefined" ? window.location.origin : "http://localhost:3000", | ||
| ); | ||
|
|
||
| /** Public URL for an asset's stored bytes (used as an `<img src>`, not a data call). */ | ||
| export function assetFileUrl(id: number): string { | ||
| return `/api/v1/assets/${id}/file`; | ||
| } | ||
|
|
||
| /** | ||
| * Unwrap an Eden Treaty response: throw on transport/HTTP error, then narrow out | ||
| * the API's `{ error }` envelope. The global `onError` handler's return type is | ||
| * merged by Elysia into every route's response type, so `data` is typed as | ||
| * `Success | envelope` even though non-2xx bodies actually arrive on `error`. | ||
| */ | ||
| export function unwrap<T>(res: { data: T | null; error: unknown }): Exclude<T, { error: unknown }> { | ||
| if (res.error) throw res.error; | ||
| if (res.data == null) throw new Error("Unexpected API response"); | ||
| // Eden routes non-2xx bodies to `res.error`, so a non-null `data` is a success | ||
| // payload; the envelope is removed at the type level only (never at runtime, so | ||
| // a payload that legitimately carries an `error` field isn't misread as failed). | ||
| return res.data as Exclude<T, { error: unknown }>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { useQuery } from "@tanstack/react-query"; | ||
|
|
||
| import type { AssetDto } from "@bunbooru/api"; | ||
|
|
||
| import { api, unwrap } from "./api"; | ||
|
|
||
| /** Grid page size. */ | ||
| export const ASSETS_PER_PAGE = 42; | ||
|
|
||
| export type { AssetDto }; | ||
|
|
||
| /** | ||
| * Newest-first page of assets. Eden Treaty is the transport; TanStack Query owns | ||
| * caching + loading/error state. The `error` discriminant narrows `data` to | ||
| * non-null, so the hook resolves a fully-typed page. | ||
| */ | ||
| export function useAssetsPage(page: number) { | ||
| return useQuery({ | ||
| queryKey: ["assets", page], | ||
| enabled: Number.isInteger(page) && page > 0, | ||
| queryFn: async () => | ||
| unwrap(await api.api.v1.assets.get({ query: { page, per_page: ASSETS_PER_PAGE } })), | ||
| }); | ||
| } | ||
|
|
||
| /** A single asset's metadata (for the detail page / deep links). */ | ||
| export function useAsset(id: number) { | ||
| return useQuery({ | ||
| queryKey: ["asset", id], | ||
| enabled: Number.isInteger(id) && id > 0, | ||
| queryFn: async () => { | ||
| const res = await api.api.v1.assets({ id: String(id) }).get(); | ||
| // A 404 is "this asset doesn't exist" — a normal not-found state, not a | ||
| // query failure. Return null so the detail page renders NotFound instead | ||
| // of the generic error/retry UI; other errors still throw via unwrap. | ||
| if (res.status === 404) return null; | ||
| return unwrap(res); | ||
| }, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.