refactor: make the public read port async so a network adapter can implement it
`PublicContentQueries` returned arrays, not promises. That signature is only implementable by something already in memory, so the port could hold exactly one adapter — the bundled fixture — and no amount of configuration could put the public site on the backend. Turning it async is the change that makes a second adapter possible; the adapter itself follows. The markup is untouched. Every page reads a value and hands it to a presentational component, so the shape those components receive is mapped at the adapter boundary and nothing below the page changes. Screens load through one query, not one per read. Several pages read in a loop — the home timeline walks every project for its activity, the explore filter walks search results to resolve titles — and a hook per read would mean a variable number of hooks per render, which React forbids. `usePublicContent` takes the whole screen's reads as one loader, where a loop is a loop and `Promise.all` is available; the loops that used to be N sequential lookups now issue together. Two places deliberately do not show the loading surface. The explore filter sits inside a page that already renders one, so a second skeleton would move the layout under it — it keeps its structure and fills its options in when they arrive. The search dialog is a type-ahead: re-querying per keystroke would replace the results with a skeleton on every key, so it loads the catalog once and applies the same predicate locally. `usePublicContent` requires an object because `undefined` is how the query layer says "no result yet". A loader returning the record itself would make a missing slug indistinguishable from a request in flight, and the page would sit on a skeleton instead of rendering its not-found route. Studio's `resolvePublishedLabel` stays synchronous. It is called from inside the public renderer, so making it async would push awaits through the render tree; the shell loads the catalog once and the callback remains a lookup. The component tests now assemble the query providers the running app assembles. Without them the render throws "No QueryClient set" — not a harness quirk, but the same failure the app would produce if it were mounted without its query layer.
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { Link, useNavigate } from "react-router-dom";
|
||||
|
||||
import type { RecordKind } from "../../../application/ports/public-content-queries.ts";
|
||||
import { TECH_LOG_FEATURE_ID } from "../../../application/tech-log-feature-input.ts";
|
||||
import { useApplication } from "../../../../../presentation/providers/application-provider.tsx";
|
||||
import { usePublicContent } from "../use-public-content.tsx";
|
||||
|
||||
export function ExploreFilterForm({
|
||||
action,
|
||||
@@ -18,20 +17,34 @@ export function ExploreFilterForm({
|
||||
showType?: boolean;
|
||||
}) {
|
||||
const navigate = useNavigate();
|
||||
const { publicContent } = useApplication().features.get(TECH_LOG_FEATURE_ID);
|
||||
const publicRecords = publicContent.listRecords();
|
||||
const topics = [...new Set(publicRecords.map((record) => record.topic))].sort();
|
||||
const projectPrefix = "/projects/";
|
||||
const projects = publicContent
|
||||
.searchPublicContent("")
|
||||
.filter((entity) => entity.contentType === "PROJECT")
|
||||
.flatMap((entity) => {
|
||||
if (!entity.path.startsWith(projectPrefix)) return [];
|
||||
const item = publicContent.getProject(
|
||||
decodeURIComponent(entity.path.slice(projectPrefix.length)),
|
||||
// This form sits inside a page that renders its own loading state, so it does
|
||||
// not hand back a fallback of its own — that would put a second skeleton
|
||||
// inside a screen already showing one, and move the layout under it. It
|
||||
// renders its real structure immediately with empty option lists and fills
|
||||
// them in when the catalog arrives.
|
||||
const view = usePublicContent(["tech-log", "explore-filters"], async (queries) => {
|
||||
const projectPrefix = "/projects/";
|
||||
const [records, entities] = await Promise.all([
|
||||
queries.listRecords(),
|
||||
queries.searchPublicContent(""),
|
||||
]);
|
||||
const projectSlugs = entities
|
||||
.filter((entity) => entity.contentType === "PROJECT")
|
||||
.flatMap((entity) =>
|
||||
entity.path.startsWith(projectPrefix)
|
||||
? [decodeURIComponent(entity.path.slice(projectPrefix.length))]
|
||||
: [],
|
||||
);
|
||||
return item ? [{ slug: item.slug, title: item.title }] : [];
|
||||
});
|
||||
const resolved = await Promise.all(projectSlugs.map((slug) => queries.getProject(slug)));
|
||||
return {
|
||||
topics: [...new Set(records.map((record) => record.topic))].sort(),
|
||||
projects: resolved
|
||||
.filter((item) => item !== undefined)
|
||||
.map((item) => ({ slug: item.slug, title: item.title })),
|
||||
};
|
||||
});
|
||||
const topics = view.data?.topics ?? [];
|
||||
const projects = view.data?.projects ?? [];
|
||||
const normalizedTopic = topic?.toLocaleLowerCase("ko-KR");
|
||||
const selectedTopic = topics.find(
|
||||
(item) => item.toLocaleLowerCase("ko-KR") === normalizedTopic,
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { useId, useRef, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import { TECH_LOG_FEATURE_ID } from "../../../application/tech-log-feature-input.ts";
|
||||
import { useApplication } from "../../../../../presentation/providers/application-provider.tsx";
|
||||
import { usePublicContent } from "../use-public-content.tsx";
|
||||
|
||||
type SearchDialogProps = {
|
||||
className?: string;
|
||||
@@ -19,8 +18,21 @@ export function SearchDialog({
|
||||
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||
const [query, setQuery] = useState("");
|
||||
const normalizedQuery = query.trim().toLocaleLowerCase("ko-KR");
|
||||
const { publicContent } = useApplication().features.get(TECH_LOG_FEATURE_ID);
|
||||
const results = publicContent.searchPublicContent(normalizedQuery);
|
||||
// Keyed on the empty query, then filtered here, rather than one request per
|
||||
// keystroke. This is a type-ahead: re-querying per character would replace the
|
||||
// result list with a loading skeleton on every key, which is a worse dialog
|
||||
// than a stale-free local filter. The predicate is the same one the catalog
|
||||
// applies for a non-empty query, so the visible result set is unchanged.
|
||||
const view = usePublicContent(["tech-log", "search", ""], async (queries) => ({
|
||||
entities: await queries.searchPublicContent(""),
|
||||
}));
|
||||
const results = (view.data?.entities ?? []).filter((entity) =>
|
||||
normalizedQuery
|
||||
? [entity.title, entity.summary, entity.topic, entity.project, ...(entity.topics ?? [])]
|
||||
.filter((value): value is string => Boolean(value))
|
||||
.some((value) => value.toLocaleLowerCase("ko-KR").includes(normalizedQuery))
|
||||
: true,
|
||||
);
|
||||
|
||||
function open() {
|
||||
onBeforeOpen?.();
|
||||
|
||||
Reference in New Issue
Block a user