Commit 9a218568 authored by Labros Papadopoulos's avatar Labros Papadopoulos
Browse files

fix: kubeconfig copy button

parent 3d54b13f
Loading
Loading
Loading
Loading
+47 −14
Changes for components/registries/registries-kubeconfig-table.tsx: 47 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -9,11 +9,51 @@ import {
  Spinner,
  Input,
  Snippet,
  Button,
} from '@nextui-org/react';
import { RegistrySecretData } from '@/services/registries-api';
import { IoFilter } from 'react-icons/io5';
import { IoFilter, IoCopyOutline, IoCheckmark } from 'react-icons/io5';
import { fetchKubeConfigSecrets } from '@/actions/registries';

const copyToClipboard = async (text: string): Promise<boolean> => {
  try {
    if (navigator.clipboard && window.isSecureContext) {
      await navigator.clipboard.writeText(text);
      return true;
    }
    // Fallback for plain HTTP
    const ta = document.createElement('textarea');
    ta.value = text;
    ta.style.position = 'fixed';
    ta.style.opacity = '0';
    document.body.appendChild(ta);
    ta.focus();
    ta.select();
    const ok = document.execCommand('copy');
    document.body.removeChild(ta);
    return ok;
  } catch (e) {
    console.error('Copy failed', e);
    return false;
  }
};

const CopyButton: React.FC<{ text: string }> = ({ text }) => {
  const [copied, setCopied] = useState(false);

  const handleClick = async () => {
    if (await copyToClipboard(text)) {
      setCopied(true);
      setTimeout(() => setCopied(false), 1500);
    }
  };

  return (
    <Button isIconOnly size="sm" variant="light" isDisabled={!text} onPress={handleClick}>
      {copied ? <IoCheckmark /> : <IoCopyOutline />}
    </Button>
  );
};
const columns = [
  { name: 'Kubernetes Service ID ', uid: 'secretPath' },
  { name: 'Kube conf', uid: 'serviceId' },
@@ -64,19 +104,12 @@ const RegistriesKubeconfigTable: React.FC = () => {
        case 'serviceId': {
          const config = registry.secretData.kubeConfig || '';
          return (
            <Snippet
              symbol=""
              variant="flat"
              codeString={config}
              classNames={{
                base: 'p-0 bg-transparent gap-2',
                pre: 'font-sans text-sm font-normal text-inherit truncate max-w-[250px]',
                copyButton: 'text-maestro-blue-80 min-w-unit-8 w-unit-8 h-unit-8',
              }}
            >
              {/* We only show a preview of the text */}
              {config.substring(0, 20000)}
            </Snippet>
            <div className="flex items-center gap-2">
              <span className="max-w-[250px] truncate font-sans text-sm">
                {config.substring(0, 100)}
              </span>
              <CopyButton text={config} />
            </div>
          );
        }
        default: