Skip to content

Commit

Permalink
fix: adjust initial decoding process for dynamic config viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
danpacho committed Aug 21, 2024
1 parent 0feab5a commit 1ae0839
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions packages/plugin/src/react/view/build/build.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { Promisify } from '@obsidian_blogger/helpers/promisify'
import React, { useCallback, useEffect, useState } from 'react'
import { DynamicConfigViewer } from './components'
import { Decoder, type DecoderAdapter } from './core'
import type {
BuildBridgeHistoryRecord,
BuildBridgeHistoryValue,
Expand Down Expand Up @@ -318,17 +319,19 @@ export type UserPluginConfigSetter = (
key: string,
config: {
value: PluginDynamicConfigPrimitiveType
decoder?: (
value: PluginDynamicConfigPrimitiveType
) => PluginDynamicConfigPrimitiveType
decoder?: DecoderAdapter
}
) => void

const FlattenRecord = (
record: PluginInterfaceDynamicConfig,
const FlattenDynamicConfig = ({
dynamicConfig,
dynamicConfigSchema,
}: {
dynamicConfig: PluginInterfaceDynamicConfig
dynamicConfigSchema: PluginDynamicConfigSchema
): UserPluginConfigMap => {
}): UserPluginConfigMap => {
const result = new Map<string, PluginDynamicConfigPrimitiveType>()

const flatten = (
record: PluginInterfaceDynamicConfig,
prefix: string,
Expand All @@ -347,14 +350,20 @@ const FlattenRecord = (
} else {
const targetSchema = dynamicConfigSchema?.[key]
if (!targetSchema) return
result.set(`${prefix}${key}`, value)

const decodedValue: PluginDynamicConfigPrimitiveType = Decoder(
targetSchema,
value
)
result.set(`${prefix}${key}`, decodedValue)
}
}
}

flatten(record, '', dynamicConfigSchema)
flatten(dynamicConfig, '', dynamicConfigSchema)
return result
}

interface PluginViewProps {
config: PluginConfig
configStorage: PluginConfigStorage
Expand Down Expand Up @@ -399,7 +408,10 @@ const PluginView = ({ config, configStorage }: PluginViewProps) => {
const [userDynamicConfig, setUserDynamicConfig] =
useState<UserPluginConfigMap>(() => {
if (!dynamicConfigSchema) return new Map()
return FlattenRecord(initialConfig, dynamicConfigSchema)
return FlattenDynamicConfig({
dynamicConfig: initialConfig,
dynamicConfigSchema,
})
})

const configSetter: UserPluginConfigSetter = useCallback(
Expand Down

0 comments on commit 1ae0839

Please sign in to comment.