From d9d2bfb932e99c6d4db60931e9c2e20842893fa2 Mon Sep 17 00:00:00 2001 From: yanle <421361608@qq.com> Date: Fri, 26 Jan 2024 23:00:10 +0800 Subject: [PATCH] fix: pr comments fix --- .../AddTagToWorkflowPopover.tsx | 33 ++++-------- ui/src/db-tables/TableBase.ts | 2 +- ui/src/db-tables/UserSettingsTable.ts | 4 ++ ui/src/db-tables/WorkspaceDB.ts | 52 +++++-------------- ui/src/db-tables/indexdb.ts | 2 +- ui/src/db-tables/tagsTable.ts | 11 ---- 6 files changed, 29 insertions(+), 75 deletions(-) diff --git a/ui/src/RecentFilesDrawer/AddTagToWorkflowPopover.tsx b/ui/src/RecentFilesDrawer/AddTagToWorkflowPopover.tsx index 58d9e01..d4d65e6 100644 --- a/ui/src/RecentFilesDrawer/AddTagToWorkflowPopover.tsx +++ b/ui/src/RecentFilesDrawer/AddTagToWorkflowPopover.tsx @@ -10,10 +10,8 @@ import { Input, HStack, IconButton, - Alert, - AlertIcon, } from "@chakra-ui/react"; -import { useEffect, useState, useContext } from "react"; +import { useState, useContext } from "react"; import { tagsTable, workflowsTable } from "../db-tables/WorkspaceDB"; import { IconPlus, IconTag } from "@tabler/icons-react"; import { MultiValue, Select } from "chakra-react-select"; @@ -27,7 +25,6 @@ export default function AddTagToWorkflowPopover({ workflow }: Props) { const { onRefreshFilesList } = useContext(RecentFilesContext); const [allTags, setAllTags] = useState([]); const [newTagName, setNewTagName] = useState(""); - const [submitError, setSubmitError] = useState(false); const initialTags = workflow.tags?.map((t) => ({ value: t, @@ -38,18 +35,14 @@ export default function AddTagToWorkflowPopover({ workflow }: Props) { useState>(initialTags); const addTag = async () => { - const tagNameList = - (await tagsTable?.listAll().then((list) => list.map((f) => f.name))) ?? - []; - if (tagNameList.includes(newTagName)) { - console.log(1111); - setSubmitError(true); - } else { - await tagsTable?.create(newTagName); - await tagsTable?.listAll().then((tags) => setAllTags(tags ?? [])); - setNewTagName(""); - setSubmitError(false); - } + await tagsTable?.put({ + id: newTagName, + name: newTagName, + workflowIDs: [], + updateTime: Date.now(), + }); + await tagsTable?.listAll().then((tags) => setAllTags(tags ?? [])); + setNewTagName(""); }; const onOpen = async () => { @@ -141,14 +134,6 @@ export default function AddTagToWorkflowPopover({ workflow }: Props) { New Tag - {submitError ? ( - - - The name is duplicated, please modify it and submit again. - - ) : ( - "" - )} diff --git a/ui/src/db-tables/TableBase.ts b/ui/src/db-tables/TableBase.ts index 95dac3d..ebfb1da 100644 --- a/ui/src/db-tables/TableBase.ts +++ b/ui/src/db-tables/TableBase.ts @@ -10,7 +10,7 @@ export class TableBase { protected constructor(tableName: Table) { this.tableName = tableName; } - protected async saveDiskDB() { + async saveDiskDB() { const objs = (await indexdb[this.tableName].toArray()) as TableBaseModel[]; const backup: Record = {}; objs.forEach((f) => { diff --git a/ui/src/db-tables/UserSettingsTable.ts b/ui/src/db-tables/UserSettingsTable.ts index 344eaaf..9f402a4 100644 --- a/ui/src/db-tables/UserSettingsTable.ts +++ b/ui/src/db-tables/UserSettingsTable.ts @@ -1,5 +1,7 @@ import { UserSettings } from "../types/dbTypes"; import { TableBase } from "./TableBase"; +import { getSystemDir } from "../Api"; + export class UserSettingsTable extends TableBase { public defaultSettings: UserSettings; public readonly DEFAULT_USER = "guest"; @@ -43,6 +45,8 @@ export class UserSettingsTable extends TableBase { static async load(): Promise { const instance = new UserSettingsTable(); + const getDir = await getSystemDir(); + instance.defaultSettings.myWorkflowsDir = `${getDir.dir_path}/my_workflows`; return instance; } } diff --git a/ui/src/db-tables/WorkspaceDB.ts b/ui/src/db-tables/WorkspaceDB.ts index bfcd0d4..983c2c7 100644 --- a/ui/src/db-tables/WorkspaceDB.ts +++ b/ui/src/db-tables/WorkspaceDB.ts @@ -5,9 +5,7 @@ import { MediaTable } from "./MediaTable"; import { UserSettingsTable } from "./UserSettingsTable"; import { TagsTable } from "./tagsTable"; import { indexdb } from "./indexdb"; -import { Folder, UserSettings, Workflow } from "../types/dbTypes"; -import { v4 as uuidv4 } from "uuid"; -import { getSystemDir } from "../Api"; +import { Folder, Workflow } from "../types/dbTypes"; export type Table = | "workflows" @@ -103,7 +101,7 @@ export async function backfillIndexdb() { // ID is a new field, compatible with the historical data of existing users. if (tagList.length > 0 && !tagList[0].id) { tagList.forEach((tag) => { - tag.id = uuidv4(); + tag.id = tag.name; }); } @@ -114,40 +112,18 @@ export async function backfillIndexdb() { }; const backfillUserSettings = async () => { try { - /** - * In the new version, the data structure of the local disk backup of userSettings has changed, adding id, which is the user id. - * Currently the default is set to Default User. - * Therefore, the backfill needs to be compatible with the historical data of the original users. - */ - if (!userSettingsTable) return; - let backupData = await userSettingsTable?.getRecords(); - if (Object.keys(backupData).length === 0) { - // no local backup data - backupData = { - [userSettingsTable.DEFAULT_USER]: userSettingsTable.defaultSettings, - }; - } - - let backfillList; - // If userId does not exist, it means that it is historical data and userId needs to be added. - if (!backupData[userSettingsTable.DEFAULT_USER]) { - backfillList = [ - { - id: userSettingsTable.DEFAULT_USER, - ...backupData, - }, - ] as UserSettings[]; - } else { - backfillList = Object.values(backupData); - } - if (!backfillList[0].myWorkflowsDir) { - const getDir = await getSystemDir(); - const myWorkflowsDir = `${getDir.dir_path}/my_workflows`; - backfillList[0].myWorkflowsDir = myWorkflowsDir; - userSettingsTable!.defaultSettings.myWorkflowsDir = myWorkflowsDir; - } - await indexdb.userSettings.bulkAdd(backfillList); - await userSettingsTable?.saveDiskDB(); + const tableInstance = userSettingsTable!; + const backupData = await tableInstance.getRecords().then((data) => { + if (!data[tableInstance.DEFAULT_USER]) { + // legacy + return data; + } + return data[tableInstance.DEFAULT_USER]; + }); + await indexdb.userSettings.put({ + ...tableInstance.defaultSettings, + ...backupData, + }); } catch (error) { console.error(error); } diff --git a/ui/src/db-tables/indexdb.ts b/ui/src/db-tables/indexdb.ts index c65d7ee..e6f7296 100644 --- a/ui/src/db-tables/indexdb.ts +++ b/ui/src/db-tables/indexdb.ts @@ -24,7 +24,7 @@ class ManagerDB extends Dexie { constructor() { super(WORKSPACE_INDEXDB_NAME); - this.version(2) + this.version(1) .stores({ workflows: "&id, name, parentFolderID", // Primary key and indexed props changelogs: "&id, workflowID", diff --git a/ui/src/db-tables/tagsTable.ts b/ui/src/db-tables/tagsTable.ts index 5ffce0c..d58bb07 100644 --- a/ui/src/db-tables/tagsTable.ts +++ b/ui/src/db-tables/tagsTable.ts @@ -1,7 +1,6 @@ import { Tag } from "../types/dbTypes"; import { Table } from "./WorkspaceDB"; import { TableBase } from "./TableBase"; -import { v4 as uuidv4 } from "uuid"; export class TagsTable extends TableBase { static readonly TABLE_NAME: Table = "tags"; @@ -14,14 +13,4 @@ export class TagsTable extends TableBase { const instance = new TagsTable(); return instance; } - - public async create(name: string) { - const newTag = { - id: uuidv4(), - name, - workflowIDs: [], - updateTime: Date.now(), - }; - await this.add(newTag); - } }