File size: 1,998 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { computed, ref } from 'vue'
import type { PluginDescriptor } from '@vue/devtools-api'
import type { Bridge } from '@vue-devtools/shared-utils'
import { BridgeEvents } from '@vue-devtools/shared-utils'
import { getBridge } from '@front/features/bridge'
import { useCurrentApp } from '@front/features/apps'

export interface Plugin {
  id: string
  label: string
  appId: string
  packageName: string
  homepage: string
  logo: string
  componentStateTypes: string[]
  settingsSchema?: PluginDescriptor['settings']
}

interface PluginsPerApp {
  [appId: string]: Plugin[]
}

const pluginsPerApp = ref<PluginsPerApp>({})

function getPlugins(appId: string) {
  let plugins = pluginsPerApp.value[appId]
  if (!plugins) {
    plugins = []
    pluginsPerApp.value[appId] = plugins
    // Read the property again to make it reactive
    plugins = pluginsPerApp.value[appId]
  }
  return plugins
}

function fetchPlugins() {
  getBridge().send(BridgeEvents.TO_BACK_DEVTOOLS_PLUGIN_LIST, {})
}

export function usePlugins() {
  const { currentAppId } = useCurrentApp()

  const plugins = computed(() => getPlugins(currentAppId.value))

  return {
    plugins,
  }
}

export function useComponentStateTypePlugin() {
  const { plugins } = usePlugins()

  function getStateTypePlugin(type: string) {
    return plugins.value.find(p => p.componentStateTypes?.includes(type))
  }

  return {
    getStateTypePlugin,
  }
}

function addPlugin(plugin: Plugin) {
  const list = getPlugins(plugin.appId)
  const index = list.findIndex(p => p.id === plugin.id)
  if (index !== -1) {
    list.splice(index, 1, plugin)
  }
  else {
    list.push(plugin)
  }
}

export function setupPluginsBridgeEvents(bridge: Bridge) {
  bridge.on(BridgeEvents.TO_FRONT_DEVTOOLS_PLUGIN_ADD, async ({ plugin }) => {
    await addPlugin(plugin)
  })

  bridge.on(BridgeEvents.TO_FRONT_DEVTOOLS_PLUGIN_LIST, async ({ plugins }) => {
    for (const plugin of plugins) {
      await addPlugin(plugin)
    }
  })

  fetchPlugins()
}