eCourt-note/components/utils/DependentDropdown.tsx
Your Name d37bc2a3f0 feat(sso): switch note session from cookies to token
เปลี่ยน session ของแอป note จาก cookie คู่ oacCase/oacCaseSig มาเป็น
access_token แบบ add-in ตามคำขอผู้ใช้ (2026-10-30):

- POST /api/note/auth/session แลก credential ของลิงก์ e-Court
  (JWT ทาง A / ticket ทาง B) เป็น access_token purpose=login อายุ 8 ชม.
  พร้อมลงทะเบียนเอกสาร N8 แบบ trusted ฝั่ง server (fileId จาก fid)
- token เก็บ localStorage "note-auth" (lib/note-session.ts) + Bearer
  ผ่าน noteAuthFetch · Server Actions ทุกตัวรับ token เป็น param แรก
  และแกะ identity เอง (lib/note-token.ts) — ไม่เชื่อ userId จาก client
- หน้า protected เป็น shell + client gate (NoteHome bootstrap ที่ /note,
  AnnotateView + GET /api/note/files/[id]/bootstrap)
- ถอด: middleware.ts, lib/session.ts, utils/user.ts, action logout,
  GET /api/note/auth/e-court, entry /note/annotate?fileUrl=
- ทาง ticket ปฏิเสธ secret default "super_secret" เฉพาะ production
- GET /api/note/files/open: ลิงก์เปิดไฟล์ตัวอย่างแบบ /tmpl (path ใน
  เครื่อง /samples เท่านั้น)
- หน้า /note/dev: ลิงก์ A Link Get(new) ที่ mint credential แล้วยิง
  เข้า flow SSO จริง (JWT/ticket สลับได้) + section Flow + เรียบเรียง
  ภาษาให้อ่านง่าย · หน้า /note เป็นหน้าข้อมูลระบบ
- แก้ตาม code review: redo closure เรียก action ด้วย signature เก่า,
  bootstrap timeout + strip credential ที่ผิดรูป, แยก no-session ออก
  จาก network error, ห้ามอักขระ & = + % ใน identity, ตรวจ purpose
  ก่อน exp ของ login-token, audit log strip อักขระควบคุม
- เอกสาร: docs/PLAN-SSO.md §0.5 (ฉบับ token), CLAUDE.md, .env.example

ทดสอบ: tsc ผ่าน · vitest ผ่านทั้งชุด · ทดสอบจริงผ่าน Playwright
(SSO → viewer → เปลี่ยนสี annotation → undo → redo ครบวงจร)

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-30 18:05:36 +07:00

91 lines
3 KiB
TypeScript

'use client';
import { useState, ChangeEvent, useEffect } from 'react';
export interface DropdownData {
[key: string]: string[];
}
interface DependentDropdownProps {
data: DropdownData;
name1: string; // เพิ่มมาเพื่อใช้กับ Form Action
name2: string; // เพิ่มมาเพื่อใช้กับ Form Action
label1?: string;
label2?: string;
placeholder1?: string;
placeholder2?: string;
onSelectChange?: (mainValue: string, subValue: string) => void;
}
export default function DependentDropdown({
data,
name1,
name2,
label1 = 'เลือกตัวเลือกหลัก:',
label2 = 'เลือกตัวเลือกย่อย:',
placeholder1 = '-- กรุณาเลือก --',
placeholder2 = '-- กรุณาเลือกตัวเลือกหลักก่อน --',
onSelectChange,
}: DependentDropdownProps) {
const [mainSelection, setMainSelection] = useState<string>('');
const [subSelection, setSubSelection] = useState<string>('');
const handleMainChange = (e: ChangeEvent<HTMLSelectElement>) => {
const value = e.target.value;
setMainSelection(value);
setSubSelection('');
};
const handleSubChange = (e: ChangeEvent<HTMLSelectElement>) => {
setSubSelection(e.target.value);
};
useEffect(() => {
if (onSelectChange) {
onSelectChange(mainSelection, subSelection);
}
}, [mainSelection, subSelection, onSelectChange]);
const subOptions = mainSelection ? data[mainSelection] : [];
return (
<div className="space-y-4 w-full">
{/* Dropdown ที่ 1 */}
<div className="flex flex-col gap-1.5">
<label className="text-sm font-medium text-ink">{label1}</label>
<select
name={name1} // ใส่ name ตรงนี้เพื่อให้ Server Action ดึงค่าได้
value={mainSelection}
onChange={handleMainChange}
required
className="genesis-input w-full"
>
<option value="">{placeholder1}</option>
{Object.keys(data).map((key) => (
<option key={key} value={key}>{key}</option>
))}
</select>
</div>
{/* Dropdown ที่ 2 */}
<div className="flex flex-col gap-1.5">
<label className="text-sm font-medium text-ink">{label2}</label>
<select
name={name2} // ใส่ name ตรงนี้เพื่อให้ Server Action ดึงค่าได้
value={subSelection}
onChange={handleSubChange}
disabled={!mainSelection}
required
className="genesis-input w-full disabled:bg-canvas disabled:text-ink-faint"
>
<option value="">
{mainSelection ? '-- กรุณาเลือก --' : placeholder2}
</option>
{subOptions.map((item) => (
<option key={item} value={item}>{item}</option>
))}
</select>
</div>
</div>
);
}