/* ============================================================================= * B06_Section_Api_Context.ts * 종횡단 설정값(`GET …/sections/context`)의 **세션 캐시 창구** — 화면 쪽. * * `B06_Section_Api_Fetch.ts` 에서 갈라냄(472-2) — 서버 Node 묶음이 Api_Fetch 를 끌어도 * 세션(`b_page_state`) · 되돌리기 사진첩(`Profile_History`)을 함께 끌지 않게. * ========================================================================== */ import { clearState, readState, stateKey, writeState } from "../A00_Common/b_page_state"; import { seedPageHistory } from "../B05_Profile/B05_Profile_UI_Profile_History"; import { requestSectionContext } from "./B06_Section_Api_Fetch"; import type { SectionContextResponse } from "./B06_Section_Api_Types"; /** 최신 확정 경로와 B04 확정값, config 기반 생성 기본값을 조회한다. */ /** 종횡단 설정값 — ④ 계산 결과라 세션에 담아 두고 화면을 오갈 때마다 다시 묻지 않는다 * (2026-09-06 호출 정리). 노선이 바뀌면 `clearSectionContextCache` 로 버린다. */ export async function fetchSectionContext(projectId: string): Promise { const cached = readState("section-context", projectId); if (cached) return seedStandardCross(projectId, cached); const fresh = await requestSectionContext(projectId); writeState("section-context", fresh, projectId); return seedStandardCross(projectId, fresh); } /** 서버가 지금 보는 노선 id — 세션 캐시를 건너뛰고 묻는다. [저장] · [확정] 앞에서 화면이 쥔 * 노선과 대조한다(PLAN 76-1). 받은 설정값으로 캐시도 새로 둔다. */ export async function fetchCurrentRouteId(projectId: string): Promise { const fresh = await requestSectionContext(projectId); writeState("section-context", fresh, projectId); return fresh.route_id; } /** * 저장된 표준 횡단면을 **세션이 비어 있을 때만** 채운다(2026-09-07). * * 브라우저 횡단 계산은 `세션 ?? config 기본값` 으로 서는데, 표준단면 세션값은 그 탭에서만 * 산다. 그래서 **탭을 새로 열면** 화면은 config 기본값으로, 서버는 저장분으로 계산해 * 같은 측점이 갈렸다(실측 — 용화 route 169 저장분은 암반 횡단경사 **5%**·측구 상단폭 * **0.9m**, config 기본값은 **3%**·**0.69m**). * * 여기가 두 화면(B05·B06)이 함께 지나는 유일한 자리라 이 한 곳에서 채운다. 사용자가 * 그 탭에서 고친 값이 있으면 **건드리지 않는다** — 초안이 언제나 우선이다. */ function seedStandardCross( projectId: string, context: SectionContextResponse, ): SectionContextResponse { const stored = context.stored_standard_cross_section; if (stored && readState("std-cross", projectId) === null) { writeState("std-cross", stored, projectId); seedPageHistory(stateKey("std-cross", projectId)); // 씨앗 — 되돌리기 걸음 아님(364-1) } return context; } export function clearSectionContextCache(projectId: string): void { clearState("section-context", projectId); }