Backend: - Perceptual hashes (aHash/dHash/pHash/wHash via imagehash, no imgdd) stored on items, computed on upload/download and by the new compute_visual_hashes command - Duplicates API: exact duplicates (multi-location items), visual matches for one item, union-find similarity groups with pagination - Delete API with ownership/staff checks, per-item and per-copy deletion, watched-folder path validation; storage overview and temp cleanup; file list accepts j_ids batches - Staged uploads are flagged visual_match with their library matches (threshold via VISUAL_MATCH_THRESHOLD) - Staff users API: list with upload counts, set role and avatar by J-ID; User.avatar FK with signed avatar URLs - Download threads close their DB connection and stale tasks are reaped, keeping behaviour Gunicorn-friendly Frontend: - /duplicates: exact duplicate groups with per-copy delete, visual similarity controls, search similar to a J-ID, paginated groups with selection, bulk delete and dismiss - /delete: storage cards, delete by J-ID with preview grid, temp cleanup - /users: staff directory with role selects and avatar J-ID inputs - Nav + command palette entries; top-bar avatar; upload cards and the metadata modal show library visual matches
222 lines
6.1 KiB
TypeScript
222 lines
6.1 KiB
TypeScript
import {
|
|
Copy,
|
|
FolderOpen,
|
|
Globe,
|
|
LogIn,
|
|
LogOut,
|
|
Menu,
|
|
Search,
|
|
Settings,
|
|
Trash2,
|
|
Upload,
|
|
Users,
|
|
type LucideIcon,
|
|
} from "lucide-react";
|
|
import { useMemo, useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
import { cn } from "@/lib/cn";
|
|
import { useAuth } from "@/store/auth";
|
|
import { useUi } from "@/store/ui";
|
|
|
|
interface Command {
|
|
id: string;
|
|
label: string;
|
|
hint?: string;
|
|
icon: LucideIcon;
|
|
run: () => void;
|
|
}
|
|
|
|
export function CommandPalette({
|
|
open,
|
|
onClose,
|
|
}: {
|
|
open: boolean;
|
|
onClose: () => void;
|
|
}) {
|
|
if (!open) return null;
|
|
return <CommandPaletteDialog onClose={onClose} />;
|
|
}
|
|
|
|
function CommandPaletteDialog({ onClose }: { onClose: () => void }) {
|
|
const navigate = useNavigate();
|
|
const user = useAuth((state) => state.user);
|
|
const logout = useAuth((state) => state.logout);
|
|
const toggleSidebar = useUi((state) => state.toggleSidebar);
|
|
const [query, setQuery] = useState("");
|
|
const [activeIndex, setActiveIndex] = useState(0);
|
|
|
|
const commands = useMemo<Command[]>(() => {
|
|
const list: Command[] = [
|
|
{
|
|
id: "library",
|
|
label: "Go to Library",
|
|
icon: FolderOpen,
|
|
run: () => navigate("/"),
|
|
},
|
|
...(user?.can_upload
|
|
? [
|
|
{
|
|
id: "upload",
|
|
label: "Go to Upload",
|
|
icon: Upload,
|
|
run: () => navigate("/upload"),
|
|
},
|
|
{
|
|
id: "duplicates",
|
|
label: "Find duplicates",
|
|
icon: Copy,
|
|
run: () => navigate("/duplicates"),
|
|
},
|
|
{
|
|
id: "delete",
|
|
label: "Delete files & storage",
|
|
icon: Trash2,
|
|
run: () => navigate("/delete"),
|
|
},
|
|
]
|
|
: []),
|
|
{
|
|
id: "online",
|
|
label: "Go to Online browser",
|
|
icon: Globe,
|
|
run: () => navigate("/online"),
|
|
},
|
|
{
|
|
id: "filters",
|
|
label: "Toggle filters",
|
|
hint: "Ctrl+B",
|
|
icon: Menu,
|
|
run: toggleSidebar,
|
|
},
|
|
{
|
|
id: "search",
|
|
label: "Focus library search",
|
|
hint: "/",
|
|
icon: Search,
|
|
run: () => {
|
|
navigate("/");
|
|
window.requestAnimationFrame(() => {
|
|
document.getElementById("library-search")?.focus();
|
|
});
|
|
},
|
|
},
|
|
];
|
|
if (user) {
|
|
list.push({
|
|
id: "account",
|
|
label: "Account settings",
|
|
icon: Settings,
|
|
run: () => navigate("/account"),
|
|
});
|
|
if (user.is_staff || user.is_superuser || user.role === "staff") {
|
|
list.push({
|
|
id: "users",
|
|
label: "Manage users",
|
|
icon: Users,
|
|
run: () => navigate("/users"),
|
|
});
|
|
}
|
|
list.push({
|
|
id: "logout",
|
|
label: `Log out (${user.username})`,
|
|
icon: LogOut,
|
|
run: () => void logout(),
|
|
});
|
|
} else {
|
|
list.push({
|
|
id: "login",
|
|
label: "Log in",
|
|
icon: LogIn,
|
|
run: () => navigate("/login"),
|
|
});
|
|
}
|
|
return list;
|
|
}, [navigate, toggleSidebar, user, logout]);
|
|
|
|
const filtered = useMemo(() => {
|
|
const needle = query.trim().toLowerCase();
|
|
return needle
|
|
? commands.filter((command) =>
|
|
command.label.toLowerCase().includes(needle),
|
|
)
|
|
: commands;
|
|
}, [commands, query]);
|
|
|
|
function runCommand(command: Command | undefined) {
|
|
if (!command) return;
|
|
onClose();
|
|
command.run();
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className="fixed inset-0 z-[60] flex items-start justify-center bg-ctp-crust/70 px-4 pt-[18vh] backdrop-blur-sm"
|
|
onClick={onClose}
|
|
>
|
|
<div
|
|
className="w-full max-w-lg overflow-hidden rounded-lg border border-ctp-surface1 bg-ctp-surface0 shadow-[0_4px_12px_rgba(17,17,27,0.6)]"
|
|
onClick={(event) => event.stopPropagation()}
|
|
>
|
|
<input
|
|
autoFocus
|
|
value={query}
|
|
onChange={(event) => {
|
|
setQuery(event.target.value);
|
|
setActiveIndex(0);
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (event.key === "ArrowDown") {
|
|
event.preventDefault();
|
|
setActiveIndex((index) =>
|
|
Math.min(index + 1, filtered.length - 1),
|
|
);
|
|
} else if (event.key === "ArrowUp") {
|
|
event.preventDefault();
|
|
setActiveIndex((index) => Math.max(index - 1, 0));
|
|
} else if (event.key === "Enter") {
|
|
event.preventDefault();
|
|
runCommand(filtered[activeIndex]);
|
|
} else if (event.key === "Escape") {
|
|
onClose();
|
|
}
|
|
}}
|
|
placeholder="Type a command…"
|
|
className="w-full border-b border-ctp-surface1 bg-transparent px-4 py-3 font-mono text-sm text-ctp-text placeholder:text-ctp-overlay0 focus:outline-none"
|
|
/>
|
|
<ul className="max-h-72 overflow-y-auto p-1.5">
|
|
{filtered.length === 0 ? (
|
|
<li className="px-3 py-6 text-center text-sm text-ctp-overlay0">
|
|
No matching commands
|
|
</li>
|
|
) : (
|
|
filtered.map((command, index) => (
|
|
<li key={command.id}>
|
|
<button
|
|
type="button"
|
|
onMouseEnter={() => setActiveIndex(index)}
|
|
onClick={() => runCommand(command)}
|
|
className={cn(
|
|
"flex w-full items-center gap-3 rounded-md px-3 py-2 text-left text-sm transition",
|
|
index === activeIndex
|
|
? "bg-ctp-surface1 text-ctp-text"
|
|
: "text-ctp-subtext0",
|
|
)}
|
|
>
|
|
<command.icon className="h-4 w-4 shrink-0" />
|
|
<span className="flex-1 truncate">{command.label}</span>
|
|
{command.hint ? (
|
|
<span className="font-mono text-[11px] text-ctp-overlay0">
|
|
{command.hint}
|
|
</span>
|
|
) : null}
|
|
</button>
|
|
</li>
|
|
))
|
|
)}
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|