- add CodeBlock for read-only syntax-highlighted display (hljs, js/json) - add CodeEditor wrapping Monaco editor with theme sync, focus state, and error state - replace code textareas in snippet, credential, and step pages with CodeEditor - replace code <pre> blocks in snippet and credential detail pages with CodeBlock - make form cards full-width on pages with code editors - add resize:vertical support to CodeEditor wrapper with automaticLayout
138 lines
4.4 KiB
TypeScript
138 lines
4.4 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { useNavigate, useParams } from 'react-router-dom';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { Upload, Pencil, Trash2 } from 'lucide-react';
|
|
import { CodeBlock } from '../../ui';
|
|
import { stringify as yamlStringify } from 'yaml';
|
|
import { credentials } from '../../api';
|
|
import type { Credential } from '../../api';
|
|
import { Breadcrumbs, Button, Card, DescriptionList, Notification, Timestamp, UuidBadge } from '../../ui';
|
|
import styles from '../Page.module.css';
|
|
|
|
export function CredentialDetailPage() {
|
|
const { t } = useTranslation();
|
|
const { id } = useParams<{ id: string }>();
|
|
const navigate = useNavigate();
|
|
const [credential, setCredential] = useState<Credential | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!id) return;
|
|
credentials
|
|
.get(id)
|
|
.then(setCredential)
|
|
.catch((err: Error) => setError(err.message))
|
|
.finally(() => setLoading(false));
|
|
}, [id]);
|
|
|
|
const handleDelete = async () => {
|
|
if (!credential) return;
|
|
await credentials.remove(credential.id);
|
|
navigate('/credentials');
|
|
};
|
|
|
|
const handleExport = async () => {
|
|
if (!credential) return;
|
|
const data = await credentials.exportCredential(credential.id);
|
|
const blob = new Blob([yamlStringify(data)], { type: 'application/yaml' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = `credential-${credential.name}.yaml`;
|
|
a.click();
|
|
URL.revokeObjectURL(url);
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<div className={styles.pageToolbar}>
|
|
<Breadcrumbs
|
|
items={[
|
|
{ label: t('credentials.title'), onClick: () => navigate('/credentials') },
|
|
{ label: credential?.name ?? `#${id}` },
|
|
]}
|
|
/>
|
|
{credential && (
|
|
<div className={styles.toolbarActions}>
|
|
<Button variant="secondary" size="sm" onClick={handleExport}>
|
|
<Upload size={14} />
|
|
{t('credentials.action_export')}
|
|
</Button>
|
|
<Button
|
|
variant="secondary"
|
|
size="sm"
|
|
onClick={() => navigate(`/credentials/${credential.id}/edit`)}
|
|
>
|
|
<Pencil size={14} />
|
|
{t('credentials.action_edit')}
|
|
</Button>
|
|
<Button variant="danger" size="sm" onClick={handleDelete}>
|
|
<Trash2 size={14} />
|
|
{t('credentials.action_delete')}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{error && (
|
|
<Notification
|
|
variant="error"
|
|
title={error.startsWith('404') ? t('errors.not_found') : undefined}
|
|
>
|
|
{error.startsWith('404') ? t('errors.not_found_credential', { id }) : error}
|
|
</Notification>
|
|
)}
|
|
|
|
{loading && <p className={styles.muted}>{t('credentials.loading')}</p>}
|
|
|
|
{credential && (
|
|
<>
|
|
<div className={styles.detailMeta}>
|
|
<DescriptionList
|
|
layout="comfortable"
|
|
items={[
|
|
{ term: t('credentials.field_id'), detail: <UuidBadge id={credential.id} /> },
|
|
{
|
|
term: t('credentials.field_last_used'),
|
|
detail: credential.lastUsedAt ? (
|
|
<Timestamp value={credential.lastUsedAt} />
|
|
) : (
|
|
'—'
|
|
),
|
|
},
|
|
{
|
|
term: t('credentials.field_created'),
|
|
detail: <Timestamp value={credential.createdAt} />,
|
|
},
|
|
{
|
|
term: t('credentials.field_updated'),
|
|
detail: <Timestamp value={credential.updatedAt} />,
|
|
},
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
{credential.data && (
|
|
<>
|
|
<h2 className={styles.sectionHeading}>{t('credentials.section_data')}</h2>
|
|
<Card>
|
|
<CodeBlock
|
|
language="json"
|
|
code={(() => {
|
|
try {
|
|
return JSON.stringify(JSON.parse(credential.data), null, 2);
|
|
} catch {
|
|
return credential.data;
|
|
}
|
|
})()}
|
|
/>
|
|
</Card>
|
|
</>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|