Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: re-implement setting store module with pinia (#1029)

Liu Ji82e7a2f1b7fc
diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vue
index 9afa75ddb5..26b3460106 100644
--- a/frontend/src/components/ProvideDashboardContext.vue
+++ b/frontend/src/components/ProvideDashboardContext.vue
@@ -3,7 +3,7 @@
</template>
<script lang="ts">
-import { useUIStateStore } from "@/store";
+import { useSettingStore, useUIStateStore } from "@/store";
import { defineComponent } from "vue";
import { useStore } from "vuex";
import { DEFAULT_PROJECT_ID } from "../types";
@@ -14,7 +14,7 @@ export default defineComponent({
const store = useStore();
await Promise.all([
- store.dispatch("setting/fetchSetting"),
+ useSettingStore().fetchSetting(),
// Fetch so MemberSelect can have the data.
store.dispatch("member/fetchMemberList"),
// Though fetchMemberList also return the principal info, it's possible that a principal is no longer a member.
diff --git a/frontend/src/store/modules/setting.ts b/frontend/src/store/modules/setting.ts
deleted file mode 100644
index 7a75120ac3..0000000000
--- a/frontend/src/store/modules/setting.ts
+++ /dev/null
@@ -1,93 +0,0 @@
-import axios from "axios";
-import { ResourceObject, SettingState } from "../../types";
-import { Setting, SettingName } from "../../types/setting";
-import { getPrincipalFromIncludedList } from "./principal";
-
-function convert(
- setting: ResourceObject,
- includedList: ResourceObject[],
- rootGetters: any
-): Setting {
- return {
- ...(setting.attributes as Omit<Setting, "id" | "creator" | "updater">),
- id: parseInt(setting.id),
- creator: getPrincipalFromIncludedList(
- setting.relationships!.creator.data,
- includedList
- ),
- updater: getPrincipalFromIncludedList(
- setting.relationships!.updater.data,
- includedList
- ),
- };
-}
-
-const state: () => SettingState = () => ({
- settingByName: new Map(),
-});
-
-const getters = {
- settingByName:
- (state: SettingState) =>
- (name: SettingName): Setting | undefined => {
- return state.settingByName.get(name);
- },
-};
-
-const actions = {
- async fetchSetting({ commit, rootGetters }: any): Promise<Setting[]> {
- const data = (await axios.get(`/api/setting`)).data;
- const settingList = data.data.map((setting: ResourceObject) => {
- return convert(setting, data.included, rootGetters);
- });
- for (const setting of settingList) {
- commit("setSettingByName", { name: setting.name, setting });
- }
- return settingList;
- },
-
- async updateSettingByName(
- { commit, rootGetters }: any,
- { name, value }: { name: SettingName; value: string }
- ): Promise<Setting> {
- const data = (
- await axios.patch(`/api/setting/${name}`, {
- data: {
- type: "settingPatch",
- attributes: {
- value,
- },
- },
- })
- ).data;
-
- const setting = convert(data.data, data.included, rootGetters);
-
- commit("setSettingByName", { name: setting.name, setting });
-
- return setting;
- },
-};
-
-const mutations = {
- setSettingByName(
- state: SettingState,
- {
- name,
- setting,
- }: {
- name: SettingName;
- setting: Setting;
- }
- ) {
- state.settingByName.set(name, setting);
- },
-};
-
-export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 8abadd8b81..359f9fbe93 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -4,6 +4,7 @@ export * from "./debug";
export * from "./label";
export * from "./oauth";
export * from "./router";
+export * from "./setting";
export * from "./subscription";
export * from "./tab";
export * from "./uistate";
diff --git a/frontend/src/store/pinia-modules/setting.ts b/frontend/src/store/pinia-modules/setting.ts
new file mode 100644
index 0000000000..bd2c4a2da7
--- /dev/null
+++ b/frontend/src/store/pinia-modules/setting.ts
@@ -0,0 +1,76 @@
+import axios from "axios";
+import { defineStore } from "pinia";
+import { ResourceObject, SettingState } from "@/types";
+import { Setting, SettingName } from "@/types/setting";
+import { getPrincipalFromIncludedList } from "../modules/principal";
+
+function convert(
+ setting: ResourceObject,
+ includedList: ResourceObject[]
+): Setting {
+ return {
+ ...(setting.attributes as Omit<Setting, "id" | "creator" | "updater">),
+ id: parseInt(setting.id),
+ creator: getPrincipalFromIncludedList(
+ setting.relationships!.creator.data,
+ includedList
+ ),
+ updater: getPrincipalFromIncludedList(
+ setting.relationships!.updater.data,
+ includedList
+ ),
+ };
+}
+
+export const useSettingStore = defineStore("setting", {
+ state: (): SettingState => ({
+ settingByName: new Map(),
+ }),
+ actions: {
+ getSettingByName(name: SettingName) {
+ return this.settingByName.get(name);
+ },
+ setSettingByName({
+ name,
+ setting,
+ }: {
+ name: SettingName;
+ setting: Setting;
+ }) {
+ this.settingByName.set(name, setting);
+ },
+ async fetchSetting(): Promise<Setting[]> {
+ const data = (await axios.get(`/api/setting`)).data;
+ const settingList = data.data.map((setting: ResourceObject) => {
+ return convert(setting, data.included);
+ });
+ for (const setting of settingList) {
+ this.setSettingByName({ name: setting.name, setting });
+ }
+ return settingList;
+ },
+ async updateSettingByName({
+ name,
+ value,
+ }: {
+ name: SettingName;
+ value: string;
+ }): Promise<Setting> {
+ const data = (
+ await axios.patch(`/api/setting/${name}`, {
+ data: {
+ type: "settingPatch",
+ attributes: {
+ value,
+ },
+ },
+ })
+ ).data;
+
+ const setting = convert(data.data, data.included);
+ this.setSettingByName({ name: setting.name, setting });
+
+ return setting;
+ },
+ },
+});
diff --git a/frontend/src/views/DashboardHeader.vue b/frontend/src/views/DashboardHeader.vue
index 6b360d012c..c5f84e67ba 100644
--- a/frontend/src/views/DashboardHeader.vue
+++ b/frontend/src/views/DashboardHeader.vue
@@ -196,10 +196,10 @@ import { useI18n } from "vue-i18n";
import ProfileDropdown from "../components/ProfileDropdown.vue";
import { InboxSummary, UNKNOWN_ID } from "../types";
-import { Setting, brandingLogoSettingName } from "../types/setting";
+import { brandingLogoSettingName } from "../types/setting";
import { isDBAOrOwner, isDev } from "../utils";
import { useLanguage } from "../composables/useLanguage";
-import { useSubscriptionStore } from "@/store";
+import { useSettingStore, useSubscriptionStore } from "@/store";
import { storeToRefs } from "pinia";
interface LocalState {
@@ -212,6 +212,7 @@ export default defineComponent({
setup() {
const { t, availableLocales } = useI18n();
const store = useStore();
+ const settingStore = useSettingStore();
const subscriptionStore = useSubscriptionStore();
const router = useRouter();
const { setLocale, toggleLocales } = useLanguage();
@@ -233,13 +234,13 @@ export default defineComponent({
});
const prepareBranding = () => {
- store.dispatch("setting/fetchSetting");
+ settingStore.fetchSetting();
};
const logoUrl = computed((): string | undefined => {
- const brandingLogoSetting: Setting = store.getters[
- "setting/settingByName"
- ](brandingLogoSettingName);
+ const brandingLogoSetting = settingStore.getSettingByName(
+ brandingLogoSettingName
+ );
return brandingLogoSetting?.value;
});
diff --git a/frontend/src/views/SettingWorkspaceGeneral.vue b/frontend/src/views/SettingWorkspaceGeneral.vue
index 333c6b5e14..1773eb479f 100644
--- a/frontend/src/views/SettingWorkspaceGeneral.vue
+++ b/frontend/src/views/SettingWorkspaceGeneral.vue
@@ -96,9 +96,9 @@
import { computed, reactive } from "vue";
import { useStore } from "vuex";
import { isOwner } from "../utils";
-import { Setting, brandingLogoSettingName } from "../types/setting";
+import { brandingLogoSettingName } from "../types/setting";
import { useI18n } from "vue-i18n";
-import { featureToRef } from "@/store";
+import { featureToRef, useSettingStore } from "@/store";
interface LocalState {
displayName?: string;
@@ -113,14 +113,15 @@ const supportImageExtensions = [".jpg", ".jpeg", ".png", ".webp", ".svg"];
// convertFileToBase64 will convert a file into base64 string.
const convertFileToBase64 = (file: File) =>
- new Promise((resolve, reject) => {
+ new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
- reader.onload = () => resolve(reader.result);
+ reader.onload = () => resolve(reader.result as string);
reader.onerror = (error) => reject(error);
});
const store = useStore();
+const settingStore = useSettingStore();
const { t } = useI18n();
const state = reactive<LocalState>({
@@ -131,10 +132,10 @@ const state = reactive<LocalState>({
showFeatureModal: false,
});
-store.dispatch("setting/fetchSetting").then(() => {
- const brandingLogoSetting: Setting = store.getters["setting/settingByName"](
+settingStore.fetchSetting().then(() => {
+ const brandingLogoSetting = settingStore.getSettingByName(
brandingLogoSettingName
- );
+ )!;
state.logoUrl = brandingLogoSetting.value;
});
@@ -172,13 +173,10 @@ const uploadLogo = async () => {
try {
const fileInBase64 = await convertFileToBase64(state.logoFile);
- const setting: Setting = await store.dispatch(
- "setting/updateSettingByName",
- {
- name: brandingLogoSettingName,
- value: fileInBase64,
- }
- );
+ const setting = await useSettingStore().updateSettingByName({
+ name: brandingLogoSettingName,
+ value: fileInBase64,
+ });
state.logoFile = null;
state.logoUrl = setting.value;