All files / src/components/features/admin/api-docs EnvironmentSelector.tsx

0% Statements 0/227
100% Branches 0/0
0% Functions 0/1
0% Lines 0/227

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
'use client';

/**
 * Environment Selector Component
 * Switch between different API environments (local, staging, production)
 */

import React, { useState } from 'react';
import { ApiEnvironment } from '@/types/api-docs';
import {
  getEnvironments,
  saveEnvironments,
  getCurrentEnvironment,
  setCurrentEnvironment } from '@/lib/api-docs/executor';
import { Icon } from '@/components/ui/icons';

interface EnvironmentSelectorProps {
  onEnvironmentChange?: (env: ApiEnvironment) => void;
}

export default function EnvironmentSelector({ onEnvironmentChange }: EnvironmentSelectorProps) {
  // Use lazy initialization to load from localStorage only once
  const [environments, setEnvironments] = useState<ApiEnvironment[]>(() => getEnvironments());
  const [currentEnv, setCurrentEnv] = useState<ApiEnvironment | null>(() => getCurrentEnvironment());
  const [showEditor, setShowEditor] = useState(false);
  const [editingEnv, setEditingEnv] = useState<ApiEnvironment | null>(null);

  const handleSelectEnv = (env: ApiEnvironment) => {
    setCurrentEnvironment(env);
    setCurrentEnv(env);
    onEnvironmentChange?.(env);
  };

  const handleSaveEnv = (env: ApiEnvironment) => {
    const updated = environments.map((e) => (e.id === env.id ? env : e));
    saveEnvironments(updated);
    setEnvironments(updated);
    setEditingEnv(null);
  };

  const handleAddEnv = () => {
    const newEnv: ApiEnvironment = {
      id: `env-${Date.now()}`,
      name: 'New Environment',
      baseUrl: ''};
    const updated = [...environments, newEnv];
    saveEnvironments(updated);
    setEnvironments(updated);
    setEditingEnv(newEnv);
  };

  const handleDeleteEnv = (id: string) => {
    const updated = environments.filter((e) => e.id !== id);
    saveEnvironments(updated);
    setEnvironments(updated);
    if (currentEnv?.id === id && updated.length > 0) {
      handleSelectEnv(updated[0]);
    }
  };

  return (
    <div className="relative">
      {/* Current Environment Display */}
      <button
        onClick={() => setShowEditor(!showEditor)}
        className="flex items-center gap-2 px-3 py-1.5 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors"
      >
        <span
          className={`w-2 h-2 rounded-full ${
            currentEnv?.baseUrl ? 'bg-green-500' : 'bg-yellow-500'
          }`}
        />
        <span className="text-sm font-medium text-gray-700 dark:text-gray-300">
          {currentEnv?.name || 'Local'}
        </span>
        <Icon
          name="chevron-down"
          size={16}
          className={`text-gray-500 dark:text-gray-400 transition-transform ${
            showEditor ? 'rotate-180' : ''
          }`}
        />
      </button>

      {/* Environment Dropdown */}
      {showEditor && (
        <div className="absolute top-full right-0 mt-2 w-80 bg-white dark:bg-gray-800 rounded-lg shadow-xl border border-gray-200 dark:border-gray-700 z-50">
          <div className="p-3 border-b border-gray-200 dark:border-gray-700">
            <div className="flex items-center justify-between">
              <h4 className="font-medium text-gray-900 dark:text-gray-100">Environments</h4>
              <button
                onClick={handleAddEnv}
                className="text-indigo-600 dark:text-indigo-400 hover:text-indigo-700 dark:hover:text-indigo-300 text-sm"
              >
                + Add
              </button>
            </div>
          </div>

          <div className="max-h-64 overflow-y-auto">
            {environments.map((env) => (
              <div key={env.id} className="border-b border-gray-100 dark:border-gray-700 last:border-0">
                {editingEnv?.id === env.id ? (
                  <EnvEditor
                    env={editingEnv}
                    onSave={handleSaveEnv}
                    onCancel={() => setEditingEnv(null)}
                  />
                ) : (
                  <div
                    className={`flex items-center justify-between p-3 hover:bg-gray-50 dark:hover:bg-gray-700/50 cursor-pointer ${
                      currentEnv?.id === env.id ? 'bg-indigo-50 dark:bg-indigo-900/30' : ''
                    }`}
                    onClick={() => handleSelectEnv(env)}
                  >
                    <div className="flex-1">
                      <div className="flex items-center gap-2">
                        <span
                          className={`w-2 h-2 rounded-full ${
                            env.baseUrl ? 'bg-green-500' : 'bg-yellow-500'
                          }`}
                        />
                        <span className="font-medium text-gray-900 dark:text-gray-100 text-sm">
                          {env.name}
                        </span>
                        {currentEnv?.id === env.id && (
                          <span className="text-xs text-indigo-600 dark:text-indigo-400">(active)</span>
                        )}
                      </div>
                      <div className="text-xs text-gray-500 dark:text-gray-400 mt-1 font-mono truncate">
                        {env.baseUrl || 'Using current origin'}
                      </div>
                    </div>
                    <div className="flex items-center gap-1">
                      <button
                        onClick={(e) => {
                          e.stopPropagation();
                          setEditingEnv(env);
                        }}
                        className="p-1.5 text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-300"
                      >
                        <Icon name="edit" size={16} />
                      </button>
                      {!env.isDefault && (
                        <button
                          onClick={(e) => {
                            e.stopPropagation();
                            handleDeleteEnv(env.id);
                          }}
                          className="p-1.5 text-gray-400 dark:text-gray-500 hover:text-red-500 dark:hover:text-red-400"
                        >
                          <Icon name="trash" size={16} />
                        </button>
                      )}
                    </div>
                  </div>
                )}
              </div>
            ))}
          </div>

          <div className="p-2 border-t border-gray-200 dark:border-gray-700 text-xs text-gray-500 dark:text-gray-400 text-center">
            Environment URLs override the request base URL
          </div>
        </div>
      )}

      {/* Backdrop */}
      {showEditor && (
        <div className="fixed inset-0 z-40" onClick={() => setShowEditor(false)} />
      )}
    </div>
  );
}

function EnvEditor({
  env,
  onSave,
  onCancel}: {
  env: ApiEnvironment;
  onSave: (env: ApiEnvironment) => void;
  onCancel: () => void;
}) {
  const [name, setName] = useState(env.name);
  const [baseUrl, setBaseUrl] = useState(env.baseUrl);

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    onSave({ ...env, name, baseUrl });
  };

  return (
    <form onSubmit={handleSubmit} className="p-3 bg-gray-50 dark:bg-gray-700/50">
      <div className="space-y-2">
        <input
          type="text"
          value={name}
          onChange={(e) => setName(e.target.value)}
          placeholder="Environment name"
          className="w-full px-2 py-1.5 text-sm border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-1 focus:ring-indigo-500"
        />
        <input
          type="text"
          value={baseUrl}
          onChange={(e) => setBaseUrl(e.target.value)}
          placeholder="https://api.example.com (leave empty for current)"
          className="w-full px-2 py-1.5 text-sm border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 font-mono focus:outline-none focus:ring-1 focus:ring-indigo-500"
        />
      </div>
      <div className="flex justify-end gap-2 mt-2">
        <button
          type="button"
          onClick={onCancel}
          className="px-2 py-1 text-xs text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100"
        >
          Cancel
        </button>
        <button
          type="submit"
          className="px-2 py-1 text-xs bg-indigo-600 text-white rounded hover:bg-indigo-700"
        >
          Save
        </button>
      </div>
    </form>
  );
}