All files / src/apps/developer/pages Settings.tsx

65.38% Statements 17/26
92.85% Branches 13/14
66.66% Functions 8/12
62.5% Lines 15/24

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                    3x 3x           5x 5x                   1x           5x 5x         5x                                                     1x             3x 3x       3x                           6x                     1x 1x                                                                                                                                                                                                                                            
import React, { useState } from "react";
import MainPage from "@/components/page/MainPage/MainPage";
import { Globe, Zap, Shield, AlertCircle } from "lucide-react";
import {
  SectionCard,
} from "@/components/detail";
 
// ─── Shared primitives (mirrors user settings style) ──────────────────────────
 
function getSetting(key: string, fallback: string): string {
  try {
    return localStorage.getItem(`dev:settings:${key}`) ?? fallback;
  } catch {
    return fallback;
  }
}
function getBoolSetting(key: string, fallback: boolean): boolean {
  const v = localStorage.getItem(`dev:settings:${key}`);
  return v === null ? fallback : v === "true";
}
function setSetting(key: string, value: string | boolean) {
  try {
    localStorage.setItem(`dev:settings:${key}`, String(value));
  } catch {
    /* ignore */
  }
}
 
const ToggleRow: React.FC<{
  label: string;
  description?: string;
  settingKey: string;
  defaultValue?: boolean;
}> = ({ label, description, settingKey, defaultValue = false }) => {
  const [checked, setChecked] = useState(() => getBoolSetting(settingKey, defaultValue));
  const toggle = () => {
    const next = !checked;
    setChecked(next);
    setSetting(settingKey, next);
  };
  return (
    <button
      type="button"
      onClick={toggle}
      className="w-full flex items-center justify-between px-5 py-3.5 hover:bg-gray-50 dark:hover:bg-gray-700/30 transition-colors text-left"
    >
      <div>
        <div className="text-sm font-medium text-gray-700 dark:text-gray-300">{label}</div>
        {description && (
          <div className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">{description}</div>
        )}
      </div>
      <div
        className={`relative inline-flex h-5 w-9 shrink-0 items-center rounded-full transition-colors ml-4 ${
          checked ? "bg-gray-900 dark:bg-gray-100" : "bg-gray-200 dark:bg-gray-600"
        }`}
      >
        <span
          className={`inline-block size-3.5 rounded-full bg-white dark:bg-gray-900 shadow transition-transform ${
            checked ? "translate-x-4" : "translate-x-0.5"
          }`}
        />
      </div>
    </button>
  );
};
 
const SelectRow: React.FC<{
  label: string;
  description?: string;
  settingKey: string;
  options: { label: string; value: string }[];
  defaultValue?: string;
}> = ({ label, description, settingKey, options, defaultValue = "" }) => {
  const [value, setValue] = useState(() => getSetting(settingKey, defaultValue));
  const handleChange = (v: string) => {
    setValue(v);
    setSetting(settingKey, v);
  };
  return (
    <div className="flex items-center justify-between px-5 py-3.5">
      <div className="min-w-0 mr-6">
        <div className="text-sm font-medium text-gray-700 dark:text-gray-300">{label}</div>
        {description && (
          <div className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">{description}</div>
        )}
      </div>
      <select
        value={value}
        onChange={(e) => handleChange(e.target.value)}
        className="shrink-0 px-3 py-1.5 text-sm rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 focus:outline-none focus:ring-2 focus:ring-gray-300 dark:focus:ring-gray-500"
      >
        {options.map((o) => (
          <option key={o.value} value={o.value}>
            {o.label}
          </option>
        ))}
      </select>
    </div>
  );
};
 
// ─── DeveloperSettings ────────────────────────────────────────────────────────
 
const DeveloperSettings: React.FC = () => (
  <MainPage
    title="Developer Settings"
    description="Workspace-level preferences for your developer integration."
  >
    <div className="max-w-2xl space-y-4">
      {/* API */}
      <SectionCard variant="settings"
        icon={Globe}
        title="API"
        description="Control how your workspace interfaces with the API."
      >
        <SelectRow
          label="API Version"
          description="The API version used by default for all requests from this workspace."
          settingKey="api_version"
          defaultValue="v1"
          options={[
            { label: "v1 (stable)", value: "v1" },
            { label: "v1-beta (preview)", value: "v1-beta" },
          ]}
        />
        <SelectRow
          label="Default Response Format"
          description="Preferred format for API responses."
          settingKey="response_format"
          defaultValue="json"
          options={[
            { label: "JSON", value: "json" },
            { label: "JSON (pretty-printed)", value: "json_pretty" },
          ]}
        />
        <ToggleRow
          label="Enable Request Logging"
          description="Log all outgoing API requests for debugging purposes."
          settingKey="request_logging"
          defaultValue={false}
        />
      </SectionCard>
 
      {/* Rate limiting */}
      <SectionCard variant="settings"
        icon={Zap}
        title="Rate Limiting"
        description="Configure request throttling behaviour."
      >
        <SelectRow
          label="Rate Limit Strategy"
          description="How to handle requests that exceed your plan's rate limits."
          settingKey="rate_limit_strategy"
          defaultValue="queue"
          options={[
            { label: "Queue (retry after delay)", value: "queue" },
            { label: "Reject (return 429 immediately)", value: "reject" },
          ]}
        />
        <ToggleRow
          label="Rate Limit Alerts"
          description="Receive notifications when you approach your usage limits."
          settingKey="rate_limit_alerts"
          defaultValue={true}
        />
      </SectionCard>
 
      {/* Security */}
      <SectionCard variant="settings"
        icon={Shield}
        title="Security"
        description="Developer-specific security preferences."
      >
        <ToggleRow
          label="Require HTTPS"
          description="Reject any API requests made over plain HTTP."
          settingKey="require_https"
          defaultValue={true}
        />
        <ToggleRow
          label="IP Allowlist Enforcement"
          description="Restrict API key usage to pre-approved IP ranges."
          settingKey="ip_allowlist"
          defaultValue={false}
        />
        <ToggleRow
          label="Audit Log API Access"
          description="Write every API authentication event to the audit log."
          settingKey="audit_api_access"
          defaultValue={true}
        />
      </SectionCard>
 
      {/* Danger zone */}
      <div className="rounded-xl border border-red-200 dark:border-red-900 bg-white dark:bg-gray-800 shadow-sm overflow-hidden">
        <div className="flex items-center gap-2 px-5 py-3.5 border-b border-red-100 dark:border-red-900/50">
          <AlertCircle className="size-4 text-red-500 dark:text-red-400 shrink-0" />
          <h3 className="text-sm font-semibold text-red-600 dark:text-red-400">Danger Zone</h3>
        </div>
        <div className="px-5 py-4 flex items-center justify-between">
          <div>
            <div className="text-sm font-medium text-gray-700 dark:text-gray-300">
              Revoke all API keys
            </div>
            <div className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">
              Immediately invalidate every API key in this workspace. This cannot be undone.
            </div>
          </div>
          <button
            type="button"
            disabled
            className="ml-6 shrink-0 px-3 py-2 rounded-lg border border-red-200 dark:border-red-800 text-red-600 dark:text-red-400 text-sm font-medium opacity-40 cursor-not-allowed"
          >
            Revoke All
          </button>
        </div>
      </div>
    </div>
  </MainPage>
);
 
export default DeveloperSettings;