Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add vault mobx store

Joshen Lime044d3889990
diff --git a/studio/components/interfaces/Settings/Vault/EncryptionKeysManagement.tsx b/studio/components/interfaces/Settings/Vault/EncryptionKeysManagement.tsx
index 3aded6cb0a6..08ca7f372b7 100644
--- a/studio/components/interfaces/Settings/Vault/EncryptionKeysManagement.tsx
+++ b/studio/components/interfaces/Settings/Vault/EncryptionKeysManagement.tsx
@@ -1,28 +1,21 @@
-import { FC, Fragment, useState } from 'react'
+import { observer } from 'mobx-react-lite'
+import { FC, Fragment, useEffect, useState } from 'react'
import { Input, IconSearch, Listbox, Button, Divider, Modal, Form, IconHelpCircle } from 'ui'
-import SecretRow from './SecretRow'
import EncryptionKeySelector from './EncryptionKeySelector'
import InformationBox from 'components/ui/InformationBox'
+import { useStore } from 'hooks'
interface Props {}
const EncryptionKeysManagement: FC<Props> = ({}) => {
+ const { vault } = useStore()
+
const [searchValue, setSearchValue] = useState<string>('')
const [selectedSort, setSelectedSort] = useState<string>('name')
const [showAddKeyModal, setShowAddKeyModal] = useState(false)
const [selectedKeyToRemove, setSelectedKeyToRemove] = useState<any>()
- const mockEncryptionKeys = [
- {
- created_at: Number(new Date()),
- },
- {
- created_at: Number(new Date()),
- },
- {
- created_at: Number(new Date()),
- },
- ]
+ console.log('keys', vault.listKeys())
return (
<>
@@ -189,4 +182,4 @@ const EncryptionKeysManagement: FC<Props> = ({}) => {
)
}
-export default EncryptionKeysManagement
+export default observer(EncryptionKeysManagement)
diff --git a/studio/pages/project/[ref]/settings/vault.tsx b/studio/pages/project/[ref]/settings/vault.tsx
index 88956bf90db..e19651248cf 100644
--- a/studio/pages/project/[ref]/settings/vault.tsx
+++ b/studio/pages/project/[ref]/settings/vault.tsx
@@ -6,8 +6,16 @@ import {
SecretsManagement,
EncryptionKeysManagement,
} from 'components/interfaces/Settings/Vault'
+import { useStore } from 'hooks'
+import { useEffect } from 'react'
const VaultSettings: NextPageWithLayout = () => {
+ const { vault } = useStore()
+
+ useEffect(() => {
+ vault.load()
+ }, [])
+
return (
<div className="1xl:px-28 mx-auto flex flex-col gap-8 px-5 py-6 lg:px-16 xl:px-24 2xl:px-32 ">
<VaultToggle />
diff --git a/studio/stores/RootStore.ts b/studio/stores/RootStore.ts
index eadb3754d51..6520c228b2e 100644
--- a/studio/stores/RootStore.ts
+++ b/studio/stores/RootStore.ts
@@ -1,4 +1,4 @@
-import { configure, reaction } from 'mobx'
+import { configure } from 'mobx'
import { Project } from 'types'
import AppStore, { IAppStore } from './app/AppStore'
import MetaStore, { IMetaStore } from './pgmeta/MetaStore'
@@ -9,6 +9,7 @@ import ProjectBackupsStore, { IProjectBackupsStore } from './project/ProjectBack
import ProjectAuthConfigStore, {
IProjectAuthConfigStore,
} from './authConfig/ProjectAuthConfigStore'
+import VaultStore, { IVaultStore } from './project/VaultStore'
// Temporary disable mobx warnings
// TODO: need to remove this after refactoring old stores.
@@ -24,6 +25,7 @@ export interface IRootStore {
functions: IProjectFunctionsStore
backups: IProjectBackupsStore
authConfig: IProjectAuthConfigStore
+ vault: IVaultStore
selectedProjectRef?: string
@@ -38,6 +40,7 @@ export class RootStore implements IRootStore {
functions: IProjectFunctionsStore
backups: IProjectBackupsStore
authConfig: IProjectAuthConfigStore
+ vault: IVaultStore
selectedProjectRef: string | undefined
@@ -50,6 +53,7 @@ export class RootStore implements IRootStore {
this.functions = new ProjectFunctionsStore(this, { projectRef: '' })
this.backups = new ProjectBackupsStore(this, { projectRef: '' })
this.authConfig = new ProjectAuthConfigStore(this, { projectRef: '' })
+ this.vault = new VaultStore(this)
}
/**
diff --git a/studio/stores/project/VaultStore.ts b/studio/stores/project/VaultStore.ts
new file mode 100644
index 00000000000..4da6e993cba
--- /dev/null
+++ b/studio/stores/project/VaultStore.ts
@@ -0,0 +1,96 @@
+import { Query } from 'components/grid'
+import { makeAutoObservable } from 'mobx'
+import { IRootStore } from '../RootStore'
+
+export interface IVaultStore {
+ isLoading: boolean
+ isInitialized: boolean
+ isLoaded: boolean
+ error: any
+
+ load: () => void
+ listKeys: () => any[]
+ listSecrets: () => any[]
+}
+
+export default class VaultStore implements IVaultStore {
+ rootStore: IRootStore
+
+ STATES = {
+ INITIAL: 'initial',
+ LOADING: 'loading',
+ ERROR: 'error',
+ LOADED: 'loaded',
+ }
+
+ data: any = { keys: [], secrets: [] }
+ state = this.STATES.INITIAL
+ error = null
+
+ constructor(rootStore: IRootStore) {
+ this.rootStore = rootStore
+ makeAutoObservable(this)
+ }
+
+ get isLoading() {
+ return this.state === this.STATES.INITIAL || this.state === this.STATES.LOADING
+ }
+
+ get isInitialized() {
+ return this.state !== this.STATES.INITIAL
+ }
+
+ get isLoaded() {
+ return this.state === this.STATES.LOADED
+ }
+
+ async fetchData() {
+ const vault = { keys: [], secrets: [] }
+
+ const query = new Query()
+ .from('key', 'pgsodium')
+ .select('id,key_id,comment,created,status')
+ .toSql()
+ const keys = await this.rootStore.meta.query(query)
+ if (!keys.error) vault.keys = keys
+
+ const secrets: any = []
+ if (!secrets.error) vault.secrets = secrets
+
+ this.data = vault
+ return vault
+ }
+
+ async load() {
+ let { LOADING, ERROR, LOADED } = this.STATES
+ try {
+ this.error = null
+ this.state = LOADING
+ await this.fetchData()
+ this.state = LOADED
+ return this.data
+ } catch (e: any) {
+ console.error('Failed to load vault data', e.message)
+ this.error = e
+ this.state = ERROR
+ }
+ }
+
+ listKeys(filter?: any) {
+ const arr = this.data.keys.slice()
+ if (!!filter) {
+ return arr.filter(filter).sort((a: any, b: any) => b.key_id - a.key_id)
+ } else {
+ return arr.sort((a: any, b: any) => b.key_id - a.key_id)
+ }
+ }
+
+ listSecrets(filter?: any) {
+ const arr = this.data.secrets.slice()
+ if (!!filter) {
+ return arr.filter(filter)
+ } else {
+ return arr
+ }
+ }
+}