- add usePageTitle hook that writes '<title> | Liqa' to document.title - list pages use section name; detail pages use entity name once loaded - create/edit pages use static action title, edit includes entity name
168 lines
5.2 KiB
TypeScript
168 lines
5.2 KiB
TypeScript
import { Globe, Pencil, Trash2, Upload } from 'lucide-react';
|
|
import { useEffect, useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { usePageTitle } from '../../hooks/usePageTitle';
|
|
import { useNavigate, useParams } from 'react-router-dom';
|
|
import { stringify as yamlStringify } from 'yaml';
|
|
import type { Environment } from '../../api';
|
|
import { environments } from '../../api';
|
|
import { DeleteConfirmationModal } from '../../components/modals';
|
|
import {
|
|
Breadcrumbs,
|
|
Button,
|
|
Card,
|
|
DescriptionList,
|
|
Timestamp,
|
|
UuidBadge,
|
|
MarkdownContent,
|
|
useToast,
|
|
} from '../../ui';
|
|
import styles from '../Page.module.css';
|
|
|
|
export function EnvironmentDetailPage() {
|
|
const { t } = useTranslation();
|
|
const toast = useToast();
|
|
const { id } = useParams<{ id: string }>();
|
|
const navigate = useNavigate();
|
|
const [env, setEnv] = useState<Environment | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
usePageTitle(env?.name ?? null);
|
|
const [confirmDeleteOpen, setConfirmDeleteOpen] = useState(false);
|
|
const [deleting, setDeleting] = useState(false);
|
|
const dataEntries = Object.entries(env?.data ?? {}).filter(([, v]) => v);
|
|
|
|
useEffect(() => {
|
|
if (!id) return;
|
|
environments
|
|
.get(id)
|
|
.then(setEnv)
|
|
.catch((err: Error) => toast.error((err as Error).message))
|
|
.finally(() => setLoading(false));
|
|
}, [id, toast]);
|
|
|
|
const handleDelete = async () => {
|
|
if (!env) return;
|
|
setDeleting(true);
|
|
try {
|
|
await environments.remove(env.id);
|
|
navigate('/environments');
|
|
} finally {
|
|
setDeleting(false);
|
|
setConfirmDeleteOpen(false);
|
|
}
|
|
};
|
|
|
|
const handleExport = async () => {
|
|
if (!env) return;
|
|
const data = await environments.exportEnvironment(env.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 = `environment-${env.name}.yaml`;
|
|
a.click();
|
|
URL.revokeObjectURL(url);
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<div className={styles.pageToolbar}>
|
|
<Breadcrumbs
|
|
items={[
|
|
{
|
|
label: t('environments.title'),
|
|
icon: <Globe size={14} />,
|
|
onClick: () => navigate('/environments'),
|
|
},
|
|
{ label: env?.name ?? `#${id}` },
|
|
]}
|
|
/>
|
|
{env && (
|
|
<div className={styles.toolbarActions}>
|
|
<Button variant="secondary" size="sm" onClick={handleExport}>
|
|
<Upload size={14} />
|
|
{t('environments.action_export')}
|
|
</Button>
|
|
<Button
|
|
variant="secondary"
|
|
size="sm"
|
|
onClick={() => navigate(`/environments/${env.id}/edit`)}
|
|
>
|
|
<Pencil size={14} />
|
|
{t('environments.action_edit')}
|
|
</Button>
|
|
<Button variant="danger" size="sm" onClick={() => setConfirmDeleteOpen(true)}>
|
|
<Trash2 size={14} />
|
|
{t('environments.action_delete')}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{loading && <p className={styles.muted}>{t('environments.loading')}</p>}
|
|
|
|
{env && (
|
|
<>
|
|
<Card>
|
|
<DescriptionList
|
|
layout="grid"
|
|
items={[
|
|
{ term: t('environments.field_id'), detail: <UuidBadge id={env.id} /> },
|
|
{
|
|
term: t('environments.field_created'),
|
|
detail: <Timestamp value={env.createdAt} />,
|
|
},
|
|
{
|
|
term: t('environments.field_updated'),
|
|
detail: <Timestamp value={env.updatedAt} />,
|
|
},
|
|
]}
|
|
/>
|
|
</Card>
|
|
|
|
{env.description && (
|
|
<div className={styles.stepsSection}>
|
|
<div className={styles.sectionHeadingRow}>
|
|
<h2 className={styles.sectionHeading}>{t('environments.field_description')}</h2>
|
|
</div>
|
|
<Card>
|
|
<MarkdownContent content={env.description} />
|
|
</Card>
|
|
</div>
|
|
)}
|
|
|
|
<div className={styles.stepsSection}>
|
|
<h2 className={styles.sectionHeading}>{t('environments.section_data')}</h2>
|
|
<Card>
|
|
{dataEntries.length === 0 ? (
|
|
<p className={styles.muted}>{t('environments.no_data')}</p>
|
|
) : (
|
|
<DescriptionList
|
|
layout="grid"
|
|
items={dataEntries.map(([key, value]) => ({
|
|
term: key,
|
|
detail: (
|
|
<a href={value} target="_blank" rel="noopener noreferrer">
|
|
{value}
|
|
</a>
|
|
),
|
|
}))}
|
|
/>
|
|
)}
|
|
</Card>
|
|
</div>
|
|
</>
|
|
)}
|
|
|
|
<DeleteConfirmationModal
|
|
open={confirmDeleteOpen}
|
|
title={t('environments.action_delete')}
|
|
message={t('common.confirm_delete_environment')}
|
|
onClose={() => setConfirmDeleteOpen(false)}
|
|
onConfirm={handleDelete}
|
|
isDeleting={deleting}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|