feat(map): add universal subject and station search
This commit is contained in:
@@ -10,6 +10,16 @@ type ReferenceSnapshot = {
|
||||
schemaVersion: "nodedc.map-reference.snapshot/v1";
|
||||
profileId: typeof TRANSPORT_STATION_REFERENCE_PROFILE_ID;
|
||||
sourceRevision: string;
|
||||
complete: boolean;
|
||||
facts: MapRuntimeFact[];
|
||||
};
|
||||
|
||||
type ReferenceSearch = {
|
||||
schemaVersion: "nodedc.map-reference.search/v1";
|
||||
profileId: typeof TRANSPORT_STATION_REFERENCE_PROFILE_ID;
|
||||
sourceRevision: string;
|
||||
complete: boolean;
|
||||
query: string;
|
||||
facts: MapRuntimeFact[];
|
||||
};
|
||||
|
||||
@@ -22,6 +32,8 @@ export function useMapReferenceRuntime(
|
||||
enabled = true,
|
||||
) {
|
||||
const [snapshot, setSnapshot] = useState<ReferenceSnapshot | null>(null);
|
||||
const [requestState, setRequestState] = useState<MapRuntimeBinding["state"]>("idle");
|
||||
const [retryEpoch, setRetryEpoch] = useState(0);
|
||||
const signature = useMemo(() => layers.map((layer) => `${layer.id}:${layer.category}:${layer.presentationProfileId}`).join("|"), [layers]);
|
||||
const bbox = useMemo(() => referenceBbox(camera), [camera?.height, camera?.latitude, camera?.longitude]);
|
||||
const bboxSignature = bbox?.join(",") ?? "";
|
||||
@@ -29,11 +41,21 @@ export function useMapReferenceRuntime(
|
||||
useEffect(() => {
|
||||
if (!enabled || !layers.length) {
|
||||
setSnapshot(null);
|
||||
setRequestState("idle");
|
||||
return;
|
||||
}
|
||||
// Above the reference-layer LOD there is nothing useful to fetch. Keep
|
||||
// the last verified snapshot so a temporary zoom-out cannot blank a
|
||||
// viewport that was already materialised.
|
||||
if (!bbox) return;
|
||||
const controller = new AbortController();
|
||||
let retryTimer: number | undefined;
|
||||
const scheduleRetry = () => {
|
||||
retryTimer = window.setTimeout(() => setRetryEpoch((current) => current + 1), 6_000);
|
||||
};
|
||||
const debounce = window.setTimeout(() => {
|
||||
const query = bbox ? `?bbox=${encodeURIComponent(bbox.join(","))}` : "";
|
||||
setRequestState(snapshot ? "reconnecting" : "loading");
|
||||
const query = `?bbox=${encodeURIComponent(bbox.join(","))}`;
|
||||
void fetch(`/api/map-gateway/api/map/reference-sources/v1/profiles/${TRANSPORT_STATION_REFERENCE_PROFILE_ID}/current${query}`, {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
@@ -43,16 +65,25 @@ export function useMapReferenceRuntime(
|
||||
const parsed = asSnapshot(await response.json());
|
||||
if (!parsed) throw new Error("map_reference_snapshot_invalid");
|
||||
setSnapshot(parsed);
|
||||
setRequestState(parsed.complete ? "ready" : "reconnecting");
|
||||
if (!parsed.complete) scheduleRetry();
|
||||
})
|
||||
.catch(() => {
|
||||
if (!controller.signal.aborted) setSnapshot(null);
|
||||
// Network/Gateway interruptions retain the last good materialised
|
||||
// snapshot. No stale provider payload is introduced because the
|
||||
// snapshot has already passed the fail-closed normalizer below.
|
||||
if (!controller.signal.aborted) {
|
||||
setRequestState(snapshot ? "reconnecting" : "error");
|
||||
scheduleRetry();
|
||||
}
|
||||
});
|
||||
}, 450);
|
||||
return () => {
|
||||
window.clearTimeout(debounce);
|
||||
if (retryTimer !== undefined) window.clearTimeout(retryTimer);
|
||||
controller.abort();
|
||||
};
|
||||
}, [bboxSignature, enabled, signature]);
|
||||
}, [bboxSignature, enabled, retryEpoch, signature]);
|
||||
|
||||
return useMemo<MapRuntimeBinding[]>(() => layers.map((layer) => ({
|
||||
bindingId: layer.id,
|
||||
@@ -61,8 +92,71 @@ export function useMapReferenceRuntime(
|
||||
presentationProfileId: layer.presentationProfileId,
|
||||
facts: snapshot?.facts.filter((fact) => fact.attributes.category === layer.category) ?? [],
|
||||
cursor: snapshot?.sourceRevision ?? null,
|
||||
state: snapshot ? "ready" : "loading",
|
||||
})), [layers, snapshot]);
|
||||
state: requestState === "idle" ? (snapshot ? "ready" : "loading") : requestState,
|
||||
})), [layers, requestState, snapshot]);
|
||||
}
|
||||
|
||||
export function useMapReferenceSearch(
|
||||
layers: MapReferenceLayer[],
|
||||
query: string,
|
||||
requestEpoch: number,
|
||||
enabled = true,
|
||||
) {
|
||||
const [search, setSearch] = useState<ReferenceSearch | null>(null);
|
||||
const [requestState, setRequestState] = useState<"idle" | "loading" | "ready" | "error">("idle");
|
||||
const normalizedQuery = useMemo(() => query.normalize("NFKC").trim().replace(/\s+/gu, " "), [query]);
|
||||
const signature = useMemo(() => layers.map((layer) => `${layer.id}:${layer.category}:${layer.presentationProfileId}`).join("|"), [layers]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !layers.length || normalizedQuery.length < 2) {
|
||||
setSearch(null);
|
||||
setRequestState("idle");
|
||||
return;
|
||||
}
|
||||
const controller = new AbortController();
|
||||
setSearch(null);
|
||||
setRequestState("loading");
|
||||
const debounce = window.setTimeout(() => {
|
||||
const params = new URLSearchParams({ q: normalizedQuery, limit: "12" });
|
||||
void fetch(`/api/map-gateway/api/map/reference-sources/v1/profiles/${TRANSPORT_STATION_REFERENCE_PROFILE_ID}/search?${params}`, {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
})
|
||||
.then(async (response) => {
|
||||
if (!response.ok) throw new Error(`map_reference_search_http_${response.status}`);
|
||||
const parsed = asSearch(await response.json());
|
||||
if (!parsed || parsed.query.normalize("NFKC").trim().toLocaleLowerCase("ru")
|
||||
!== normalizedQuery.toLocaleLowerCase("ru")) {
|
||||
throw new Error("map_reference_search_invalid");
|
||||
}
|
||||
setSearch(parsed);
|
||||
setRequestState(parsed.complete ? "ready" : "error");
|
||||
})
|
||||
.catch(() => {
|
||||
if (!controller.signal.aborted) {
|
||||
setSearch(null);
|
||||
setRequestState("error");
|
||||
}
|
||||
});
|
||||
}, 300);
|
||||
return () => {
|
||||
window.clearTimeout(debounce);
|
||||
controller.abort();
|
||||
};
|
||||
}, [enabled, normalizedQuery, requestEpoch, signature]);
|
||||
|
||||
return useMemo(() => ({
|
||||
bindings: layers.map<MapRuntimeBinding>((layer) => ({
|
||||
bindingId: layer.id,
|
||||
dataProductId: `platform-reference.${layer.referenceProfileId}`,
|
||||
slotId: "reference-points",
|
||||
presentationProfileId: layer.presentationProfileId,
|
||||
facts: search?.facts.filter((fact) => fact.attributes.category === layer.category) ?? [],
|
||||
cursor: search?.sourceRevision ?? null,
|
||||
state: search ? "ready" : "idle",
|
||||
})),
|
||||
state: requestState,
|
||||
}), [layers, requestState, search]);
|
||||
}
|
||||
|
||||
function referenceBbox(camera: { longitude: number; latitude: number; height: number } | null) {
|
||||
@@ -82,12 +176,31 @@ function referenceBbox(camera: { longitude: number; latitude: number; height: nu
|
||||
function asSnapshot(value: unknown): ReferenceSnapshot | null {
|
||||
if (!isObject(value) || value.schemaVersion !== "nodedc.map-reference.snapshot/v1"
|
||||
|| value.profileId !== TRANSPORT_STATION_REFERENCE_PROFILE_ID
|
||||
|| typeof value.sourceRevision !== "string" || !Array.isArray(value.facts)) return null;
|
||||
|| typeof value.sourceRevision !== "string" || typeof value.complete !== "boolean"
|
||||
|| !Array.isArray(value.facts)) return null;
|
||||
const facts = value.facts.map(asFact).filter((fact): fact is MapRuntimeFact => Boolean(fact));
|
||||
return {
|
||||
schemaVersion: "nodedc.map-reference.snapshot/v1",
|
||||
profileId: TRANSPORT_STATION_REFERENCE_PROFILE_ID,
|
||||
sourceRevision: value.sourceRevision,
|
||||
complete: value.complete,
|
||||
facts,
|
||||
};
|
||||
}
|
||||
|
||||
function asSearch(value: unknown): ReferenceSearch | null {
|
||||
if (!isObject(value) || value.schemaVersion !== "nodedc.map-reference.search/v1"
|
||||
|| value.profileId !== TRANSPORT_STATION_REFERENCE_PROFILE_ID
|
||||
|| typeof value.sourceRevision !== "string" || typeof value.complete !== "boolean"
|
||||
|| typeof value.query !== "string" || value.query.length > 96
|
||||
|| !Array.isArray(value.facts)) return null;
|
||||
const facts = value.facts.map(asFact).filter((fact): fact is MapRuntimeFact => Boolean(fact));
|
||||
return {
|
||||
schemaVersion: "nodedc.map-reference.search/v1",
|
||||
profileId: TRANSPORT_STATION_REFERENCE_PROFILE_ID,
|
||||
sourceRevision: value.sourceRevision,
|
||||
complete: value.complete,
|
||||
query: value.query,
|
||||
facts,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user