SSD Frontend Setup — การตั้งค่าโปรเจค Frontend
บริบท
Frontend ของ SSD ใช้ React + TypeScript (TSX) เป็นหลัก โดยมี Template มาตรฐานที่ดาวน์โหลดได้จาก GitHub Organization ของ SSD ทุกโปรเจคต้องใช้ Template เดียวกันและ Package ชุดเดียวกัน
กฎหลัก
- ต้องใช้ TypeScript + TSX เสมอ ห้ามใช้ JavaScript ล้วน
- ต้องใช้ Template ของ SSD เป็นพื้นฐาน ห้ามสร้างโปรเจคเปล่าเอง
- ต้องบันทึกไฟล์ทุกไฟล์เป็น encoding UTF-8 และลงท้ายบรรทัดด้วย CRLF
- ต้องใช้ Package ที่กำหนดเท่านั้น หากจะใช้ Package อื่นต้องปรึกษาหัวหน้างานก่อน
- ต้องปฏิบัติตาม Style Guide อย่างสม่ำเสมอ: ไฟล์ใหม่ต้องใช้ Style Guide ทันที
เครื่องมือที่ต้องติดตั้ง
VS Code Extensions:
- Auto Close Tag
- Auto Rename Tag
- ESLint
- Path Intellisense
- Prettier - Code formatter
- ES7+ React/Redux/React-Native snippets
- Error Lens
Chrome Extensions:
- Redux Devtools
- React Developer Tools
Prettier Config (ตั้งค่าใน .prettierrc):
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": false,
"printWidth": 120,
"editorconfig": false,
"eslintIntegration": true,
"stylelintIntegration": true,
"endOfLine": "crlf"
}
Package มาตรฐานที่ต้องใช้
ต้องใช้:
- Vite 4.3.9, React 18.2
- @mui/material ^5.13.4 — UI components
- @tanstack/react-query ^4.29.17 — API state management
- axios 1.4.0 — HTTP client
- formik 2.4.1 — Form management
- react-redux 8.0.7, redux-persist 6.0.0 — State management
- react-router-dom 6.12.1 — Routing
- oidc-client-ts 2.2.4 — SSO/Identity Server
- dayjs 1.11.8, @mui/x-date-pickers ^6.9.0 — Date handling
- sweetalert2 11.7.12 — Popup dialogs
ควรใช้ตามความเหมาะสม:
- zod ^3.22.4 + zod-formik-adapter ^1.2.0 — Validation
- apexcharts ^3.35.0 — Charts
- pdfmake ^0.2.10, html-to-image ^1.11.11 — Export
โครงสร้าง Folder มาตรฐาน
src/app/
├── layout/ # Template layout (ไม่ควรแก้ไข)
├── api/ # API client files
│ ├── orderApi.client.ts # สร้างจาก NSwag
│ ├── orderApi.ts # เขียนเอง
│ └── order.query.ts # React Query hooks
├── modules/ # UI screens และ components
│ ├── _common/ # Shared components (Template, ไม่ควรแก้)
│ ├── _auth/ # Auth components (Template, ไม่ควรแก้)
│ └── Order/ # ชื่อ Module ต้องขึ้นต้นด้วยตัวใหญ่
│ ├── components/
│ │ ├── OrderTable.tsx
│ │ ├── OrderTable.hook.tsx
│ │ └── index.ts
│ ├── pages/
│ │ ├── IndexPage.tsx # หน้าแรกของ module ต้องชื่อนี้
│ │ ├── OrderPage.tsx # ทุก page ต้องลงท้ายด้วย Page
│ │ └── index.ts
│ └── orderSlice.ts
├── routes/
│ ├── Routes.tsx # Route configuration
│ └── ASideMenuList.tsx # Sidebar menu
└── redux/ # Redux store configuration
src/
├── const.ts # Constants และ environment variables
└── vite-env.d.ts # Type definitions สำหรับ env vars
Naming Conventions
TypeScript/JavaScript
| รูปแบบ |
ใช้กับ |
PascalCase |
class, interface, type, enum, component functions, TSX |
camelCase |
variable, parameter, function, method, property |
CONSTANT_CASE |
global constants, enum values |
- ตัวย่อ 2+ คำ: ตัวใหญ่ทั้งหมด เช่น
DTO, API, HTML
- ตัวย่อ 1 คำ: PascalCase เช่น
Id, Doc, Cust
- ห้ามใช้
any type — ให้ใช้ unknown หรือ type ที่ชัดเจน
- ห้ามใช้
{} type — ให้ใช้ Record<string, T> แทน
React/File Naming
- ไฟล์ component ใช้
.tsx เสมอ
- ชื่อไฟล์ component: PascalCase เช่น
ReservationCard.tsx
- Component หลักของ folder ชื่อ
index.tsx
- Module names: PascalCase, ไม่เกิน 3 คำ
- Page names: ขึ้นต้นตัวใหญ่, ลงท้ายด้วย
Page
- Component naming suffixes:
Container, View, Card, Form, Table, List, Item
Route Naming
- ตัวพิมพ์เล็กทั้งหมด, ใช้
- แทน space
- ห้ามใช้เครื่องหมายวรรคตอน
- ไม่เกิน 70 ตัวอักษร
ขั้นตอนการตั้งค่า Environment Variables
ขั้นตอนที่ 1: เพิ่ม Type ใน vite-env.d.ts
interface ImportMetaEnv {
readonly VITE_API_URL: string;
readonly VITE_SSO_ISSUER: string;
// ชื่อ config ต้องขึ้นต้นด้วย VITE_ เสมอ
}
ขั้นตอนที่ 2: ตั้งค่าใน .env files
VITE_APP_NAME = "ชื่อระบบ"
VITE_APP_VERSION = "1.0.0"
VITE_BASE_URL = "https://..."
VITE_API_URL = "https://...api..."
VITE_SSO_ISSUER = "https://...sso..."
VITE_SSO_CLIENT_ID = "..."
VITE_SSO_SCOPE = "openid profile roles email ..."
| ไฟล์ |
ใช้กับ |
.env |
ค่าพื้นฐาน |
.env.production |
Production |
.env.uat |
UAT |
.env.dev |
Development |
.env.local |
เครื่องตัวเอง |
ขั้นตอนที่ 3: Export ผ่าน const.ts (แนะนำ)
// /src/const.ts
export { VITE_BASE_URL, VITE_API_URL } = window.__CONST__ENV__;
// เรียกใช้ใน Component
import { API_URL } from '../Const';
ห้ามใช้ import.meta.env โดยตรง ให้ใช้ window.__CONST__ENV__ หรือผ่าน const.ts
RPA & Automation Test
เพื่อให้ component สามารถทดสอบด้วย RPA และ automation test ได้:
- Standard form components ทุกตัวต้องใช้งานได้ตามปกติ
- Button ต้องใส่ attribute
name
- Label, Typography ที่แสดงผล ต้องใส่
name
- Dynamic render component ต้องใส่ attribute
id
1---2name: ssd-frontend-setup3description: ใช้ skill นี้เมื่อตั้งค่าโปรเจค Frontend ใหม่, ถามเรื่องโครงสร้าง folder ของ React, เครื่องมือที่ต้องติดตั้ง, naming conventions สำหรับ TypeScript/React, หรือ environment variables ของ SSD4---56# SSD Frontend Setup — การตั้งค่าโปรเจค Frontend78## บริบท910Frontend ของ SSD ใช้ React + TypeScript (TSX) เป็นหลัก โดยมี Template มาตรฐานที่ดาวน์โหลดได้จาก GitHub Organization ของ SSD ทุกโปรเจคต้องใช้ Template เดียวกันและ Package ชุดเดียวกัน1112## กฎหลัก13141. ต้องใช้ TypeScript + TSX เสมอ ห้ามใช้ JavaScript ล้วน152. ต้องใช้ Template ของ SSD เป็นพื้นฐาน ห้ามสร้างโปรเจคเปล่าเอง163. ต้องบันทึกไฟล์ทุกไฟล์เป็น encoding UTF-8 และลงท้ายบรรทัดด้วย CRLF174. ต้องใช้ Package ที่กำหนดเท่านั้น หากจะใช้ Package อื่นต้องปรึกษาหัวหน้างานก่อน185. ต้องปฏิบัติตาม Style Guide อย่างสม่ำเสมอ: ไฟล์ใหม่ต้องใช้ Style Guide ทันที1920## เครื่องมือที่ต้องติดตั้ง2122**VS Code Extensions:**23- Auto Close Tag24- Auto Rename Tag25- ESLint26- Path Intellisense27- Prettier - Code formatter28- ES7+ React/Redux/React-Native snippets29- Error Lens3031**Chrome Extensions:**32- Redux Devtools33- React Developer Tools3435**Prettier Config (ตั้งค่าใน .prettierrc):**36```json37{38 "trailingComma": "es5",39 "tabWidth": 4,40 "semi": true,41 "singleQuote": false,42 "printWidth": 120,43 "editorconfig": false,44 "eslintIntegration": true,45 "stylelintIntegration": true,46 "endOfLine": "crlf"47}48```4950## Package มาตรฐานที่ต้องใช้5152**ต้องใช้:**53- Vite 4.3.9, React 18.254- @mui/material ^5.13.4 — UI components55- @tanstack/react-query ^4.29.17 — API state management56- axios 1.4.0 — HTTP client57- formik 2.4.1 — Form management58- react-redux 8.0.7, redux-persist 6.0.0 — State management59- react-router-dom 6.12.1 — Routing60- oidc-client-ts 2.2.4 — SSO/Identity Server61- dayjs 1.11.8, @mui/x-date-pickers ^6.9.0 — Date handling62- sweetalert2 11.7.12 — Popup dialogs6364**ควรใช้ตามความเหมาะสม:**65- zod ^3.22.4 + zod-formik-adapter ^1.2.0 — Validation66- apexcharts ^3.35.0 — Charts67- pdfmake ^0.2.10, html-to-image ^1.11.11 — Export6869## โครงสร้าง Folder มาตรฐาน7071```72src/app/73├── layout/ # Template layout (ไม่ควรแก้ไข)74├── api/ # API client files75│ ├── orderApi.client.ts # สร้างจาก NSwag76│ ├── orderApi.ts # เขียนเอง77│ └── order.query.ts # React Query hooks78├── modules/ # UI screens และ components79│ ├── _common/ # Shared components (Template, ไม่ควรแก้)80│ ├── _auth/ # Auth components (Template, ไม่ควรแก้)81│ └── Order/ # ชื่อ Module ต้องขึ้นต้นด้วยตัวใหญ่82│ ├── components/83│ │ ├── OrderTable.tsx84│ │ ├── OrderTable.hook.tsx85│ │ └── index.ts86│ ├── pages/87│ │ ├── IndexPage.tsx # หน้าแรกของ module ต้องชื่อนี้88│ │ ├── OrderPage.tsx # ทุก page ต้องลงท้ายด้วย Page89│ │ └── index.ts90│ └── orderSlice.ts91├── routes/92│ ├── Routes.tsx # Route configuration93│ └── ASideMenuList.tsx # Sidebar menu94└── redux/ # Redux store configuration9596src/97├── const.ts # Constants และ environment variables98└── vite-env.d.ts # Type definitions สำหรับ env vars99```100101## Naming Conventions102103### TypeScript/JavaScript104| รูปแบบ | ใช้กับ |105|--------|--------|106| `PascalCase` | class, interface, type, enum, component functions, TSX |107| `camelCase` | variable, parameter, function, method, property |108| `CONSTANT_CASE` | global constants, enum values |109110- ตัวย่อ 2+ คำ: ตัวใหญ่ทั้งหมด เช่น `DTO`, `API`, `HTML`111- ตัวย่อ 1 คำ: PascalCase เช่น `Id`, `Doc`, `Cust`112- ห้ามใช้ `any` type — ให้ใช้ `unknown` หรือ type ที่ชัดเจน113- ห้ามใช้ `{}` type — ให้ใช้ `Record<string, T>` แทน114115### React/File Naming116- ไฟล์ component ใช้ `.tsx` เสมอ117- ชื่อไฟล์ component: PascalCase เช่น `ReservationCard.tsx`118- Component หลักของ folder ชื่อ `index.tsx`119- Module names: PascalCase, ไม่เกิน 3 คำ120- Page names: ขึ้นต้นตัวใหญ่, ลงท้ายด้วย `Page`121- Component naming suffixes: `Container`, `View`, `Card`, `Form`, `Table`, `List`, `Item`122123### Route Naming124- ตัวพิมพ์เล็กทั้งหมด, ใช้ `-` แทน space125- ห้ามใช้เครื่องหมายวรรคตอน126- ไม่เกิน 70 ตัวอักษร127128## ขั้นตอนการตั้งค่า Environment Variables129130### ขั้นตอนที่ 1: เพิ่ม Type ใน vite-env.d.ts131```typescript132interface ImportMetaEnv {133 readonly VITE_API_URL: string;134 readonly VITE_SSO_ISSUER: string;135 // ชื่อ config ต้องขึ้นต้นด้วย VITE_ เสมอ136}137```138139### ขั้นตอนที่ 2: ตั้งค่าใน .env files140```141VITE_APP_NAME = "ชื่อระบบ"142VITE_APP_VERSION = "1.0.0"143VITE_BASE_URL = "https://..."144VITE_API_URL = "https://...api..."145VITE_SSO_ISSUER = "https://...sso..."146VITE_SSO_CLIENT_ID = "..."147VITE_SSO_SCOPE = "openid profile roles email ..."148```149150| ไฟล์ | ใช้กับ |151|------|--------|152| `.env` | ค่าพื้นฐาน |153| `.env.production` | Production |154| `.env.uat` | UAT |155| `.env.dev` | Development |156| `.env.local` | เครื่องตัวเอง |157158### ขั้นตอนที่ 3: Export ผ่าน const.ts (แนะนำ)159```typescript160// /src/const.ts161export { VITE_BASE_URL, VITE_API_URL } = window.__CONST__ENV__;162```163164```typescript165// เรียกใช้ใน Component166import { API_URL } from '../Const';167```168169> ห้ามใช้ `import.meta.env` โดยตรง ให้ใช้ `window.__CONST__ENV__` หรือผ่าน const.ts170171## RPA & Automation Test172173เพื่อให้ component สามารถทดสอบด้วย RPA และ automation test ได้:1741. Standard form components ทุกตัวต้องใช้งานได้ตามปกติ1752. Button ต้องใส่ attribute `name`1763. Label, Typography ที่แสดงผล ต้องใส่ `name`1774. Dynamic render component ต้องใส่ attribute `id`