All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 51s
Search: GET /api/search sent each matching user's full row — password hash and email included — to anyone, signed in or not, because searchUsersGlobal returned User[] and the route passed it through. It returns profiles only now, stripped where the rows are read, and the search test fails on the old code. Types alone wouldn't have caught it (a User is structurally a PublicUser), so the strip is explicit and tested.
Typed routes. Every JSON response is declared as APIResponse<T>, the way the typed routes already did, with the missing shapes named in api/model/interfaces.ts: PublicUser (no hash, no email) and SelfUser (your own profile, with email) plus toPublicUser/toSelfUser in place of ten hand-written destructurings, AuthTokens, AuthResponse, MediaTokenResponse, InviteResponse, AttachmentResponse, SearchResults, LinkPreview, UserSuggestion and Invitee. Request bodies are `unknown` and narrowed by the existing guards, or by a new fieldOf(obj, key) where a route reads one field — no more reading off Oak's `any` or casting to Record<string, unknown>. The password-reset routes answer `{ success: true, data: null }` rather than a bare `{ success: true }`, so every success fits the envelope; the frontend only reads `success`.
Socket protocol: routes/ws.ts had its own send() taking Record<string, unknown>, so nothing checked what it sent. It takes ServerToClientMessage now, which turned up two mismatches: `pong` was missing from the union although the server answers every client ping with one, and vote_ack carried a `success: true` no type declares and no client reads. The union has PongMessage and vote_ack lost the field.
No explicit any. The YouTube playlist parse reads ytInitialData as `unknown` through a small at(node, …path) that checks each step, instead of `type Json = any`. The backend test helpers type the response body as the server's envelope: request<T>() and get/post/put/patch/del<T>() name what `data` holds, res.data throws with the server's message when the call didn't succeed and res.error when it didn't fail, and each test says which API type it expects, so a test reading a field the route doesn't send no longer compiles. Assertions that a field is absent are `assertFalse("passwordHash" in …)` rather than reading a field the type says isn't there. The socket tests' messages are ServerToClientMessage, with next()/all() keyed by type so each returns that message's shape.
No double casts in src/ either. window.YT and onYouTubeIframeAPIReady are declared on Window; the test's fake player is a class forwarding to the spies the tests inspect. WSContext, FollowContext and PlayerContext export their defaults (defaultWSValue, defaultFollowValue, defaultPlayerValue), which the tests import instead of reading React's private _currentValue. handleMentionKeyDown takes a MentionKey — the key fields it reads — so the test builds a plain object; useUserDumpFeed declares sentinelRef as the callback ref it is. In src/test/setup.ts the IntersectionObserver fake implements IntersectionObserver, and report(isIntersecting, top?) builds complete entries in place of partial ones cast in five files; the Animation fake implements Animation; the canvas getContext stub goes in through defineProperty. The XHR fake's last request is optional behind a getter that fails if none was made, the keyframes are checked with matchers, and the roleless legacy session is built by leaving the role out. One test changes meaning slightly: "leaving the view…" relied on entries with no position at all, and now leaves and re-enters at the same height, which is what it describes. The one cast left is in api/test/ws.test.ts: deno.json's "dom" lib hides Deno's WebSocket overload that takes handshake headers, and dropping that lib would break the editor's view of src/; it's commented and names the real signature.
Also: the notifications test answered a Response-typed promise with a plain object, which failed `tsc -b`; it uses the fetch stub's deferred() now. ESLint didn't read the `deno-lint-ignore no-explicit-any` comments, so `eslint .` failed on three files; those anys are gone. `deno fmt` over api/ and src/ (not the compiled src/locales/*.js); the reflow left all 332 message ids intact, and the .po files only have their source line references updated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
220 lines
7.1 KiB
TypeScript
220 lines
7.1 KiB
TypeScript
import { lazy, type ReactNode, Suspense, useRef, useState } from "react";
|
|
import { Link, useLocation, useNavigate, useSearchParams } from "react-router";
|
|
import { t } from "@lingui/core/macro";
|
|
import { Trans } from "@lingui/react/macro";
|
|
import { useAuth } from "../hooks/useAuth.ts";
|
|
import { useWS } from "../hooks/useWS.ts";
|
|
import { NotificationBell } from "./NotificationBell.tsx";
|
|
import { ChatButton } from "./ChatButton.tsx";
|
|
import { UserMenu } from "./UserMenu.tsx";
|
|
import { SearchBar } from "./SearchBar.tsx";
|
|
import { SITE_EMOJI, SITE_NAME } from "../hooks/useDocumentTitle.ts";
|
|
|
|
const DumpCreateModal = lazy(() =>
|
|
import("./DumpCreateModal.tsx").then((m) => ({ default: m.DumpCreateModal }))
|
|
);
|
|
|
|
/** Carried in a search entry's history state: the history index of the page
|
|
* the search started from, so leaving search returns there in one step
|
|
* however many searches (or result tabs) came in between. */
|
|
interface SearchEntryState {
|
|
searchExit?: number;
|
|
}
|
|
|
|
/** React Router's index of the current history entry — 0 for the first page
|
|
* of the session, which has nothing in the app to go back to. */
|
|
function historyIndex(): number {
|
|
return (window.history.state as { idx?: number } | null)?.idx ?? 0;
|
|
}
|
|
|
|
export function AppHeader(
|
|
{ centerSlot, disableNew, initialDumpUrl }: {
|
|
centerSlot?: ReactNode;
|
|
disableNew?: boolean;
|
|
initialDumpUrl?: string;
|
|
},
|
|
) {
|
|
const { user } = useAuth();
|
|
const { wsStatus, wsErrorMessage } = useWS();
|
|
const navigate = useNavigate();
|
|
const location = useLocation();
|
|
const [searchParams] = useSearchParams();
|
|
// The results page and each of its tabs (/search/~/users…).
|
|
const isSearchPage = location.pathname === "/search" ||
|
|
location.pathname.startsWith("/search/");
|
|
const query = isSearchPage ? searchParams.get("q") ?? "" : "";
|
|
|
|
// On the results page the search stays open, holding the query it shows.
|
|
const [searchOpen, setSearchOpen] = useState(false);
|
|
const searchExpanded = isSearchPage || searchOpen;
|
|
const [draft, setDraft] = useState(query);
|
|
const [prevQuery, setPrevQuery] = useState(query);
|
|
if (prevQuery !== query) {
|
|
setPrevQuery(query);
|
|
setDraft(query);
|
|
}
|
|
|
|
const [createModalOpen, setCreateModalOpen] = useState(!!initialDumpUrl);
|
|
const searchToggleRef = useRef<HTMLButtonElement>(null);
|
|
|
|
function submitSearch(q: string) {
|
|
const here = location.state as SearchEntryState | null;
|
|
// A search from elsewhere remembers that page; one from the results page
|
|
// passes on what it was given, or — for results reached by a link —
|
|
// the page before them. Results that opened the session have nowhere to
|
|
// return to, so a new search just replaces them.
|
|
const searchExit = !isSearchPage
|
|
? historyIndex()
|
|
: here?.searchExit ?? (historyIndex() > 0 ? historyIndex() - 1 : null);
|
|
const to = `/search?q=${encodeURIComponent(q)}`;
|
|
if (searchExit === null) navigate(to, { replace: true });
|
|
else navigate(to, { state: { searchExit } satisfies SearchEntryState });
|
|
}
|
|
|
|
function closeSearch() {
|
|
if (isSearchPage) {
|
|
const exit = (location.state as SearchEntryState | null)?.searchExit;
|
|
if (exit !== undefined) navigate(exit - historyIndex());
|
|
else if (historyIndex() > 0) navigate(-1);
|
|
else navigate("/");
|
|
} else {
|
|
setSearchOpen(false);
|
|
setDraft("");
|
|
searchToggleRef.current?.focus();
|
|
}
|
|
}
|
|
|
|
// Escape leaves an opened search, but only reverts an edited query on the
|
|
// results page — leaving that page is the ✕'s job.
|
|
function escapeSearch() {
|
|
if (isSearchPage) setDraft(query);
|
|
else closeSearch();
|
|
}
|
|
|
|
function handleSearchToggle() {
|
|
if (searchExpanded) closeSearch();
|
|
else setSearchOpen(true);
|
|
}
|
|
|
|
const searchBarProps = {
|
|
expanded: searchExpanded,
|
|
value: draft,
|
|
onValueChange: setDraft,
|
|
onSubmit: submitSearch,
|
|
onCancel: escapeSearch,
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<header className="app-header app-header--has-center">
|
|
<Link to="/" className="app-header-brand">
|
|
{SITE_EMOJI}
|
|
<span className="app-header-brand-name">
|
|
{" "}
|
|
{SITE_NAME}
|
|
</span>
|
|
</Link>
|
|
|
|
<div className="app-header-center">
|
|
{centerSlot}
|
|
<SearchBar {...searchBarProps} />
|
|
</div>
|
|
|
|
<nav className="app-header-nav">
|
|
<button
|
|
ref={searchToggleRef}
|
|
type="button"
|
|
className="nav-search-btn"
|
|
aria-label={searchExpanded ? t`Cancel search` : t`Search`}
|
|
aria-expanded={searchExpanded}
|
|
onClick={handleSearchToggle}
|
|
>
|
|
{searchExpanded ? "✕" : "🔍"}
|
|
</button>
|
|
{user
|
|
? (
|
|
<>
|
|
{/* Full text links — hidden below the compact breakpoint */}
|
|
<div className="nav-links">
|
|
<Link
|
|
to={`/users/${user.username}`}
|
|
className="app-header-user"
|
|
>
|
|
{user.username}
|
|
</Link>
|
|
<Link
|
|
to={`/users/${user.username}/playlists`}
|
|
className="app-header-user"
|
|
>
|
|
<Trans>Playlists</Trans>
|
|
</Link>
|
|
</div>
|
|
|
|
{/* Compact avatar menu — hidden above the compact breakpoint */}
|
|
<div className="nav-compact">
|
|
<UserMenu user={user} />
|
|
</div>
|
|
|
|
<ChatButton />
|
|
<NotificationBell />
|
|
<button
|
|
type="button"
|
|
className="btn-primary"
|
|
onClick={() => setCreateModalOpen(true)}
|
|
disabled={disableNew}
|
|
title={disableNew ? t`Server unreachable` : undefined}
|
|
>
|
|
+<span className="btn-new-label">
|
|
<Trans>Dump</Trans>
|
|
</span>
|
|
</button>
|
|
</>
|
|
)
|
|
: (
|
|
<>
|
|
<button type="button" onClick={() => navigate("/login")}>
|
|
<Trans>Log in</Trans>
|
|
</button>
|
|
</>
|
|
)}
|
|
</nav>
|
|
</header>
|
|
|
|
{
|
|
/* Stays mounted so the row can slide open and shut; `inert` keeps the
|
|
collapsed input out of the tab order. */
|
|
}
|
|
<div
|
|
className={`header-search-reveal${
|
|
searchExpanded ? " header-search-reveal--open" : ""
|
|
}`}
|
|
inert={!searchExpanded}
|
|
>
|
|
<div className="header-search-reveal-clip">
|
|
<div className="header-search-below">
|
|
<SearchBar {...searchBarProps} />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{wsStatus === "disconnected" && wsErrorMessage && (
|
|
<div className="app-header-status" role="alert">
|
|
<strong>
|
|
<Trans>Live updates unavailable.</Trans>
|
|
</strong>{" "}
|
|
{wsErrorMessage}
|
|
</div>
|
|
)}
|
|
|
|
{createModalOpen && (
|
|
<Suspense>
|
|
<DumpCreateModal
|
|
onClose={() => setCreateModalOpen(false)}
|
|
initialUrl={initialDumpUrl}
|
|
/>
|
|
</Suspense>
|
|
)}
|
|
</>
|
|
);
|
|
}
|