Files
ServerKit/frontend/src/components/previews/PreviewList.jsx
T
Juan DenisandClaude Opus 4.8 3fcb128cb3 refactor(frontend): Phase 3 §6.6 — remove the useConfirm-driven local ConfirmDialogs
Follows the ConfirmProvider checkpoint. Removed the now-inert per-page
<ConfirmDialog> instances that were driven by useConfirm's confirmState across
18 files (ApplicationDetail, Docker, ServerDetail, Terminal, WordPressDetail,
Backups, CronJobs, Databases, Queue*, SSLCertificates, ServiceDetail,
ContainerOpsPanel, ProtectionPanel, BackupsTab, PreviewList, PackagesTab,
MigrationHistoryTab); simplified each useConfirm() destructure to { confirm } and
dropped the unused ConfirmDialog imports. confirm() now renders only via the
app-level provider.

~20 other files keep their own local-useState-controlled ConfirmDialogs (not
useConfirm-driven) — intentionally left; migrating those is a separate behavior
change.

Verified: npm run build passes; no dangling confirmState refs; Docker page
renders.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 02:50:59 -04:00

267 lines
11 KiB
React

import { useState, useEffect, useCallback } from 'react';
import {
GitPullRequest, ExternalLink, RefreshCw, Trash2, RotateCcw,
} from 'lucide-react';
import api from '../../services/api';
import { useToast } from '../../contexts/ToastContext';
import { useConfirm } from '../../hooks/useConfirm';
import EmptyState from '../EmptyState';
import { Pill } from '../ds';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
// Preview status → ds Pill kind.
const STATUS_PILL = {
running: 'green',
building: 'amber',
queued: 'amber',
stopped: 'gray',
failed: 'red',
destroyed: 'gray',
};
const DEFAULT_TEMPLATE = 'pr-{pr_number}.{app_domain}';
const PreviewList = ({ appId }) => {
const toast = useToast();
const { confirm } = useConfirm();
const [previews, setPreviews] = useState([]);
const [settings, setSettings] = useState(null);
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
const [syncing, setSyncing] = useState(false);
const [busyId, setBusyId] = useState(null);
const [template, setTemplate] = useState(DEFAULT_TEMPLATE);
const load = useCallback(async () => {
try {
const [list, conf] = await Promise.all([
api.getPreviews(appId),
api.getPreviewSettings(appId),
]);
setPreviews(list?.previews || []);
setSettings(conf || null);
setTemplate(conf?.domain_template || DEFAULT_TEMPLATE);
} catch (err) {
console.error('Failed to load previews:', err);
toast?.error?.('Could not load PR previews');
} finally {
setLoading(false);
}
}, [appId, toast]);
useEffect(() => { load(); }, [load]);
async function saveSettings(patch) {
setSaving(true);
try {
const updated = await api.updatePreviewSettings(appId, patch);
setSettings(updated);
setTemplate(updated?.domain_template || DEFAULT_TEMPLATE);
toast?.success?.('Preview settings saved');
} catch (err) {
console.error('Failed to save preview settings:', err);
toast?.error?.(err.message || 'Could not save settings');
} finally {
setSaving(false);
}
}
async function handleToggle(enabled) {
await saveSettings({ enabled });
}
async function handleSaveTemplate() {
await saveSettings({ domain_template: template || DEFAULT_TEMPLATE });
}
async function handleSync() {
setSyncing(true);
try {
await api.syncPreviews(appId);
toast?.success?.('Reconciled previews against open PRs');
await load();
} catch (err) {
toast?.error?.(err.message || 'Sync failed');
} finally {
setSyncing(false);
}
}
async function handleRedeploy(preview) {
setBusyId(preview.id);
try {
await api.redeployPreview(appId, preview.id);
toast?.success?.(`Redeploying preview for PR #${preview.pr_number}`);
await load();
} catch (err) {
toast?.error?.(err.message || 'Redeploy failed');
} finally {
setBusyId(null);
}
}
async function handleDestroy(preview) {
const ok = await confirm({
title: 'Destroy preview',
message: `Tear down the preview environment for PR #${preview.pr_number}? This removes its temporary domain and resources.`,
confirmText: 'Destroy',
variant: 'danger',
});
if (!ok) return;
setBusyId(preview.id);
try {
await api.destroyPreview(appId, preview.id);
toast?.success?.(`Preview for PR #${preview.pr_number} destroyed`);
await load();
} catch (err) {
toast?.error?.(err.message || 'Destroy failed');
} finally {
setBusyId(null);
}
}
if (loading) {
return <EmptyState loading title="Loading previews..." />;
}
const enabled = !!settings?.enabled;
return (
<div className="preview-list">
{/* Enable / configure card */}
<div className="preview-settings">
<div className="preview-settings__header">
<div className="preview-settings__title">
<GitPullRequest size={18} />
<div>
<h3>PR preview environments</h3>
<p className="preview-settings__hint">
Deploy an isolated preview of each open pull request to a
temporary domain, and tear it down when the PR closes.
</p>
</div>
</div>
<div className="preview-settings__toggle">
<Label htmlFor="preview-enabled">{enabled ? 'Enabled' : 'Disabled'}</Label>
<Switch
id="preview-enabled"
checked={enabled}
disabled={saving}
onCheckedChange={handleToggle}
/>
</div>
</div>
{enabled && (
<div className="preview-settings__template">
<Label htmlFor="preview-template">Domain template</Label>
<div className="preview-settings__template-row">
<Input
id="preview-template"
value={template}
onChange={(e) => setTemplate(e.target.value)}
placeholder={DEFAULT_TEMPLATE}
spellCheck={false}
/>
<Button
variant="outline"
onClick={handleSaveTemplate}
disabled={saving || template === settings?.domain_template}
>
Save
</Button>
</div>
<p className="preview-settings__hint">
Placeholders: <code>{'{pr_number}'}</code>, <code>{'{branch}'}</code>,{' '}
<code>{'{app_domain}'}</code>.
</p>
</div>
)}
</div>
{/* Active previews */}
<div className="preview-list__toolbar">
<h4>Active previews</h4>
<Button variant="ghost" onClick={handleSync} disabled={syncing}>
<RefreshCw size={15} className={syncing ? 'spin' : undefined} />
Sync with open PRs
</Button>
</div>
{previews.length === 0 ? (
<EmptyState
icon={GitPullRequest}
title="No active previews"
description={
enabled
? 'Open a pull request to spin up a preview, or sync to reconcile now.'
: 'Enable PR previews above to start deploying preview environments.'
}
/>
) : (
<ul className="preview-list__items">
{previews.map((p) => (
<li key={p.id} className="preview-item">
<div className="preview-item__main">
<div className="preview-item__title">
<span className="preview-item__pr">PR #{p.pr_number}</span>
<span className="preview-item__name">{p.pr_title || '(untitled)'}</span>
<Pill kind={STATUS_PILL[p.status] || 'gray'}>{p.status}</Pill>
</div>
<div className="preview-item__meta">
{p.branch && <span className="preview-item__branch mono">{p.branch}</span>}
{p.short_sha && <span className="preview-item__sha mono">{p.short_sha}</span>}
{p.url && (
<a
className="preview-item__url"
href={p.url}
target="_blank"
rel="noopener noreferrer"
>
{p.domain}
</a>
)}
</div>
</div>
<div className="preview-item__actions">
{p.url && (
<Button variant="ghost" size="sm" asChild>
<a href={p.url} target="_blank" rel="noopener noreferrer">
<ExternalLink size={15} />
Open
</a>
</Button>
)}
<Button
variant="ghost"
size="sm"
onClick={() => handleRedeploy(p)}
disabled={busyId === p.id}
>
<RotateCcw size={15} />
Redeploy
</Button>
<Button
variant="ghost"
size="sm"
onClick={() => handleDestroy(p)}
disabled={busyId === p.id}
>
<Trash2 size={15} />
Destroy
</Button>
</div>
</li>
))}
</ul>
)}
</div>
);
};
export default PreviewList;