From d693d26323bf65e9a78990240fd5716182520e8f Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Wed, 18 Dec 2024 10:27:55 +0800 Subject: [PATCH] refactor: clean code --- ui/src/components/access/AccessEditForm.tsx | 6 +++--- ui/src/components/access/AccessEditModal.tsx | 2 +- ui/src/declarations.d.ts | 4 ---- ui/src/domain/access.ts | 2 -- ui/src/domain/certificate.ts | 6 ++---- ui/src/domain/settings.ts | 4 +--- ui/src/domain/workflow.ts | 5 +++-- ui/src/global.css | 10 ---------- ui/src/global.d.ts | 13 +++++++++++++ ui/src/repository/access.ts | 4 ++-- ui/src/repository/settings.ts | 4 ++-- ui/src/repository/workflow.ts | 2 +- ui/src/stores/access/index.ts | 6 +++--- ui/src/stores/workflow/index.ts | 6 +++--- ui/tsconfig.app.json | 14 +++++++++++--- ui/tsconfig.json | 5 +++-- ui/tsconfig.node.json | 4 +++- 17 files changed, 51 insertions(+), 46 deletions(-) delete mode 100644 ui/src/declarations.d.ts create mode 100644 ui/src/global.d.ts diff --git a/ui/src/components/access/AccessEditForm.tsx b/ui/src/components/access/AccessEditForm.tsx index 1a5b9a07..0f03c2bd 100644 --- a/ui/src/components/access/AccessEditForm.tsx +++ b/ui/src/components/access/AccessEditForm.tsx @@ -46,7 +46,7 @@ import AccessEditFormTencentCloudConfig from "./AccessEditFormTencentCloudConfig import AccessEditFormVolcEngineConfig from "./AccessEditFormVolcEngineConfig"; import AccessEditFormWebhookConfig from "./AccessEditFormWebhookConfig"; -type AccessEditFormModelType = Partial>; +type AccessEditFormModelType = Partial>; export type AccessEditFormProps = { className?: string; @@ -79,9 +79,9 @@ const AccessEditForm = forwardRef(( const formRule = createSchemaFieldRule(formSchema); const [form] = Form.useForm>(); - const [initialValues, setInitialValues] = useState>>(model ?? {}); + const [initialValues, setInitialValues] = useState>>(model as Partial>); useEffect(() => { - setInitialValues(model ?? {}); + setInitialValues(model as Partial>); }, [model]); const [configType, setConfigType] = useState(model?.configType); diff --git a/ui/src/components/access/AccessEditModal.tsx b/ui/src/components/access/AccessEditModal.tsx index c18f1cee..4021333e 100644 --- a/ui/src/components/access/AccessEditModal.tsx +++ b/ui/src/components/access/AccessEditModal.tsx @@ -9,7 +9,7 @@ import { getErrMsg } from "@/utils/error"; import AccessEditForm, { type AccessEditFormInstance } from "./AccessEditForm"; export type AccessEditModalProps = { - data?: Partial; + data?: Partial>; loading?: boolean; mode: "add" | "edit" | "copy"; open?: boolean; diff --git a/ui/src/declarations.d.ts b/ui/src/declarations.d.ts deleted file mode 100644 index eb3a1b90..00000000 --- a/ui/src/declarations.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -declare module "antd/locale/zh_CN" { - import zhCN from "antd/locale/zh_CN"; - export default zhCN; -} diff --git a/ui/src/domain/access.ts b/ui/src/domain/access.ts index 94864129..1dd53ad4 100644 --- a/ui/src/domain/access.ts +++ b/ui/src/domain/access.ts @@ -1,5 +1,3 @@ -import { type BaseModel } from "pocketbase"; - /* 注意:如果追加新的常量值,请保持以 ASCII 排序。 NOTICE: If you add new constant, please keep ASCII order. diff --git a/ui/src/domain/certificate.ts b/ui/src/domain/certificate.ts index 249d053d..796b5429 100644 --- a/ui/src/domain/certificate.ts +++ b/ui/src/domain/certificate.ts @@ -1,8 +1,6 @@ -import { type BaseModel } from "pocketbase"; +import { type WorkflowModel } from "./workflow"; -import { WorkflowModel } from "./workflow"; - -export interface CertificateModel extends Omit { +export interface CertificateModel extends BaseModel { san: string; certificate: string; privateKey: string; diff --git a/ui/src/domain/settings.ts b/ui/src/domain/settings.ts index 292eaee2..977bd091 100644 --- a/ui/src/domain/settings.ts +++ b/ui/src/domain/settings.ts @@ -1,6 +1,4 @@ -import { type BaseModel } from "pocketbase"; - -export interface SettingsModel extends Omit { +export interface SettingsModel extends BaseModel { name: string; content: T; } diff --git a/ui/src/domain/workflow.ts b/ui/src/domain/workflow.ts index c3e196d3..9c3a1650 100644 --- a/ui/src/domain/workflow.ts +++ b/ui/src/domain/workflow.ts @@ -1,6 +1,5 @@ import { produce } from "immer"; import { nanoid } from "nanoid"; -import { type BaseModel } from "pocketbase"; import i18n from "@/i18n"; import { deployTargets, KVType } from "./domain"; @@ -28,7 +27,7 @@ export type WorkflowOutput = { error: string; }; -export interface WorkflowModel extends Omit { +export interface WorkflowModel extends BaseModel { name: string; description?: string; type: string; @@ -152,6 +151,8 @@ export const initWorkflow = (): WorkflowModel => { crontab: "0 0 * * *", enabled: false, draft: rs, + created: new Date().toUTCString(), + updated: new Date().toUTCString(), }; }; diff --git a/ui/src/global.css b/ui/src/global.css index 6859a7e9..8717ae8b 100644 --- a/ui/src/global.css +++ b/ui/src/global.css @@ -24,11 +24,6 @@ --input: 20 5.9% 90%; --ring: 24.6 95% 53.1%; --radius: 0.5rem; - --chart-1: 12 76% 61%; - --chart-2: 173 58% 39%; - --chart-3: 197 37% 24%; - --chart-4: 43 74% 66%; - --chart-5: 27 87% 67%; } .dark { @@ -51,10 +46,5 @@ --border: 12 6.5% 15.1%; --input: 12 6.5% 15.1%; --ring: 20.5 90.2% 48.2%; - --chart-1: 220 70% 50%; - --chart-2: 160 60% 45%; - --chart-3: 30 80% 55%; - --chart-4: 280 65% 60%; - --chart-5: 340 75% 55%; } } diff --git a/ui/src/global.d.ts b/ui/src/global.d.ts new file mode 100644 index 00000000..a567ce00 --- /dev/null +++ b/ui/src/global.d.ts @@ -0,0 +1,13 @@ +import { type BaseModel as PbBaseModel } from "pocketbase"; + +declare global { + declare interface BaseModel extends PbBaseModel { + deleted?: string; + } + + declare type MaybeModelRecord = T | Omit; + + declare type MaybeModelRecordWithId = T | Pick; +} + +export {}; diff --git a/ui/src/repository/access.ts b/ui/src/repository/access.ts index 6f0f7d1c..f9912b64 100644 --- a/ui/src/repository/access.ts +++ b/ui/src/repository/access.ts @@ -12,7 +12,7 @@ export const list = async () => { }); }; -export const save = async (record: AccessModel | Omit) => { +export const save = async (record: MaybeModelRecord) => { if (record.id) { return await getPocketBase().collection(COLLECTION_NAME).update(record.id, record); } @@ -20,7 +20,7 @@ export const save = async (record: AccessModel | Omit(record); }; -export const remove = async (record: AccessModel) => { +export const remove = async (record: MaybeModelRecordWithId) => { record = { ...record, deleted: dayjs.utc().format("YYYY-MM-DD HH:mm:ss") }; await getPocketBase().collection(COLLECTION_NAME).update(record.id!, record); }; diff --git a/ui/src/repository/settings.ts b/ui/src/repository/settings.ts index 613a430f..62772d2a 100644 --- a/ui/src/repository/settings.ts +++ b/ui/src/repository/settings.ts @@ -1,4 +1,4 @@ -import { SettingsModel } from "@/domain/settings"; +import { type SettingsModel } from "@/domain/settings"; import { getPocketBase } from "./pocketbase"; export const get = async (name: string) => { @@ -13,7 +13,7 @@ export const get = async (name: string) => { } }; -export const save = async (record: SettingsModel) => { +export const save = async (record: MaybeModelRecordWithId>) => { if (record.id) { return await getPocketBase().collection("settings").update>(record.id, record); } diff --git a/ui/src/repository/workflow.ts b/ui/src/repository/workflow.ts index a57769e4..8688a39a 100644 --- a/ui/src/repository/workflow.ts +++ b/ui/src/repository/workflow.ts @@ -39,7 +39,7 @@ export const save = async (record: Record(record); }; -export const remove = async (record: WorkflowModel) => { +export const remove = async (record: MaybeModelRecordWithId) => { return await getPocketBase().collection(COLLECTION_NAME).delete(record.id); }; diff --git a/ui/src/stores/access/index.ts b/ui/src/stores/access/index.ts index 510167c8..54262458 100644 --- a/ui/src/stores/access/index.ts +++ b/ui/src/stores/access/index.ts @@ -6,9 +6,9 @@ import { list as listAccess, save as saveAccess, remove as removeAccess } from " export interface AccessState { accesses: AccessModel[]; - createAccess: (access: Omit) => void; - updateAccess: (access: AccessModel) => void; - deleteAccess: (access: AccessModel) => void; + createAccess: (access: MaybeModelRecord) => void; + updateAccess: (access: MaybeModelRecordWithId) => void; + deleteAccess: (access: MaybeModelRecordWithId) => void; fetchAccesses: () => Promise; } diff --git a/ui/src/stores/workflow/index.ts b/ui/src/stores/workflow/index.ts index 965ebb95..1e32f377 100644 --- a/ui/src/stores/workflow/index.ts +++ b/ui/src/stores/workflow/index.ts @@ -36,14 +36,14 @@ export const useWorkflowStore = create((set, get) => ({ id: "", name: "placeholder", type: WorkflowNodeType.Start, - }, + } as WorkflowModel, initialized: false, init: async (id?: string) => { - let data: WorkflowModel = { + let data = { id: "", name: "placeholder", type: "auto", - }; + } as WorkflowModel; if (!id) { data = initWorkflow(); diff --git a/ui/tsconfig.app.json b/ui/tsconfig.app.json index 2ce8c6a2..a191ee03 100644 --- a/ui/tsconfig.app.json +++ b/ui/tsconfig.app.json @@ -4,12 +4,18 @@ "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", "target": "ES2020", "useDefineForClassFields": true, - "lib": ["ES2020", "DOM", "DOM.Iterable"], + "lib": [ + "ES2020", + "DOM", + "DOM.Iterable" + ], "module": "ESNext", "skipLibCheck": true, "baseUrl": ".", "paths": { - "@/*": ["./src/*"] + "@/*": [ + "./src/*" + ] }, /* Bundler mode */ "moduleResolution": "bundler", @@ -25,5 +31,7 @@ "noUnusedParameters": true, "noFallthroughCasesInSwitch": true }, - "include": ["src", "src/declarations.d.ts"] + "include": [ + "src" + ] } diff --git a/ui/tsconfig.json b/ui/tsconfig.json index 0302a048..696df044 100644 --- a/ui/tsconfig.json +++ b/ui/tsconfig.json @@ -11,8 +11,9 @@ "compilerOptions": { "baseUrl": ".", "paths": { - "@/*": ["./src/*"] + "@/*": [ + "./src/*" + ] } } } - diff --git a/ui/tsconfig.node.json b/ui/tsconfig.node.json index 3afdd6e3..387a89e7 100644 --- a/ui/tsconfig.node.json +++ b/ui/tsconfig.node.json @@ -9,5 +9,7 @@ "strict": true, "noEmit": true }, - "include": ["vite.config.ts"] + "include": [ + "vite.config.ts" + ] }