/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import { lazy, ComponentType } from 'react'; import { JsonObject } from '@superset-ui/core'; export interface RegistryMetadata { description: string; name: string; } export interface ComponentItem { metadata: Metadata; loadComponent: () => Promise<{ default: ComponentType }>; } export interface ComponentRegistry { metadata: Metadata; Component: ComponentType; } export type FunctionalRegistryState = { registry: { [key: string]: RegistryT & { key: string } }; registryKeys: string[]; }; export const registryGetAll = ({ registryKeys, registry }: FunctionalRegistryState) => () => registryKeys.map(key => registry[key]); export const registryDelete = ({ registryKeys, registry }: FunctionalRegistryState) => (keyToDelete: string) => { // eslint-disable-next-line no-param-reassign registryKeys = registryKeys.filter(key => key !== keyToDelete); // eslint-disable-next-line no-param-reassign delete registry[keyToDelete]; }; export const registryGet = ({ registry }: FunctionalRegistryState) => (key: string) => registry[key]; export const registrySet = ({ registryKeys, registry }: FunctionalRegistryState) => (key: string, item: JsonObject) => { registryKeys.push(key); // eslint-disable-next-line no-param-reassign registry[key] = { key, ...item, }; }; export const registrySetComponent = ({ registryKeys, registry }: FunctionalRegistryState) => (key: string, item: ComponentItem) => { registryKeys.push(key); // eslint-disable-next-line no-param-reassign registry[key] = { key, metadata: item.metadata, Component: lazy(item.loadComponent), }; };