Spaces:
Sleeping
Sleeping
signer: silent background LNA check; notify only when not allowed
Browse filesReplace the upfront "เปิดใช้งาน" card with a background probe that auto-fires on load (which
itself triggers the one-time Allow prompt). Granted -> seamless, zero UI. Only if the user did
NOT allow (Block / off-network) do we show a notification + recovery steps + Retry. App stays
gated until allowed. Fresh probe key per attempt so Retry actually re-fetches.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- streamlit_app.py +33 -41
streamlit_app.py
CHANGED
|
@@ -165,56 +165,48 @@ def fetch_signers_for_po(po):
|
|
| 165 |
|
| 166 |
|
| 167 |
def require_local_network():
|
| 168 |
-
"""
|
| 169 |
-
|
| 170 |
-
|
|
|
|
| 171 |
if st.session_state.get("lna_ok"):
|
| 172 |
return
|
| 173 |
if not streamlit_js_eval or not SMP_TOKEN:
|
| 174 |
st.error(":material/error: ระบบยังไม่พร้อมใช้งาน (ผู้ดูแลระบบยังไม่ได้ตั้งค่า SMP_TOKEN) — โปรดแจ้งผู้ดูแล")
|
| 175 |
st.stop()
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
ok = bool(json.loads(res).get("ok"))
|
| 201 |
-
except Exception: # noqa: BLE001
|
| 202 |
-
ok = False
|
| 203 |
-
if ok:
|
| 204 |
-
st.session_state["lna_ok"] = True
|
| 205 |
-
st.rerun()
|
| 206 |
-
elif ok is False:
|
| 207 |
-
st.error(":material/wifi_off: เชื่อมต่อระบบไม่ได้")
|
| 208 |
-
st.markdown(
|
| 209 |
-
"โปรดตรวจสอบ:\n"
|
| 210 |
-
"1. คุณอยู่ใน **เครือข่าย PTT** (หรือ VPN)\n"
|
| 211 |
-
"2. ถ้าเผลอกด **Block**: กดไอคอน **🔒** ที่แถบที่อยู่ → **Local network** → **Allow** → รีโหลด")
|
| 212 |
-
if st.button(":material/refresh: ลองใหม่", use_container_width=True):
|
| 213 |
st.rerun()
|
| 214 |
-
else:
|
| 215 |
-
st.info(":material/hourglass_top: กำลังตรวจสอบสิทธิ์… โปรดกด **Allow** ที่ป๊อปอัปของเบราว์เซอร์")
|
| 216 |
st.stop()
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
# =============================================================================
|
| 220 |
# Config: live data source — OneDrive "anyone with the link" (ตั้งใน Space secret DATA_URL)
|
|
|
|
| 165 |
|
| 166 |
|
| 167 |
def require_local_network():
|
| 168 |
+
"""Background reachability check: silently probe the SMP gateway on load — which auto-triggers
|
| 169 |
+
the one-time Local Network Access 'Allow' prompt. If granted → seamless, no UI. ONLY if the
|
| 170 |
+
user did NOT allow (blocked / off-network) do we surface a notification with recovery + retry.
|
| 171 |
+
App stays gated until allowed (no manual bypass, per product decision)."""
|
| 172 |
if st.session_state.get("lna_ok"):
|
| 173 |
return
|
| 174 |
if not streamlit_js_eval or not SMP_TOKEN:
|
| 175 |
st.error(":material/error: ระบบยังไม่พร้อมใช้งาน (ผู้ดูแลระบบยังไม่ได้ตั้งค่า SMP_TOKEN) — โปรดแจ้งผู้ดูแล")
|
| 176 |
st.stop()
|
| 177 |
|
| 178 |
+
# auto-fire the probe in the background (fresh key per attempt so 'ลองใหม่' re-fetches)
|
| 179 |
+
attempt = st.session_state.get("lna_attempt", 0)
|
| 180 |
+
res = streamlit_js_eval(js_expressions=signer_lookup.build_probe_js(SMP_TOKEN),
|
| 181 |
+
key=f"lna_probe_{attempt}", want_output=True)
|
| 182 |
+
ok = None
|
| 183 |
+
if isinstance(res, str) and res.strip().startswith("{"):
|
| 184 |
+
try:
|
| 185 |
+
ok = bool(json.loads(res).get("ok"))
|
| 186 |
+
except Exception: # noqa: BLE001
|
| 187 |
+
ok = False
|
| 188 |
+
|
| 189 |
+
if ok:
|
| 190 |
+
st.session_state["lna_ok"] = True
|
| 191 |
+
return # granted → seamless, no UI shown
|
| 192 |
+
|
| 193 |
+
if ok is False: # user did not allow / blocked / off-network
|
| 194 |
+
_, mid, _ = st.columns([1, 2, 1])
|
| 195 |
+
with mid:
|
| 196 |
+
st.warning(":material/lan: **กด “Allow / อนุญาต” ที่ป๊อปอัปของเบราว์เซอร์เพื่อใช้งานแอป**\n\n"
|
| 197 |
+
"ถ้าไม่มีป๊อปอัป หรือเคยกด Block: กดไอคอน 🔒 ที่แถบที่อยู่ → **Local network** → "
|
| 198 |
+
"**Allow** → รีโหลด — หรืออยู่ในเครือข่าย PTT/VPN แล้วกดปุ่มด้านล่าง")
|
| 199 |
+
if st.button(":material/refresh: ลองอีกครั้ง", use_container_width=True):
|
| 200 |
+
st.session_state["lna_attempt"] = attempt + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
st.rerun()
|
|
|
|
|
|
|
| 202 |
st.stop()
|
| 203 |
|
| 204 |
+
# pending (the Allow prompt is open) → wait quietly
|
| 205 |
+
_, mid, _ = st.columns([1, 2, 1])
|
| 206 |
+
with mid:
|
| 207 |
+
st.info(":material/hourglass_top: กำลังเชื่อมต่อระบบ…")
|
| 208 |
+
st.stop()
|
| 209 |
+
|
| 210 |
|
| 211 |
# =============================================================================
|
| 212 |
# Config: live data source — OneDrive "anyone with the link" (ตั้งใน Space secret DATA_URL)
|