|
| 1 | +/* |
| 2 | + * Copyright © 2024 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { GLOBALS } from 'services/global-constants'; |
| 18 | +import MySettingsService from 'components/StudioV2/utils/settings'; |
| 19 | +import { IPlugin } from 'components/StudioV2/types'; |
| 20 | +import StudioV2Store from '..'; |
| 21 | + |
| 22 | +const PREFIX = 'PLUGINS_ACTIONS'; |
| 23 | + |
| 24 | +export const PluginsActions = { |
| 25 | + FETCH_PLUGINS: `${PREFIX}/FETCH_PLUGINS`, |
| 26 | + FETCH_ALL_PLUGINS: `${PREFIX}/FETCH_ALL_PLUGINS`, |
| 27 | + FETCH_PLUGIN_TEMPLATE: `${PREFIX}/FETCH_PLUGIN_TEMPLATE`, |
| 28 | + FETCH_PLUGINS_DEFAULT_VERSIONS: `${PREFIX}/FETCH_PLUGINS_DEFAULT_VERSIONS`, |
| 29 | + UPDATE_PLUGINS_DEFAULT_VERSIONS: `${PREFIX}/UPDATE_PLUGINS_DEFAULT_VERSIONS`, |
| 30 | + CHECK_AND_UPDATE_PLUGIN_DEFAULT_VERSION: `${PREFIX}/CHECK_AND_UPDATE_PLUGIN_DEFAULT_VERSION`, |
| 31 | + FETCH_EXTENSIONS: `${PREFIX}/FETCH_EXTENSIONS`, |
| 32 | + RESET: `${PREFIX}/RESET`, |
| 33 | +}; |
| 34 | + |
| 35 | +export const keepUiSupportedExtensions = (pipelineType: string) => (extension: string) => { |
| 36 | + const extensionMap = GLOBALS.pluginTypes[pipelineType]; |
| 37 | + return Object.keys(extensionMap).filter((ext) => extensionMap[ext] === extension).length; |
| 38 | +}; |
| 39 | + |
| 40 | +export async function fetchPluginsDefaultVersions() { |
| 41 | + try { |
| 42 | + const pluginDefalutVersion = await MySettingsService.getInstance().get( |
| 43 | + 'plugin-default-version' |
| 44 | + ); |
| 45 | + if (!pluginDefalutVersion) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + StudioV2Store.dispatch({ |
| 50 | + type: PluginsActions.FETCH_PLUGINS_DEFAULT_VERSIONS, |
| 51 | + payload: pluginDefalutVersion, |
| 52 | + }); |
| 53 | + } catch (err) { |
| 54 | + return; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export async function updatePluginDefaultVersion(plugin: IPlugin) { |
| 59 | + try { |
| 60 | + let pluginDefalutVersion = |
| 61 | + (await MySettingsService.getInstance().get('plugin-default-version')) || {}; |
| 62 | + const key = `${plugin.name}-${plugin.type}-${plugin.artifact.name}`; |
| 63 | + pluginDefalutVersion[key] = plugin.artifact; |
| 64 | + await MySettingsService.getInstance().set('plugin-default-version', pluginDefalutVersion); |
| 65 | + pluginDefalutVersion = await MySettingsService.getInstance().get('plugin-default-version'); |
| 66 | + |
| 67 | + if (!pluginDefalutVersion) { |
| 68 | + return; |
| 69 | + } |
| 70 | + StudioV2Store.dispatch({ |
| 71 | + type: PluginsActions.FETCH_PLUGINS_DEFAULT_VERSIONS, |
| 72 | + payload: pluginDefalutVersion, |
| 73 | + }); |
| 74 | + } catch (err) { |
| 75 | + return; |
| 76 | + } |
| 77 | +} |
0 commit comments