Files
ServerKit/frontend/src/components/shared/EnvironmentVariablesPanel.jsx
T
Juan DenisandClaude Opus 4.8 478b24a3cb feat: Phase 3 team & scale — projects/environments, shared resources, PR previews, proxy stacks
- Project / Environment hierarchy (C2): Workspace -> Project -> Environment
  org model. New Project/Environment models + CRUD APIs, nullable
  applications.project_id/environment_id (opt-in, no destructive backfill),
  ProjectService.ensure_default, a Projects list + ProjectDetail (env tabs),
  a project/environment selector in the create wizard, and sidebar nav.

- Polymorphic shared resources facade (C9): ResourceTag + SharedVariableGroup
  /SharedVariable/attachment models keyed by (resource_type, resource_id),
  reusing the env-var Fernet encryption; SharedResourceService resolves merged
  variables (most-recent attachment wins) with secrets masked. /api/v1/shared
  endpoints + reusable TagsPanel/EnvironmentVariablesPanel + a Shared Variables
  management page. Existing per-resource tables untouched (facade first).

- PR preview environments (C3): ApplicationPreview + settings models, a
  PreviewService with pure domain-templating + reconcile (open PRs vs active
  previews) cores and best-effort provisioning (incl. guarded WordPress
  file+DB clone), a pull_request webhook endpoint, preview.* jobs, and a
  Previews tab on the app detail page.

- Per-server managed proxy stack (C6): opt-in Dockerized Traefik/Caddy as a
  Compose stack (host Nginx stays default and superior for PHP/WordPress).
  ProxyStack model + ProxyStackService with pure compose/config generators and
  versioned config backups, /api/v1/servers/<id>/proxy endpoints, and a Proxy
  tab on the server detail page.

Migrations 031-034 add the new tables/columns; the full chain applies cleanly
from an empty DB. Full backend suite: 871 passed, 14 skipped; frontend builds.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:33:44 -04:00

106 lines
4.1 KiB
React

import { useState, useEffect, useCallback } from 'react';
import api from '../../services/api';
import { useToast } from '../../contexts/ToastContext';
/**
* Read-mostly facade showing the *resolved* shared variables for a resource —
* i.e. the merged effective set from every shared variable group attached to it.
* Secret values arrive pre-masked from the backend; this panel never reveals
* them. Editing happens in SharedVariableGroups (the group is the source of
* truth); this surface is the read-side facade meant to be embedded on a
* resource's detail page.
*
* Props:
* resourceType one of SharedResourceService.RESOURCE_TYPES
* resourceId the resource's id (number or string)
*/
const EnvironmentVariablesPanel = ({ resourceType, resourceId }) => {
const toast = useToast();
const [variables, setVariables] = useState([]);
const [groups, setGroups] = useState([]);
const [loading, setLoading] = useState(true);
const [filter, setFilter] = useState('');
const load = useCallback(async () => {
if (!resourceType || resourceId == null) return;
try {
setLoading(true);
const data = await api.getResolvedVariables(resourceType, resourceId);
setVariables(data.variables || []);
setGroups(data.groups || []);
} catch (err) {
toast.error('Failed to load shared variables');
console.error('Failed to load resolved variables:', err);
} finally {
setLoading(false);
}
}, [resourceType, resourceId, toast]);
useEffect(() => { load(); }, [load]);
const filtered = filter
? variables.filter((v) => v.key.toLowerCase().includes(filter.toLowerCase()))
: variables;
if (loading) {
return <div className="shared-vars-panel shared-vars-panel--loading">Loading shared variables…</div>;
}
return (
<div className="shared-vars-panel">
<div className="shared-vars-panel__header">
<h3>Shared Variables</h3>
<span className="shared-vars-panel__count">
{variables.length} resolved · {groups.length} group{groups.length !== 1 ? 's' : ''}
</span>
</div>
<p className="shared-vars-panel__hint">
Effective variables merged from every shared group attached to this
resource. Later-attached groups override earlier ones on key
collisions. Secret values are masked.
</p>
{variables.length > 5 && (
<div className="shared-vars-panel__filter">
<input
type="text"
value={filter}
onChange={(e) => setFilter(e.target.value)}
placeholder="Filter variables…"
/>
</div>
)}
{filtered.length === 0 ? (
<div className="shared-vars-panel__empty">
{filter
? 'No matching variables'
: 'No shared variable groups attached to this resource yet.'}
</div>
) : (
<table className="shared-vars-table">
<thead>
<tr>
<th>Key</th>
<th>Value</th>
<th>Source group</th>
</tr>
</thead>
<tbody>
{filtered.map((v) => (
<tr key={v.key} className={v.is_secret ? 'is-secret' : ''}>
<td className="shared-vars-table__key">{v.key}</td>
<td className="shared-vars-table__value">{v.value}</td>
<td className="shared-vars-table__group">{v.group_name}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
);
};
export default EnvironmentVariablesPanel;