Skip to content

Commit

Permalink
fix(frontend): improved dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
bekossy committed Dec 17, 2024
1 parent c8a3ace commit aad37b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 5 additions & 5 deletions agenta-web/src/hooks/useVaultSecret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {
saveLlmProviderKey,
} from "@/lib/helpers/llmProviders"
import {isDemo} from "@/lib/helpers/utils"
import {dynamicLib, dynamicService} from "@/lib/helpers/dynamic"

export const useVaultSecret = () => {
const [secrets, setSecrets] = useState<LlmProvider[]>(llmAvailableProviders)

const getVaultSecrets = async () => {
try {
if (isDemo()) {
const {fetchVaultSecret} = await import("@/services/vault/api"!)
const {fetchVaultSecret} = await dynamicService("vault/api")
const data = await fetchVaultSecret()

setSecrets((prevSecret) => {
Expand Down Expand Up @@ -46,8 +47,8 @@ export const useVaultSecret = () => {
const handleModifyVaultSecret = async (provider: LlmProvider) => {
try {
if (isDemo()) {
const {createVaultSecret, updateVaultSecret} = await import("@/services/vault/api"!)
const {SecretDTOKind, SecretDTOProvider} = await import("@/lib/types_ee"!)
const {updateVaultSecret, createVaultSecret} = await dynamicService("vault/api")
const {SecretDTOProvider, SecretDTOKind} = await dynamicLib("types_ee")

const envNameMap: Record<string, any> = {
OPENAI_API_KEY: SecretDTOProvider.OPENAI,
Expand Down Expand Up @@ -98,8 +99,7 @@ export const useVaultSecret = () => {
const handleDeleteVaultSecret = async (provider: LlmProvider) => {
try {
if (isDemo() && provider.id) {
const {deleteVaultSecret} = await import("@/services/vault/api"!)

const {deleteVaultSecret} = await dynamicService("vault/api")
await deleteVaultSecret({secret_id: provider.id})
await getVaultSecrets()
} else {
Expand Down
8 changes: 8 additions & 0 deletions agenta-web/src/lib/helpers/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ export async function dynamicService(path: string, fallback?: any) {
return fallback
}
}

export async function dynamicLib(path: string, fallback?: any) {
try {
return await import(`@/lib/${path}`)
} catch (error) {
return fallback
}
}

0 comments on commit aad37b8

Please sign in to comment.