Mache die Inhaltsliste zur einen Arbeitsflaeche mit Vorschauzeilen, damit Pfad, Herkunft und eigene Reihenfolge im Alltag erhalten bleiben.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
52bda91aad
commit
3465ed57f6
16 files changed
+1375
-314
No files matched your search
+442
-27
@@ -1,6 +1,6 @@
|
||||
import { useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import { api } from "../api.js";
|
||||
import { api, fetchFile, fetchPaperless } from "../api.js";
|
||||
import { ErrorNote } from "./widgets.jsx";
|
||||
|
||||
export function formatDate(value) {
|
||||
@@ -41,6 +41,56 @@ export function Trail({ items }) {
|
||||
);
|
||||
}
|
||||
|
||||
export function nestRecords(items, parentKey) {
|
||||
const ids = new Set((items || []).map((item) => item.id));
|
||||
const children = new Map();
|
||||
const roots = [];
|
||||
for (const item of items || []) {
|
||||
const parentId = item[parentKey];
|
||||
if (parentId && ids.has(parentId)) {
|
||||
const list = children.get(parentId) || [];
|
||||
list.push(item);
|
||||
children.set(parentId, list);
|
||||
} else {
|
||||
roots.push(item);
|
||||
}
|
||||
}
|
||||
return { roots, children };
|
||||
}
|
||||
|
||||
function TreeBranch({ item, children, hrefFor, nested }) {
|
||||
const kids = children.get(item.id) || [];
|
||||
return (
|
||||
<li>
|
||||
<Link className="hierarchy__row" to={hrefFor(item)}>
|
||||
<span className="hierarchy__kind">{item.kind_label || "Eintrag"}</span>
|
||||
<strong>{item.title}</strong>
|
||||
{!nested && item.parent_title ? <span className="muted">in {item.parent_title}</span> : null}
|
||||
<span className="muted">{item.status === "open" ? "Offen" : item.status === "closed" ? "Geschlossen" : ""}</span>
|
||||
</Link>
|
||||
{kids.length ? (
|
||||
<ul>
|
||||
{kids.map((child) => (
|
||||
<TreeBranch key={child.id} item={child} children={children} hrefFor={hrefFor} nested />
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
export function RecordTree({ items, parentKey, hrefFor }) {
|
||||
const { roots, children } = nestRecords(items, parentKey);
|
||||
if (!roots.length) return null;
|
||||
return (
|
||||
<ul className="hierarchy">
|
||||
{roots.map((item) => (
|
||||
<TreeBranch key={item.id} item={item} children={children} hrefFor={hrefFor} nested={false} />
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
function sectionsOf(chapters, bindings) {
|
||||
const buckets = new Map();
|
||||
const loose = [];
|
||||
@@ -62,6 +112,102 @@ function sectionsOf(chapters, bindings) {
|
||||
return sections;
|
||||
}
|
||||
|
||||
export function matterOpenHref(item) {
|
||||
const contexts = item?.contexts || [];
|
||||
if (contexts.some((entry) => entry.kind === "direct")) return `/matters/${item.id}`;
|
||||
const context = contexts.find((entry) => entry.kind === "case");
|
||||
if (!context) return `/matters/${item.id}`;
|
||||
return `/matters/${item.id}?context_kind=case&context_id=${context.id}`;
|
||||
}
|
||||
|
||||
export function Placements({ items }) {
|
||||
if (!items?.length) return null;
|
||||
const current = items.find((item) => item.current);
|
||||
return (
|
||||
<p className="placements">
|
||||
<span>In Akte: </span>
|
||||
{items.map((item, index) => (
|
||||
<span key={item.case_id}>
|
||||
{index ? <span aria-hidden="true"> · </span> : null}
|
||||
<Link to={item.href} aria-current={item.current ? "true" : undefined}>{item.label.replaceAll("Unterakte ", "").replaceAll("Akte ", "")}</Link>
|
||||
</span>
|
||||
))}
|
||||
{current ? <span className="muted"> · aktueller Handlungskontext</span> : <span className="muted"> · Handlungskontext direkt, solange keine Akte gewählt ist</span>}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
export function ObjectMenu({ view, onView, children }) {
|
||||
return (
|
||||
<details className="object-menu">
|
||||
<summary>Menü</summary>
|
||||
<div className="stack">
|
||||
<button type="button" className={view === "course" ? "" : "quiet"} onClick={() => onView("course")}>Fachlicher Verlauf</button>
|
||||
<button type="button" className={view === "audit" ? "" : "quiet"} onClick={() => onView("audit")}>Änderungsprotokoll</button>
|
||||
<button type="button" className={view === "links" ? "" : "quiet"} onClick={() => onView("links")}>Zuordnungen</button>
|
||||
{children}
|
||||
</div>
|
||||
</details>
|
||||
);
|
||||
}
|
||||
|
||||
export function WorkSwitch({ view, onView }) {
|
||||
return (
|
||||
<div className="order-bar" role="tablist" aria-label="Arbeitsfläche">
|
||||
<button type="button" role="tab" aria-selected={view === "contents"} className={view === "contents" ? "" : "quiet"} onClick={() => onView("contents")}>Inhalte</button>
|
||||
<button type="button" role="tab" aria-selected={view === "work"} className={view === "work" ? "" : "quiet"} onClick={() => onView("work")}>Aufgaben und Fristen</button>
|
||||
{view !== "contents" && view !== "work" ? (
|
||||
<button type="button" className="quiet" onClick={() => onView("contents")}>Zurück zu den Inhalten</button>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function sourceLabel(element) {
|
||||
if (element.kind === "note") return "Notiz";
|
||||
if (element.kind === "external_ref") return element.external?.source === "paperless" ? "Paperless" : "Referenz";
|
||||
if (element.file?.content_type === "application/pdf") return "PDF";
|
||||
if (element.file?.content_type?.startsWith("image/")) return "Bild";
|
||||
if (element.kind === "file") return "Datei";
|
||||
return kindLabel(element.kind);
|
||||
}
|
||||
|
||||
function blurb(element) {
|
||||
return element.summary || element.excerpt || "";
|
||||
}
|
||||
|
||||
function Thumb({ element, context }) {
|
||||
const [url, setUrl] = useState("");
|
||||
const [media, setMedia] = useState("");
|
||||
const [failed, setFailed] = useState(false);
|
||||
useEffect(() => {
|
||||
if (!element.preview?.available) return undefined;
|
||||
let alive = true;
|
||||
let current = "";
|
||||
const load = element.preview.source === "paperless" ? fetchPaperless(element.id, context) : fetchFile(element.id, context);
|
||||
load.then((blob) => {
|
||||
if (!alive) return;
|
||||
current = URL.createObjectURL(blob);
|
||||
setUrl(current);
|
||||
if (blob.type.startsWith("image/")) setMedia("image");
|
||||
else if (blob.type.includes("pdf")) setMedia("pdf");
|
||||
else setMedia("other");
|
||||
}).catch(() => {
|
||||
if (alive) setFailed(true);
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
if (current) URL.revokeObjectURL(current);
|
||||
};
|
||||
}, [element.id, element.preview?.available, element.preview?.source, context?.kind, context?.id]);
|
||||
if (element.kind === "note") return <span className="thumb thumb--note">Notiz</span>;
|
||||
if (url && media === "image") return <img className="thumb" alt="" src={url} />;
|
||||
if (url && media === "pdf") return <iframe className="thumb thumb--pdf" title={`Vorschau ${element.title}`} src={url} />;
|
||||
if (failed) return <span className="thumb thumb--empty">Vorschau nicht verfügbar</span>;
|
||||
if (element.preview?.available) return <span className="thumb thumb--empty">Vorschau wird geladen</span>;
|
||||
return <span className="thumb thumb--empty">Keine Vorschau</span>;
|
||||
}
|
||||
|
||||
export function ContentBoard({
|
||||
record,
|
||||
bindings,
|
||||
@@ -71,27 +217,61 @@ export function ContentBoard({
|
||||
sortable = true,
|
||||
onChange,
|
||||
itemHref,
|
||||
onlyChapter = "",
|
||||
}) {
|
||||
const source = bindings || record?.bindings || [];
|
||||
const chapters = record?.chapters || [];
|
||||
const [error, setError] = useState("");
|
||||
const [pending, setPending] = useState(false);
|
||||
const [draggingId, setDraggingId] = useState("");
|
||||
const [indicator, setIndicator] = useState(null);
|
||||
const dragRef = useRef(null);
|
||||
const rootRef = useRef(null);
|
||||
const custom = record?.content_order === "position";
|
||||
const direction = record?.content_order_dir === "desc" ? "desc" : "asc";
|
||||
const sections = sectionsOf(chapters, source);
|
||||
const visibleSections = onlyChapter ? sections.filter((section) => section.id === onlyChapter) : sections;
|
||||
const canSort = sortable && record?.rights?.edit;
|
||||
const orderPath = `/api/v1/${containerKind === "case" ? "cases" : "matters"}/${containerId}`;
|
||||
const orderContext = context && containerKind === "matter" ? { context } : {};
|
||||
|
||||
useEffect(() => {
|
||||
function onKey(event) {
|
||||
if (event.key !== "Escape" || !dragRef.current) return;
|
||||
dragRef.current = null;
|
||||
setDraggingId("");
|
||||
setIndicator(null);
|
||||
}
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
}, []);
|
||||
|
||||
async function saveMode(mode, nextDirection) {
|
||||
setError("");
|
||||
setPending(true);
|
||||
try {
|
||||
await api(`/api/v1/${containerKind === "case" ? "cases" : "matters"}/${containerId}/content-order`, {
|
||||
await api(`${orderPath}/content-order`, {
|
||||
method: "PATCH",
|
||||
body: { expected_version: record.version, mode, direction: nextDirection, ...orderContext },
|
||||
});
|
||||
onChange();
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
setPending(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function persist(next) {
|
||||
setError("");
|
||||
setPending(true);
|
||||
try {
|
||||
await api(`${orderPath}/bindings/order`, {
|
||||
method: "POST",
|
||||
body: {
|
||||
expected_version: record.version,
|
||||
mode,
|
||||
direction: nextDirection,
|
||||
...(context && containerKind === "matter" ? { context } : {}),
|
||||
binding_ids: next.flatMap((section) => section.items.map((item) => item.id)),
|
||||
...orderContext,
|
||||
},
|
||||
});
|
||||
onChange();
|
||||
@@ -114,16 +294,16 @@ export function ContentBoard({
|
||||
moved = true;
|
||||
}
|
||||
if (!moved) return;
|
||||
await persist(next);
|
||||
}
|
||||
|
||||
async function placeChapter(bindingId, chapterId) {
|
||||
setError("");
|
||||
setPending(true);
|
||||
try {
|
||||
await api(`/api/v1/${containerKind === "case" ? "cases" : "matters"}/${containerId}/bindings/order`, {
|
||||
method: "POST",
|
||||
body: {
|
||||
expected_version: record.version,
|
||||
binding_ids: next.flatMap((section) => section.items.map((item) => item.id)),
|
||||
...(context && containerKind === "matter" ? { context } : {}),
|
||||
},
|
||||
await api(`/api/v1/bindings/${bindingId}/chapter`, {
|
||||
method: "PATCH",
|
||||
body: { expected_version: record.version, chapter_id: chapterId || null, ...orderContext },
|
||||
});
|
||||
onChange();
|
||||
} catch (err) {
|
||||
@@ -133,8 +313,97 @@ export function ContentBoard({
|
||||
}
|
||||
}
|
||||
|
||||
function indexAt(sectionId, clientY) {
|
||||
const rows = [...(rootRef.current?.querySelectorAll(`[data-section="${sectionId}"] [data-binding]`) || [])];
|
||||
for (let i = 0; i < rows.length; i += 1) {
|
||||
const rect = rows[i].getBoundingClientRect();
|
||||
if (clientY < rect.top + rect.height / 2) return i;
|
||||
}
|
||||
return rows.length;
|
||||
}
|
||||
|
||||
function finishDrag(drag) {
|
||||
dragRef.current = null;
|
||||
setDraggingId("");
|
||||
setIndicator(null);
|
||||
if (!drag || !drag.moved || drag.index == null) return;
|
||||
const next = sections.map((section) => ({ ...section, items: [...section.items] }));
|
||||
const section = next.find((item) => item.id === drag.sectionId);
|
||||
if (!section) return;
|
||||
const from = section.items.findIndex((item) => item.id === drag.bindingId);
|
||||
if (from < 0) return;
|
||||
let target = drag.index;
|
||||
if (from < target) target -= 1;
|
||||
if (target < 0) target = 0;
|
||||
if (target > section.items.length - 1) target = section.items.length - 1;
|
||||
if (target === from) return;
|
||||
const [item] = section.items.splice(from, 1);
|
||||
section.items.splice(target, 0, item);
|
||||
persist(next);
|
||||
}
|
||||
|
||||
function track(event) {
|
||||
const drag = dragRef.current;
|
||||
if (!drag || drag.pointerId !== event.pointerId) return;
|
||||
if (Math.abs(event.clientY - drag.startY) > 4) drag.moved = true;
|
||||
if (event.clientY < 72) window.scrollBy(0, -16);
|
||||
if (event.clientY > window.innerHeight - 72) window.scrollBy(0, 16);
|
||||
drag.index = indexAt(drag.sectionId, event.clientY);
|
||||
setIndicator({ sectionId: drag.sectionId, index: drag.index });
|
||||
}
|
||||
|
||||
function release(event) {
|
||||
const drag = dragRef.current;
|
||||
window.removeEventListener("pointermove", track);
|
||||
window.removeEventListener("pointerup", release);
|
||||
window.removeEventListener("pointercancel", cancel);
|
||||
if (!drag || drag.html5 || drag.pointerId !== event.pointerId) return;
|
||||
finishDrag(drag);
|
||||
}
|
||||
|
||||
function cancel() {
|
||||
window.removeEventListener("pointermove", track);
|
||||
window.removeEventListener("pointerup", release);
|
||||
window.removeEventListener("pointercancel", cancel);
|
||||
dragRef.current = null;
|
||||
setDraggingId("");
|
||||
setIndicator(null);
|
||||
}
|
||||
|
||||
function onHandleDown(event, bindingId, sectionId) {
|
||||
if (event.button != null && event.button !== 0) return;
|
||||
dragRef.current = { pointerId: event.pointerId, bindingId, sectionId, startY: event.clientY, moved: false, index: null };
|
||||
setDraggingId(bindingId);
|
||||
window.addEventListener("pointermove", track);
|
||||
window.addEventListener("pointerup", release);
|
||||
window.addEventListener("pointercancel", cancel);
|
||||
}
|
||||
|
||||
function onDragStart(event, bindingId, sectionId) {
|
||||
event.dataTransfer.effectAllowed = "move";
|
||||
event.dataTransfer.setData("text/plain", bindingId);
|
||||
dragRef.current = { pointerId: "html5", bindingId, sectionId, startY: event.clientY, moved: true, index: null, html5: true };
|
||||
setDraggingId(bindingId);
|
||||
}
|
||||
|
||||
function onDragOver(event, sectionId) {
|
||||
const drag = dragRef.current;
|
||||
if (!drag?.html5 || drag.sectionId !== sectionId) return;
|
||||
event.preventDefault();
|
||||
drag.index = indexAt(sectionId, event.clientY);
|
||||
setIndicator({ sectionId, index: drag.index });
|
||||
}
|
||||
|
||||
function onDrop(event, sectionId) {
|
||||
const drag = dragRef.current;
|
||||
if (!drag?.html5 || drag.sectionId !== sectionId) return;
|
||||
event.preventDefault();
|
||||
drag.index = indexAt(sectionId, event.clientY);
|
||||
finishDrag(drag);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="contents">
|
||||
<div className="contents" ref={rootRef}>
|
||||
{canSort ? (
|
||||
<div className="order-bar" role="group" aria-label="Ordnung der Inhalte">
|
||||
<button type="button" className={record.content_order !== "position" && direction === "asc" ? "" : "quiet"} disabled={pending} onClick={() => saveMode("date", "asc")}>
|
||||
@@ -148,33 +417,69 @@ export function ContentBoard({
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<p className="muted">{custom ? "Eigene Reihenfolge" : `Fachliches Datum, ${direction === "desc" ? "absteigend" : "aufsteigend"}`}</p>
|
||||
<p className="muted">{sortable ? (custom ? "Eigene Reihenfolge" : `Fachliches Datum, ${direction === "desc" ? "absteigend" : "aufsteigend"}`) : "Lesesicht nach fachlichem Datum. Zum Ordnen einen einzelnen Bereich wählen."}</p>
|
||||
)}
|
||||
{pending ? <p className="muted">Ordnung wird gespeichert …</p> : null}
|
||||
<ErrorNote error={error} />
|
||||
{source.length ? (
|
||||
sections.map((section) => (
|
||||
<section key={section.id} className="content-section">
|
||||
visibleSections.map((section) => (
|
||||
<section
|
||||
key={section.id}
|
||||
className="content-section"
|
||||
data-section={section.id}
|
||||
onDragOver={(event) => onDragOver(event, section.id)}
|
||||
onDrop={(event) => onDrop(event, section.id)}
|
||||
>
|
||||
{section.title ? <h3 className="chapter-title">{section.title}</h3> : null}
|
||||
<ol className="content-list">
|
||||
<ol className="preview-list">
|
||||
{section.items.map((binding, index) => {
|
||||
const element = binding.element;
|
||||
const href = itemHref(binding);
|
||||
const text = blurb(element);
|
||||
return (
|
||||
<li key={binding.id} className="content-row">
|
||||
<Link className="content-row__main" to={href}>
|
||||
<span className="content-row__date">{formatDate(fachDate(element))}</span>
|
||||
<span className="content-row__title">{element.title}</span>
|
||||
<span className="content-row__kind">{kindLabel(element.kind)}</span>
|
||||
<li key={binding.id} data-binding={binding.id} className={draggingId === binding.id ? "preview-row is-dragging" : "preview-row"}>
|
||||
{indicator?.sectionId === section.id && indicator.index === index ? <div className="insert-line" /> : null}
|
||||
{canSort && custom ? (
|
||||
<button
|
||||
type="button"
|
||||
className="handle"
|
||||
aria-label={`${element.title} ziehen`}
|
||||
draggable={!pending}
|
||||
disabled={pending}
|
||||
onPointerDown={(event) => onHandleDown(event, binding.id, section.id)}
|
||||
onDragStart={(event) => onDragStart(event, binding.id, section.id)}
|
||||
onDragEnd={cancel}
|
||||
>
|
||||
Ziehen
|
||||
</button>
|
||||
) : null}
|
||||
<Thumb element={element} context={context} />
|
||||
<Link className="preview-row__main" to={href}>
|
||||
<strong>{element.title}</strong>
|
||||
<span className="preview-row__meta">{formatDate(fachDate(element))} · {sourceLabel(element)}</span>
|
||||
{text ? <span className="preview-row__text">{text}</span> : null}
|
||||
{binding.origin ? <span className="preview-row__origin">{binding.origin}{binding.shared ? " · weitere Einbindung" : ""}</span> : null}
|
||||
</Link>
|
||||
{canSort && custom ? (
|
||||
<span className="content-row__move">
|
||||
<button type="button" className="quiet" aria-label={`${element.title} nach oben`} disabled={pending || index === 0} onClick={() => move(binding.id, -1)}>Nach oben</button>
|
||||
<button type="button" className="quiet" aria-label={`${element.title} nach unten`} disabled={pending || index === section.items.length - 1} onClick={() => move(binding.id, 1)}>Nach unten</button>
|
||||
</span>
|
||||
<details className="row-menu">
|
||||
<summary>Ordnen</summary>
|
||||
<button type="button" aria-label={`${element.title} nach oben`} disabled={pending || index === 0} onClick={() => move(binding.id, -1)}>Nach oben</button>
|
||||
<button type="button" aria-label={`${element.title} nach unten`} disabled={pending || index === section.items.length - 1} onClick={() => move(binding.id, 1)}>Nach unten</button>
|
||||
{chapters.length ? (
|
||||
<label>
|
||||
In Kapitel einordnen
|
||||
<select value={binding.chapter_id || ""} disabled={pending} onChange={(event) => placeChapter(binding.id, event.target.value)}>
|
||||
<option value="">Ohne Kapitel</option>
|
||||
{chapters.map((chapter) => <option key={chapter.id} value={chapter.id}>{chapter.title}</option>)}
|
||||
</select>
|
||||
</label>
|
||||
) : null}
|
||||
</details>
|
||||
) : null}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
{indicator?.sectionId === section.id && indicator.index === section.items.length ? <li className="insert-line" aria-hidden="true" /> : null}
|
||||
</ol>
|
||||
</section>
|
||||
))
|
||||
@@ -268,3 +573,113 @@ export function AuditLog({ items, nextCursor, onMore }) {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function WorkLists({ record, context, onChange }) {
|
||||
const linked = new Set((record.task_deadline_links || []).map((item) => item.task_id));
|
||||
|
||||
async function complete(task) {
|
||||
const body = { expected_version: task.version, status: "done" };
|
||||
const taskContext = task.action_context || context;
|
||||
if (taskContext) body.context = taskContext;
|
||||
await api(`/api/v1/tasks/${task.id}`, { method: "PATCH", body });
|
||||
onChange();
|
||||
}
|
||||
|
||||
async function fulfill(deadline) {
|
||||
const body = { expected_version: deadline.version };
|
||||
if (context) body.context = context;
|
||||
await api(`/api/v1/deadlines/${deadline.id}/fulfill`, { method: "POST", body });
|
||||
onChange();
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="stack">
|
||||
<p className="muted">Erledigen und Erfüllen bleiben getrennte Aktionen.</p>
|
||||
<div>
|
||||
<h3>Aufgaben</h3>
|
||||
{record.tasks?.length ? (
|
||||
<ul className="work">
|
||||
{record.tasks.map((task) => (
|
||||
<li key={task.id}>
|
||||
<div>
|
||||
<strong>{task.title}</strong>
|
||||
<span>{task.status === "done" ? "Erledigt" : "Offen"}{task.due_on ? ` · ${formatDate(task.due_on)}` : ""}{task.matter_title ? ` · ${task.matter_title}` : ""}</span>
|
||||
{linked.has(task.id) ? <small>Mit einer Frist verknüpft, ohne sie zu erfüllen.</small> : null}
|
||||
</div>
|
||||
{task.status === "open" && task.rights?.edit !== false ? <button type="button" onClick={() => complete(task)}>Erledigen</button> : null}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : <p className="muted">Keine Aufgabe.</p>}
|
||||
</div>
|
||||
<div>
|
||||
<h3>Fristen</h3>
|
||||
{record.deadlines?.length ? (
|
||||
<ul className="work">
|
||||
{record.deadlines.map((deadline) => (
|
||||
<li key={deadline.id}>
|
||||
<div>
|
||||
<strong>{deadline.title}</strong>
|
||||
<span>{deadline.status === "fulfilled" ? "Erfüllt" : "Offen"} · {formatDate(deadline.due_on)}{deadline.matter_title ? ` · ${deadline.matter_title}` : ""}</span>
|
||||
</div>
|
||||
{deadline.status === "open" ? <button type="button" onClick={() => fulfill(deadline)}>Frist erfüllen</button> : null}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : <p className="muted">Keine Frist.</p>}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function SurfaceView({ view, coursePath, auditPath }) {
|
||||
const [course, setCourse] = useState(null);
|
||||
const [audit, setAudit] = useState(null);
|
||||
const [cursor, setCursor] = useState(null);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
setCourse(null);
|
||||
setAudit(null);
|
||||
setCursor(null);
|
||||
}, [coursePath, auditPath]);
|
||||
|
||||
useEffect(() => {
|
||||
if (view !== "course") return undefined;
|
||||
let alive = true;
|
||||
api(coursePath).then((data) => {
|
||||
if (alive) setCourse(data.items);
|
||||
}).catch((err) => {
|
||||
if (alive) setError(err.message);
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [view, coursePath]);
|
||||
|
||||
async function showAudit(nextCursor) {
|
||||
const joiner = auditPath.includes("?") ? "&" : "?";
|
||||
const data = await api(`${auditPath}${nextCursor ? `${joiner}cursor=${encodeURIComponent(nextCursor)}` : ""}`);
|
||||
setAudit((current) => (nextCursor ? [...(current || []), ...data.items] : data.items));
|
||||
setCursor(data.next_cursor);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (view !== "audit") return undefined;
|
||||
let alive = true;
|
||||
showAudit("").catch((err) => {
|
||||
if (alive) setError(err.message);
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [view, auditPath]);
|
||||
|
||||
if (view !== "course" && view !== "audit") return null;
|
||||
return (
|
||||
<div>
|
||||
<ErrorNote error={error} />
|
||||
{view === "course" ? <CourseList items={course} /> : <AuditLog items={audit} nextCursor={cursor} onMore={() => showAudit(cursor).catch((err) => setError(err.message))} />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user