import { registerSidebarEntry } from '@kinvolk/headlamp-plugin/lib';
import React from 'react';
import { getNamespaces } from '../api/polaris';
import { usePolarisDataContext } from '../api/PolarisDataContext';

const registeredNamespaces = new Set<string>();

export default function DynamicSidebarRegistrar() {
  const { data } = usePolarisDataContext();

  React.useEffect(() => {
    if (!data) return;

    const namespaces = getNamespaces(data);
    for (const ns of namespaces) {
      if (registeredNamespaces.has(ns)) continue;
      registeredNamespaces.add(ns);
      registerSidebarEntry({
        parent: 'polaris-namespaces',
        name: `polaris-ns-${ns}`,
        label: ns,
        url: `/polaris/ns/${ns}`,
        icon: 'mdi:folder-outline',
      });
    }
  }, [data]);

  return null;
}
