For 3k+ row deployments, returning the full table in one shot is the
bottleneck — the JSON payload alone is hundreds of KB and the client
mounts thousands of EditableCell instances on every visit. Pagination
with auto-fetch on scroll shrinks both the wire payload and the initial
render to a single page (200 rows).
Server (app/cm_api.py):
- /acc/ and /user/ accept ?limit, ?offset, ?prefix, ?dir, plus ?sort on
/user/ (f_username | last_update_time). Defaults: limit=200 (capped at
1000), offset=0, dir=desc.
- ORDER BY done in SQL with prefix-priority: rows whose username starts
with the configured CM_PREFIX_PATTERN come first, then asc/desc by the
sort column. The 'dir' value is whitelisted to ASC|DESC before string
interpolation; everything else goes through parameterised binding.
- Schema verification (verify_tables_once) deferred to first request via
a Flask before_request hook — keeps create_app() free of MySQL touches
so unit tests + gunicorn preload still work without a live DB.
Web client:
- web/lib/api.ts: getAccountsPage / getUsersPage return { rows, hasMore }.
hasMore = (rows.length === PAGE_SIZE), so the client knows when to
stop fetching. Each page is its own Next.js cache entry (the URL is
the cache key) — caching from the previous commit still applies.
- web/app/actions.ts: loadMoreAccounts / loadMoreUsers Server Actions
for next-page requests; refreshAccounts / refreshUsers force-evict the
cache via revalidateTag before refetching page 1.
- web/app/page.tsx + users/page.tsx: only fetch the first page now.
- web/components/{accounts,users}-table.tsx: rewrote state model. Rows
accumulate as the user scrolls. An IntersectionObserver on a sentinel
div near the bottom triggers loadMore when it enters the viewport
(300px rootMargin so the next page starts loading before the user
reaches the end). useOptimistic wraps the accumulated rows for in-
flight edits; on success the row is committed locally so the change
survives even though we no longer router.refresh.
- Sort toggle now refetches from page 1 with the new dir/sort param.
Local sort over a partial set would be inconsistent.
- Mutations: delete filters from local state; create + refresh both
reset to page 1 so the row appears in its sorted position.
- Header count shows '<loaded>+' when more pages exist so the operator
knows what they're seeing isn't the full table.
Removed AutoRefresh:
- web/app/layout.tsx no longer mounts AutoRefresh.
- web/components/auto-refresh.tsx deleted.
- Reason: router.refresh every 30s would yank the user back to page 1
every time, losing scroll position and accumulated rows. Manual
Refresh button replaces it (now wired to refreshAccounts/refreshUsers
which evict cache + refetch).
Tests: deferred verify_tables_once() means tests.test_bot_cli's
CreateAppFactoryTests pass without DB env vars again. All 38 existing
tests pass.
110 lines
3.3 KiB
TypeScript
110 lines
3.3 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath, revalidateTag } from "next/cache";
|
|
import {
|
|
ACCOUNTS_TAG,
|
|
USERS_TAG,
|
|
fetchApi,
|
|
getAccountsPage,
|
|
getUsersPage,
|
|
type AccountsPageOpts,
|
|
type Page,
|
|
type UsersPageOpts,
|
|
} from "@/lib/api";
|
|
import type { Acc, AccUpdate, User, UserUpdate } from "@/lib/types";
|
|
|
|
export type ActionResult = { ok: true } | { ok: false; error: string };
|
|
|
|
// Each mutation evicts the matching tag so the next GET bypasses the
|
|
// 30s data cache and re-reads from MySQL.
|
|
|
|
export async function updateAccount(data: AccUpdate): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/update-acc-data", { method: "POST", body: data });
|
|
revalidateTag(ACCOUNTS_TAG);
|
|
revalidatePath("/");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function updateUser(data: UserUpdate): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/update-user-data", { method: "POST", body: data });
|
|
revalidateTag(USERS_TAG);
|
|
revalidatePath("/users");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function createAccount(data: AccUpdate): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/create-acc-data", { method: "POST", body: data });
|
|
revalidateTag(ACCOUNTS_TAG);
|
|
revalidatePath("/");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function createUser(data: UserUpdate): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/create-user-data", { method: "POST", body: data });
|
|
revalidateTag(USERS_TAG);
|
|
revalidatePath("/users");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function deleteAccount(username: string): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/delete-acc-data", { method: "POST", body: { username } });
|
|
revalidateTag(ACCOUNTS_TAG);
|
|
revalidatePath("/");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function deleteUser(f_username: string): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/delete-user-data", { method: "POST", body: { f_username } });
|
|
revalidateTag(USERS_TAG);
|
|
revalidatePath("/users");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
// ---- Pagination + force-refresh ----
|
|
|
|
export async function loadMoreAccounts(opts: AccountsPageOpts): Promise<Page<Acc>> {
|
|
return getAccountsPage(opts);
|
|
}
|
|
|
|
export async function loadMoreUsers(opts: UsersPageOpts): Promise<Page<User>> {
|
|
return getUsersPage(opts);
|
|
}
|
|
|
|
// Force-refresh evicts the cached tag before refetching the first page,
|
|
// so manual Refresh always returns DB-fresh data even if the cache is
|
|
// still warm.
|
|
|
|
export async function refreshAccounts(opts: AccountsPageOpts = {}): Promise<Page<Acc>> {
|
|
revalidateTag(ACCOUNTS_TAG);
|
|
return getAccountsPage({ ...opts, offset: 0 });
|
|
}
|
|
|
|
export async function refreshUsers(opts: UsersPageOpts = {}): Promise<Page<User>> {
|
|
revalidateTag(USERS_TAG);
|
|
return getUsersPage({ ...opts, offset: 0 });
|
|
}
|