Menixis Claude Opus 4.8 (1M context) commited on
Commit
8433a01
·
1 Parent(s): adf9eeb

signer: silent background LNA check; notify only when not allowed

Browse files

Replace 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>

Files changed (1) hide show
  1. 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
- """HARD entry gate: block the whole app until the browser can reach the SMP gateway
169
- (user grants the one-time Local Network Access 'Allow'). No manual bypass — only recovery
170
- guidance + retry. Off-network users cannot pass (by design, per product decision)."""
 
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
- st.markdown("<div style='height:6vh'></div>", unsafe_allow_html=True)
178
- _, mid, _ = st.columns([1, 2, 1])
179
- with mid:
180
- st.markdown(
181
- "<div style='text-align:center'>"
182
- "<div style='font-size:2.6rem'>📡</div>"
183
- "<h3>เปิดใช้งานการดึงชื่อผู้ลงนามอัตโนมัติ</h3>"
184
- "<p>แอปนี้ต้องเชื่อมต่อระบบ SMP เพื่อดึงชื่อ <b>ผู้อนุมัติ / ประธานกรรมการตรวจรับ</b> ให้อัตโนมัติ<br>"
185
- "กดปุ่มด้านล่าง แล้วกด <b>“Allow / อนุญาต”</b> ที่ป๊อปอัปของเบราว์เซอร์ (ทำครั้งเดียวต่อเครื่อง)</p></div>",
186
- unsafe_allow_html=True,
187
- )
188
- if not st.session_state.get("lna_armed"):
189
- if st.button(":material/lan: เปิดใช้งาน", type="primary", use_container_width=True):
190
- st.session_state["lna_armed"] = True
191
- st.rerun()
192
- st.stop()
193
-
194
- # armed → run the probe (this triggers the browser's native "Allow" prompt)
195
- res = streamlit_js_eval(js_expressions=signer_lookup.build_probe_js(SMP_TOKEN),
196
- key="lna_probe", want_output=True)
197
- ok = None
198
- if isinstance(res, str) and res.strip().startswith("{"):
199
- try:
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)