Files
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

54 lines
2.4 KiB
TypeScript

"use client";
import { useCallback, useEffect, useRef } from "react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
/**
* Replaces react-router-dom's `useSearchParams` for the ported feature pages
* (members, reports): filter/sort/pagination state lives in the URL so a
* filtered view stays shareable and survives reload (see each page's own
* comments). The tuple shape mirrors react-router's closely enough that
* porting a page off it only needed this hook swapped in, plus dropping the
* `{ replace: true }` second argument react-router's setter took (this
* always replaces, matching how every call site here used it).
*
* The setter builds on the last query string this hook wrote, not the params
* of the last render: `router.replace` lands later, so two updates before the
* next render (or a debounced callback holding an old setter) would otherwise
* start from stale params and drop the first change. The pending write is
* shared (module level) because every instance on a page writes the same URL.
* A rendered URL this hook never sent is an outside navigation, which wins.
*/
let pending: { path: string; qs: string; sent: Set<string> } | null = null;
export function useQueryState(): [
URLSearchParams,
(updater: (prev: URLSearchParams) => URLSearchParams) => void,
] {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
const rendered = searchParams.toString();
const latest = useRef(rendered);
useEffect(() => {
latest.current = rendered;
if (pending && (pending.path !== pathname || pending.qs === rendered || !pending.sent.has(rendered))) pending = null;
}, [pathname, rendered]);
const setSearchParams = useCallback(
(updater: (prev: URLSearchParams) => URLSearchParams) => {
const base = pending?.path === pathname ? pending.qs : latest.current;
const qs = updater(new URLSearchParams(base)).toString();
// Only clear when the write matches what is rendered (nothing left waiting to render); a no-op relative to a pending base must keep that pending change.
const sent = new Set(pending?.path === pathname ? pending.sent : []).add(latest.current).add(qs);
pending = qs === latest.current ? null : { path: pathname, qs, sent };
router.replace(qs ? `${pathname}?${qs}` : pathname, { scroll: false });
},
[router, pathname],
);
return [searchParams, setSearchParams];
}