import { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
import { NavLink, Outlet, useLocation, useSearchParams } from "react-router-dom";
import { api } from "../api.js";
import { SHARE_DRAG_TYPE, isEmployee, parseShareDrag, shareTargets } from "../admin/admin-core.js";
import { ErrorNote } from "../pages/widgets.jsx";
import { SessionContext } from "../session.js";
import { effectText, folderTree } from "./library-core.js";
import { MiniIcon } from "./MiniIcon.jsx";
export const LibraryContext = createContext(null);
export function useLibrary() {
return useContext(LibraryContext);
}
const PLURAL = { case: "cases", matter: "matters" };
function TreeNodes({ nodes, children, hrefFor, parentKey, current, openIds, toggle }) {
return (
);
}
function TreeSection({ to, label, items, parentKey, folder, active }) {
const { roots, children } = folderTree(items || [], parentKey);
const [openIds, setOpenIds] = useState(() => new Set());
const byId = new Map((items || []).map((item) => [item.id, item]));
// The path to the selected folder stays open.
const path = new Set();
for (let node = byId.get(folder); node; node = node[parentKey] ? byId.get(node[parentKey]) : null) path.add(node[parentKey]);
const shown = new Set([...openIds, ...path]);
const toggle = (id) =>
setOpenIds((current) => {
const next = new Set(current);
if (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
return (
{label}
{(items || []).length}
`${to}?ordner=${node.id}`}
current={active ? folder : ""}
openIds={shown}
toggle={toggle}
/>
);
}
function LibraryTree({ cases, matters, rooms, onDropOnRoom, employee }) {
const location = useLocation();
const [params] = useSearchParams();
const folder = params.get("ordner") || "";
const [over, setOver] = useState("");
return (
);
}
// Frame of the library: folder tree on the left, tiles on the right, one place for data and messages.
export function LibraryLayout() {
const { session } = useContext(SessionContext);
const employee = isEmployee(session);
const [data, setData] = useState({ cases: null, matters: null, rooms: null });
const [error, setError] = useState("");
const [message, setMessage] = useState("");
const [treeOpen, setTreeOpen] = useState(false);
const timer = useRef(null);
const location = useLocation();
const reload = useCallback(async () => {
try {
const [cases, matters, rooms] = await Promise.all([
api("/api/v1/cases"),
api("/api/v1/matters"),
api("/api/v1/rooms/overview"),
]);
setData({ cases: cases.items, matters: matters.items, rooms: rooms.items });
} catch (err) {
setError(err.message);
}
}, []);
useEffect(() => {
reload();
}, [reload, session.scope.id]);
useEffect(() => {
setTreeOpen(false);
}, [location.pathname, location.search]);
const notify = useCallback((text) => {
setMessage(text);
clearTimeout(timer.current);
timer.current = setTimeout(() => setMessage(""), 9000);
}, []);
async function share(kind, item, room) {
setError("");
try {
const result = await api(`/api/v1/${PLURAL[kind]}/${item.id}/room-share/rooms/${room.id}`, { method: "PUT", body: { access: "edit" } });
notify(effectText(room.name, result.effect, item.title));
await reload();
} catch (err) {
setError(err.message);
}
}
async function unshare(kind, item, mark) {
setError("");
try {
await api(`/api/v1/${PLURAL[kind]}/${item.id}/room-share/rooms/${mark.id}`, { method: "DELETE" });
notify(`„${item.title}“ liegt nicht mehr in „${mark.name}“. Das Original bleibt bei dir.`);
await reload();
} catch (err) {
setError(err.message);
}
}
const value = {
...data,
reload,
notify,
share,
unshare,
dropOnRoom: (payload, room) => share(payload.kind, { id: payload.id, title: payload.title }, room),
targets: shareTargets(session.rooms),
employee,
};
return (
{message ?
{message}
: null}
);
}