dsh-plugin-image-workbench 1.1.5 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.en.md +5 -3
- package/README.md +7 -3
- package/client.js +241 -241
- package/cordis.patch.yml +14 -14
- package/host.js +51 -0
- package/install.mjs +333 -387
- package/package.json +3 -2
- package/scripts/fetch-style-images.mjs +104 -0
- package/scripts/vendor-snapshot.py +154 -154
- package/server/access.py +26 -26
- package/server/app.py +1371 -1329
- package/server/catalog.py +147 -147
- package/server/composer.py +391 -391
- package/server/drafts.py +158 -158
- package/server/history.py +575 -575
- package/server/imaging.py +172 -172
- package/server/pool.py +1335 -1335
- package/server/pool_admin.py +921 -921
- package/server/pool_service.py +1073 -1073
- package/server/requirements.txt +6 -6
- package/server/smoke_check.py +402 -402
- package/server/static/access.html +202 -202
- package/server/static/index.html +15 -0
- package/vendor/chatgpt2api/source/.env.example +82 -82
- package/vendor/chatgpt2api/source/.python-version +1 -1
- package/vendor/chatgpt2api/source/LICENSE +21 -21
- package/vendor/chatgpt2api/source/README.md +391 -391
- package/vendor/chatgpt2api/source/VERSION +1 -1
- package/vendor/chatgpt2api/source/api/__init__.py +2 -2
- package/vendor/chatgpt2api/source/api/accounts.py +537 -537
- package/vendor/chatgpt2api/source/api/ai.py +217 -217
- package/vendor/chatgpt2api/source/api/app.py +62 -62
- package/vendor/chatgpt2api/source/api/errors.py +46 -46
- package/vendor/chatgpt2api/source/api/image_inputs.py +309 -309
- package/vendor/chatgpt2api/source/api/image_tasks.py +120 -120
- package/vendor/chatgpt2api/source/api/support.py +134 -134
- package/vendor/chatgpt2api/source/api/system.py +365 -365
- package/vendor/chatgpt2api/source/assets/kunrouai.svg +50 -50
- package/vendor/chatgpt2api/source/config.default.json +108 -108
- package/vendor/chatgpt2api/source/main.py +9 -9
- package/vendor/chatgpt2api/source/pyproject.toml +27 -27
- package/vendor/chatgpt2api/source/scripts/init_proxy_config.py +186 -186
- package/vendor/chatgpt2api/source/scripts/migrate_storage.py +163 -163
- package/vendor/chatgpt2api/source/scripts/privoxy-warp.conf +13 -13
- package/vendor/chatgpt2api/source/scripts/test_outlook_token_mailbox.py +385 -385
- package/vendor/chatgpt2api/source/scripts/test_storage.py +129 -129
- package/vendor/chatgpt2api/source/scripts/verify_oauth_refresh.py +92 -92
- package/vendor/chatgpt2api/source/services/account_service.py +1757 -1757
- package/vendor/chatgpt2api/source/services/auth_service.py +253 -253
- package/vendor/chatgpt2api/source/services/backup_service.py +671 -671
- package/vendor/chatgpt2api/source/services/config.py +667 -667
- package/vendor/chatgpt2api/source/services/content_filter.py +242 -242
- package/vendor/chatgpt2api/source/services/cpa_service.py +315 -315
- package/vendor/chatgpt2api/source/services/editable_file_task_service.py +260 -260
- package/vendor/chatgpt2api/source/services/image_service.py +376 -376
- package/vendor/chatgpt2api/source/services/image_storage_service.py +405 -405
- package/vendor/chatgpt2api/source/services/image_tags_service.py +76 -76
- package/vendor/chatgpt2api/source/services/image_task_service.py +550 -550
- package/vendor/chatgpt2api/source/services/log_service.py +337 -337
- package/vendor/chatgpt2api/source/services/model_service.py +182 -182
- package/vendor/chatgpt2api/source/services/oauth_login_service.py +267 -267
- package/vendor/chatgpt2api/source/services/openai_backend_api.py +2763 -2763
- package/vendor/chatgpt2api/source/services/openai_oauth.py +32 -32
- package/vendor/chatgpt2api/source/services/protocol/__init__.py +2 -2
- package/vendor/chatgpt2api/source/services/protocol/anthropic_v1_messages.py +307 -307
- package/vendor/chatgpt2api/source/services/protocol/chat_completion_cache.py +238 -238
- package/vendor/chatgpt2api/source/services/protocol/conversation.py +1644 -1644
- package/vendor/chatgpt2api/source/services/protocol/error_response.py +124 -124
- package/vendor/chatgpt2api/source/services/protocol/openai_search.py +19 -19
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_chat_complete.py +316 -316
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_edit.py +86 -86
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_generations.py +42 -42
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_models.py +53 -53
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_response.py +453 -453
- package/vendor/chatgpt2api/source/services/protocol/web_search_tool.py +160 -160
- package/vendor/chatgpt2api/source/services/proxy_service.py +648 -648
- package/vendor/chatgpt2api/source/services/storage/__init__.py +5 -5
- package/vendor/chatgpt2api/source/services/storage/base.py +38 -38
- package/vendor/chatgpt2api/source/services/storage/database_storage.py +194 -194
- package/vendor/chatgpt2api/source/services/storage/factory.py +104 -104
- package/vendor/chatgpt2api/source/services/storage/git_storage.py +183 -183
- package/vendor/chatgpt2api/source/services/storage/json_storage.py +95 -95
- package/vendor/chatgpt2api/source/services/sub2api_service.py +531 -531
- package/vendor/chatgpt2api/source/utils/helper.py +469 -469
- package/vendor/chatgpt2api/source/utils/image_tokens.py +346 -346
- package/vendor/chatgpt2api/source/utils/log.py +110 -110
- package/vendor/chatgpt2api/source/utils/pkce.py +19 -19
- package/vendor/chatgpt2api/source/utils/pow.py +207 -207
- package/vendor/chatgpt2api/source/utils/sentinel.py +159 -159
- package/vendor/chatgpt2api/source/utils/turnstile.py +184 -184
- package/vendor/chatgpt2api/source/uv.lock +904 -904
- package/vendor/chatgpt2api/source/web/bun.lock +1501 -1501
- package/vendor/chatgpt2api/source/web/components.json +24 -24
- package/vendor/chatgpt2api/source/web/eslint.config.mjs +17 -17
- package/vendor/chatgpt2api/source/web/next.config.ts +40 -40
- package/vendor/chatgpt2api/source/web/package.json +58 -58
- package/vendor/chatgpt2api/source/web/postcss.config.mjs +5 -5
- package/vendor/chatgpt2api/source/web/src/app/accounts/components/account-import-dialog.tsx +925 -925
- package/vendor/chatgpt2api/source/web/src/app/accounts/page.tsx +1307 -1307
- package/vendor/chatgpt2api/source/web/src/app/debug/components/chat-panel.tsx +224 -224
- package/vendor/chatgpt2api/source/web/src/app/debug/components/editable-file-panel.tsx +444 -444
- package/vendor/chatgpt2api/source/web/src/app/debug/components/ppt-panel.tsx +9 -9
- package/vendor/chatgpt2api/source/web/src/app/debug/components/psd-panel.tsx +9 -9
- package/vendor/chatgpt2api/source/web/src/app/debug/components/search-panel.tsx +172 -172
- package/vendor/chatgpt2api/source/web/src/app/debug/components/skill-panel.tsx +169 -169
- package/vendor/chatgpt2api/source/web/src/app/debug/components/types.ts +39 -39
- package/vendor/chatgpt2api/source/web/src/app/debug/page.tsx +59 -59
- package/vendor/chatgpt2api/source/web/src/app/globals.css +230 -230
- package/vendor/chatgpt2api/source/web/src/app/image/components/image-composer.tsx +542 -542
- package/vendor/chatgpt2api/source/web/src/app/image/components/image-results.tsx +554 -554
- package/vendor/chatgpt2api/source/web/src/app/image/components/image-sidebar.tsx +176 -176
- package/vendor/chatgpt2api/source/web/src/app/image/page.tsx +1799 -1799
- package/vendor/chatgpt2api/source/web/src/app/image-manager/page.tsx +732 -732
- package/vendor/chatgpt2api/source/web/src/app/layout.tsx +50 -50
- package/vendor/chatgpt2api/source/web/src/app/login/page.tsx +101 -101
- package/vendor/chatgpt2api/source/web/src/app/logs/page.tsx +356 -356
- package/vendor/chatgpt2api/source/web/src/app/page.tsx +30 -30
- package/vendor/chatgpt2api/source/web/src/app/settings/components/api-docs-card.tsx +341 -341
- package/vendor/chatgpt2api/source/web/src/app/settings/components/backup-settings-card.tsx +447 -447
- package/vendor/chatgpt2api/source/web/src/app/settings/components/base-url-card.tsx +71 -71
- package/vendor/chatgpt2api/source/web/src/app/settings/components/config-card.tsx +491 -491
- package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pool-dialog.tsx +108 -108
- package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pools-card.tsx +169 -169
- package/vendor/chatgpt2api/source/web/src/app/settings/components/import-browser-dialog.tsx +185 -185
- package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-runtime-card.tsx +379 -379
- package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings-card.tsx +127 -127
- package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings.tsx +200 -200
- package/vendor/chatgpt2api/source/web/src/app/settings/components/settings-header.tsx +12 -12
- package/vendor/chatgpt2api/source/web/src/app/settings/components/sub2api-connections.tsx +886 -886
- package/vendor/chatgpt2api/source/web/src/app/settings/components/third-party-apps-card.tsx +81 -81
- package/vendor/chatgpt2api/source/web/src/app/settings/components/user-keys-card.tsx +422 -422
- package/vendor/chatgpt2api/source/web/src/app/settings/page.tsx +135 -135
- package/vendor/chatgpt2api/source/web/src/app/settings/store.ts +1069 -1069
- package/vendor/chatgpt2api/source/web/src/components/date-range-filter.tsx +49 -49
- package/vendor/chatgpt2api/source/web/src/components/header-actions.tsx +24 -24
- package/vendor/chatgpt2api/source/web/src/components/image-lightbox.tsx +434 -434
- package/vendor/chatgpt2api/source/web/src/components/image-thumbnail.tsx +46 -46
- package/vendor/chatgpt2api/source/web/src/components/theme-script.tsx +18 -18
- package/vendor/chatgpt2api/source/web/src/components/theme-toggle.tsx +14 -14
- package/vendor/chatgpt2api/source/web/src/components/top-nav.tsx +263 -263
- package/vendor/chatgpt2api/source/web/src/components/ui/animated-theme-toggler.tsx +256 -256
- package/vendor/chatgpt2api/source/web/src/components/ui/badge.tsx +41 -41
- package/vendor/chatgpt2api/source/web/src/components/ui/button.tsx +59 -59
- package/vendor/chatgpt2api/source/web/src/components/ui/calendar.tsx +46 -46
- package/vendor/chatgpt2api/source/web/src/components/ui/card.tsx +61 -61
- package/vendor/chatgpt2api/source/web/src/components/ui/checkbox.tsx +27 -27
- package/vendor/chatgpt2api/source/web/src/components/ui/dia-text-reveal.tsx +267 -267
- package/vendor/chatgpt2api/source/web/src/components/ui/dialog.tsx +132 -132
- package/vendor/chatgpt2api/source/web/src/components/ui/field.tsx +13 -13
- package/vendor/chatgpt2api/source/web/src/components/ui/input.tsx +19 -19
- package/vendor/chatgpt2api/source/web/src/components/ui/label.tsx +24 -24
- package/vendor/chatgpt2api/source/web/src/components/ui/morphing-text.tsx +149 -149
- package/vendor/chatgpt2api/source/web/src/components/ui/popover.tsx +38 -38
- package/vendor/chatgpt2api/source/web/src/components/ui/select.tsx +175 -175
- package/vendor/chatgpt2api/source/web/src/components/ui/sheet.tsx +143 -143
- package/vendor/chatgpt2api/source/web/src/components/ui/table.tsx +29 -29
- package/vendor/chatgpt2api/source/web/src/components/ui/tabs.tsx +91 -91
- package/vendor/chatgpt2api/source/web/src/components/ui/text-3d-flip.tsx +269 -269
- package/vendor/chatgpt2api/source/web/src/components/ui/textarea.tsx +18 -18
- package/vendor/chatgpt2api/source/web/src/components/version-release-dialog.tsx +127 -127
- package/vendor/chatgpt2api/source/web/src/constants/common-env.ts +6 -6
- package/vendor/chatgpt2api/source/web/src/hooks/use-version-check.ts +85 -85
- package/vendor/chatgpt2api/source/web/src/lib/api.ts +926 -926
- package/vendor/chatgpt2api/source/web/src/lib/auth-session.ts +26 -26
- package/vendor/chatgpt2api/source/web/src/lib/release.ts +25 -25
- package/vendor/chatgpt2api/source/web/src/lib/request.ts +93 -93
- package/vendor/chatgpt2api/source/web/src/lib/use-auth-guard.ts +89 -89
- package/vendor/chatgpt2api/source/web/src/lib/utils.ts +6 -6
- package/vendor/chatgpt2api/source/web/src/store/auth.ts +112 -112
- package/vendor/chatgpt2api/source/web/src/store/editable-file-history.ts +33 -33
- package/vendor/chatgpt2api/source/web/src/store/image-conversations.ts +318 -318
- package/vendor/chatgpt2api/source/web/tsconfig.json +42 -42
|
@@ -1,732 +1,732 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
-
import { CalendarDays, ChevronLeft, ChevronRight, Copy, Download, ImageIcon, LoaderCircle, Maximize2, Plus, RefreshCw, Search, Tag, Trash2, X } from "lucide-react";
|
|
5
|
-
import { toast } from "sonner";
|
|
6
|
-
|
|
7
|
-
import { DateRangeFilter } from "@/components/date-range-filter";
|
|
8
|
-
import { ImageLightbox } from "@/components/image-lightbox";
|
|
9
|
-
import { Badge } from "@/components/ui/badge";
|
|
10
|
-
import { Button } from "@/components/ui/button";
|
|
11
|
-
import { Card, CardContent } from "@/components/ui/card";
|
|
12
|
-
import { Checkbox } from "@/components/ui/checkbox";
|
|
13
|
-
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
|
14
|
-
import { Input } from "@/components/ui/input";
|
|
15
|
-
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
|
16
|
-
import { compressAllImages, deleteImageTag, deleteManagedImages, deleteToTarget, downloadImages, downloadSingleImage, fetchImageStorage, fetchImageTags, fetchManagedImages, setImageTags, type ImageStorageStats, type ManagedImage } from "@/lib/api";
|
|
17
|
-
import { useAuthGuard } from "@/lib/use-auth-guard";
|
|
18
|
-
|
|
19
|
-
const LONG_PRESS_MS = 800;
|
|
20
|
-
const IMAGE_MANAGER_CHECKBOX_CLASS = "border-stone-300 bg-white/80 dark:border-white/35 dark:bg-white/5 data-[state=checked]:border-stone-950 dark:data-[state=checked]:border-white";
|
|
21
|
-
|
|
22
|
-
function formatSize(size: number) {
|
|
23
|
-
return size > 1024 * 1024 ? `${(size / 1024 / 1024).toFixed(2)} MB` : `${Math.ceil(size / 1024)} KB`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function imageKey(item: ManagedImage) {
|
|
27
|
-
return item.rel || item.url;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function useLongPress(onLongPress: () => void, ms = LONG_PRESS_MS) {
|
|
31
|
-
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
32
|
-
const activeRef = useRef(false);
|
|
33
|
-
|
|
34
|
-
const start = useCallback((e: React.MouseEvent | React.TouchEvent) => {
|
|
35
|
-
activeRef.current = true;
|
|
36
|
-
timerRef.current = setTimeout(() => {
|
|
37
|
-
if (activeRef.current) {
|
|
38
|
-
onLongPress();
|
|
39
|
-
}
|
|
40
|
-
}, ms);
|
|
41
|
-
}, [onLongPress, ms]);
|
|
42
|
-
|
|
43
|
-
const stop = useCallback(() => {
|
|
44
|
-
activeRef.current = false;
|
|
45
|
-
if (timerRef.current) {
|
|
46
|
-
clearTimeout(timerRef.current);
|
|
47
|
-
timerRef.current = null;
|
|
48
|
-
}
|
|
49
|
-
}, []);
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
onMouseDown: start,
|
|
53
|
-
onMouseUp: stop,
|
|
54
|
-
onMouseLeave: stop,
|
|
55
|
-
onTouchStart: start,
|
|
56
|
-
onTouchEnd: stop,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function ImageManagerContent() {
|
|
61
|
-
const [items, setItems] = useState<ManagedImage[]>([]);
|
|
62
|
-
const [startDate, setStartDate] = useState("");
|
|
63
|
-
const [endDate, setEndDate] = useState("");
|
|
64
|
-
const [lightboxIndex, setLightboxIndex] = useState(0);
|
|
65
|
-
const [lightboxOpen, setLightboxOpen] = useState(false);
|
|
66
|
-
const [page, setPage] = useState(1);
|
|
67
|
-
const [isLoading, setIsLoading] = useState(true);
|
|
68
|
-
const [deleteStartDate, setDeleteStartDate] = useState("");
|
|
69
|
-
const [deleteTarget, setDeleteTarget] = useState<ManagedImage | null>(null);
|
|
70
|
-
const [isDeleting, setIsDeleting] = useState(false);
|
|
71
|
-
const [allTags, setAllTags] = useState<string[]>([]);
|
|
72
|
-
const [storage, setStorage] = useState<ImageStorageStats | null>(null);
|
|
73
|
-
const [storageLoading, setStorageLoading] = useState(false);
|
|
74
|
-
const [compressResult, setCompressResult] = useState<string>("");
|
|
75
|
-
const [targetFreeMb, setTargetFreeMb] = useState(500);
|
|
76
|
-
|
|
77
|
-
const loadStorage = useCallback(async () => {
|
|
78
|
-
try {
|
|
79
|
-
setStorageLoading(true);
|
|
80
|
-
const data = await fetchImageStorage();
|
|
81
|
-
setStorage(data);
|
|
82
|
-
} catch { /* ignore */ }
|
|
83
|
-
finally { setStorageLoading(false); }
|
|
84
|
-
}, []);
|
|
85
|
-
|
|
86
|
-
useEffect(() => { void loadStorage(); }, [loadStorage]);
|
|
87
|
-
const [selectedTags, setSelectedTags] = useState<string[]>([]);
|
|
88
|
-
const [tagEditTarget, setTagEditTarget] = useState<ManagedImage | null>(null);
|
|
89
|
-
const [tagInput, setTagInput] = useState("");
|
|
90
|
-
const [dialogVisible, setDialogVisible] = useState(false);
|
|
91
|
-
const deleteTargetRef = useRef<ManagedImage | null>(null);
|
|
92
|
-
const [selectedPaths, setSelectedPaths] = useState<string[]>([]);
|
|
93
|
-
const [deleteMode, setDeleteMode] = useState<"selected" | "filtered" | "byDate" | null>(null);
|
|
94
|
-
const [isDownloading, setIsDownloading] = useState(false);
|
|
95
|
-
|
|
96
|
-
const filteredItems = selectedTags.length > 0
|
|
97
|
-
? items.filter((item) => selectedTags.every((t) => (item.tags ?? []).includes(t)))
|
|
98
|
-
: items;
|
|
99
|
-
|
|
100
|
-
const lightboxImages = filteredItems.map((item) => ({
|
|
101
|
-
id: item.name,
|
|
102
|
-
src: item.url,
|
|
103
|
-
sizeLabel: formatSize(item.size),
|
|
104
|
-
dimensions: item.width && item.height ? `${item.width} x ${item.height}` : undefined,
|
|
105
|
-
}));
|
|
106
|
-
const pageSize = 12;
|
|
107
|
-
const pageCount = Math.max(1, Math.ceil(filteredItems.length / pageSize));
|
|
108
|
-
const safePage = Math.min(page, pageCount);
|
|
109
|
-
const currentRows = filteredItems.slice((safePage - 1) * pageSize, safePage * pageSize);
|
|
110
|
-
const selectedSet = useMemo(() => new Set(selectedPaths), [selectedPaths]);
|
|
111
|
-
const selectedCount = deleteMode === "filtered" ? items.length : deleteMode === "byDate" ? 0 : selectedPaths.length;
|
|
112
|
-
const currentPageSelected = currentRows.length > 0 && currentRows.every((item) => selectedSet.has(imageKey(item)));
|
|
113
|
-
const allSelected = filteredItems.length > 0 && filteredItems.every((item) => selectedSet.has(imageKey(item)));
|
|
114
|
-
|
|
115
|
-
const loadImages = async () => {
|
|
116
|
-
setIsLoading(true);
|
|
117
|
-
try {
|
|
118
|
-
const [data, tagsData] = await Promise.all([
|
|
119
|
-
fetchManagedImages({ start_date: startDate, end_date: endDate }),
|
|
120
|
-
fetchImageTags(),
|
|
121
|
-
]);
|
|
122
|
-
setItems(data.items);
|
|
123
|
-
setAllTags(tagsData.tags);
|
|
124
|
-
setSelectedPaths((current) => current.filter((path) => data.items.some((item) => imageKey(item) === path)));
|
|
125
|
-
setPage(1);
|
|
126
|
-
} catch (error) {
|
|
127
|
-
toast.error(error instanceof Error ? error.message : "加载图片失败");
|
|
128
|
-
} finally {
|
|
129
|
-
setIsLoading(false);
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const closeDialog = useCallback(() => {
|
|
134
|
-
setDialogVisible(false);
|
|
135
|
-
setTimeout(() => setDeleteTarget(null), 200);
|
|
136
|
-
}, []);
|
|
137
|
-
|
|
138
|
-
const openDeleteDialog = useCallback((item: ManagedImage) => {
|
|
139
|
-
deleteTargetRef.current = item;
|
|
140
|
-
setDeleteTarget(item);
|
|
141
|
-
setDialogVisible(true);
|
|
142
|
-
}, []);
|
|
143
|
-
|
|
144
|
-
const handleDelete = async () => {
|
|
145
|
-
if (!deleteTarget) return;
|
|
146
|
-
setIsDeleting(true);
|
|
147
|
-
try {
|
|
148
|
-
await deleteManagedImages({ paths: [deleteTarget.rel] });
|
|
149
|
-
setItems((prev) => prev.filter((item) => item.rel !== deleteTarget.rel));
|
|
150
|
-
setSelectedPaths((prev) => prev.filter((p) => p !== imageKey(deleteTarget)));
|
|
151
|
-
toast.success("图片已删除");
|
|
152
|
-
} catch (error) {
|
|
153
|
-
toast.error(error instanceof Error ? error.message : "删除失败");
|
|
154
|
-
} finally {
|
|
155
|
-
setIsDeleting(false);
|
|
156
|
-
closeDialog();
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
const handleSetTags = async (item: ManagedImage, tags: string[]) => {
|
|
161
|
-
try {
|
|
162
|
-
const result = await setImageTags(item.rel, tags);
|
|
163
|
-
setItems((prev) => prev.map((i) => i.rel === item.rel ? { ...i, tags: result.tags } : i));
|
|
164
|
-
const tagsData = await fetchImageTags();
|
|
165
|
-
setAllTags(tagsData.tags);
|
|
166
|
-
} catch (error) {
|
|
167
|
-
toast.error(error instanceof Error ? error.message : "设置标签失败");
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
const handleAddTag = (item: ManagedImage) => {
|
|
172
|
-
const tag = tagInput.trim();
|
|
173
|
-
if (!tag) return;
|
|
174
|
-
const current = item.tags ?? [];
|
|
175
|
-
if (current.includes(tag)) {
|
|
176
|
-
toast.error("标签已存在");
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
void handleSetTags(item, [...current, tag]);
|
|
180
|
-
setTagInput("");
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
const handleRemoveTag = (item: ManagedImage, tag: string) => {
|
|
184
|
-
void handleSetTags(item, (item.tags ?? []).filter((t) => t !== tag));
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
const toggleFilterTag = (tag: string) => {
|
|
188
|
-
setSelectedTags((prev) => prev.includes(tag) ? prev.filter((t) => t !== tag) : [...prev, tag]);
|
|
189
|
-
setPage(1);
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
const [pressingTag, setPressingTag] = useState<string | null>(null);
|
|
193
|
-
const pressTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
194
|
-
const [tagDeleteTarget, setTagDeleteTarget] = useState<string | null>(null);
|
|
195
|
-
|
|
196
|
-
const handleDeleteTag = async (tag: string) => {
|
|
197
|
-
try {
|
|
198
|
-
const result = await deleteImageTag(tag);
|
|
199
|
-
setAllTags((prev) => prev.filter((t) => t !== tag));
|
|
200
|
-
setSelectedTags((prev) => prev.filter((t) => t !== tag));
|
|
201
|
-
setItems((prev) => prev.map((item) => ({
|
|
202
|
-
...item,
|
|
203
|
-
tags: (item.tags ?? []).filter((t) => t !== tag),
|
|
204
|
-
})));
|
|
205
|
-
toast.success(`标签"${tag}"已删除,影响 ${result.removed_from} 张图片`);
|
|
206
|
-
} catch (error) {
|
|
207
|
-
toast.error(error instanceof Error ? error.message : "删除标签失败");
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
const startTagPress = useCallback((tag: string) => {
|
|
212
|
-
setPressingTag(tag);
|
|
213
|
-
pressTimerRef.current = setTimeout(() => {
|
|
214
|
-
setPressingTag(null);
|
|
215
|
-
setTagDeleteTarget(tag);
|
|
216
|
-
}, LONG_PRESS_MS);
|
|
217
|
-
}, []);
|
|
218
|
-
|
|
219
|
-
const stopTagPress = useCallback(() => {
|
|
220
|
-
setPressingTag(null);
|
|
221
|
-
if (pressTimerRef.current) {
|
|
222
|
-
clearTimeout(pressTimerRef.current);
|
|
223
|
-
pressTimerRef.current = null;
|
|
224
|
-
}
|
|
225
|
-
}, []);
|
|
226
|
-
|
|
227
|
-
const clearFilters = () => {
|
|
228
|
-
setStartDate("");
|
|
229
|
-
setEndDate("");
|
|
230
|
-
setSelectedTags([]);
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
const togglePaths = (paths: string[], checked: boolean) => {
|
|
234
|
-
setSelectedPaths((current) => checked ? Array.from(new Set([...current, ...paths])) : current.filter((path) => !paths.includes(path)));
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
const confirmDelete = async () => {
|
|
238
|
-
if (!deleteMode || selectedCount === 0) return;
|
|
239
|
-
setIsDeleting(true);
|
|
240
|
-
try {
|
|
241
|
-
const data = await deleteManagedImages(deleteMode === "filtered" ? { start_date: startDate, end_date: endDate, all_matching: true } : { paths: selectedPaths });
|
|
242
|
-
toast.success(`已删除 ${data.removed} 张图片`);
|
|
243
|
-
setDeleteMode(null);
|
|
244
|
-
setSelectedPaths([]);
|
|
245
|
-
await loadImages();
|
|
246
|
-
} catch (error) {
|
|
247
|
-
toast.error(error instanceof Error ? error.message : "删除图片失败");
|
|
248
|
-
} finally {
|
|
249
|
-
setIsDeleting(false);
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
const handleBatchDownload = async () => {
|
|
254
|
-
const paths = deleteMode === "filtered" ? items.map((item) => item.rel) : selectedPaths;
|
|
255
|
-
if (paths.length === 0) return;
|
|
256
|
-
setIsDownloading(true);
|
|
257
|
-
try {
|
|
258
|
-
await downloadImages(paths);
|
|
259
|
-
toast.success(`已下载 ${paths.length} 张图片`);
|
|
260
|
-
} catch (error) {
|
|
261
|
-
toast.error(error instanceof Error ? error.message : "下载失败");
|
|
262
|
-
} finally {
|
|
263
|
-
setIsDownloading(false);
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
const handleSingleDownload = async (item: ManagedImage) => {
|
|
268
|
-
await downloadSingleImage(item.rel);
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
useEffect(() => {
|
|
272
|
-
void loadImages();
|
|
273
|
-
}, [startDate, endDate]);
|
|
274
|
-
|
|
275
|
-
return (
|
|
276
|
-
<section className="space-y-5">
|
|
277
|
-
<div className="flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between">
|
|
278
|
-
<div className="space-y-1">
|
|
279
|
-
<div className="text-xs font-semibold tracking-[0.18em] text-stone-500 uppercase">Images</div>
|
|
280
|
-
<h1 className="text-2xl font-semibold tracking-tight">图片管理</h1>
|
|
281
|
-
</div>
|
|
282
|
-
<div className="flex flex-wrap gap-2">
|
|
283
|
-
<DateRangeFilter startDate={startDate} endDate={endDate} onChange={(start, end) => { setStartDate(start); setEndDate(end); }} />
|
|
284
|
-
<Button variant="outline" onClick={clearFilters} className="h-10 rounded-xl border-stone-200 bg-white px-4 text-stone-700">
|
|
285
|
-
清除筛选条件
|
|
286
|
-
</Button>
|
|
287
|
-
<Button onClick={() => void loadImages()} disabled={isLoading} className="h-10 rounded-xl bg-stone-950 px-4 text-white hover:bg-stone-800">
|
|
288
|
-
{isLoading ? <LoaderCircle className="size-4 animate-spin" /> : <Search className="size-4" />}
|
|
289
|
-
查询
|
|
290
|
-
</Button>
|
|
291
|
-
<Button variant="outline" onClick={() => setDeleteMode("filtered")} disabled={isDeleting || items.length === 0 || (!startDate && !endDate)} className="h-10 rounded-xl border-rose-200 bg-white px-4 text-rose-600 hover:bg-rose-50">
|
|
292
|
-
<Trash2 className="size-4" />
|
|
293
|
-
删除匹配日期
|
|
294
|
-
</Button>
|
|
295
|
-
</div>
|
|
296
|
-
</div>
|
|
297
|
-
|
|
298
|
-
{allTags.length > 0 ? (
|
|
299
|
-
<div className="flex flex-wrap items-center gap-2">
|
|
300
|
-
<span className="text-xs font-medium text-stone-500">
|
|
301
|
-
<Tag className="mr-1 inline size-3.5" />
|
|
302
|
-
标签筛选:
|
|
303
|
-
</span>
|
|
304
|
-
{allTags.map((tag) => {
|
|
305
|
-
const isPressing = pressingTag === tag;
|
|
306
|
-
return (
|
|
307
|
-
<span
|
|
308
|
-
key={tag}
|
|
309
|
-
className="relative inline-flex items-center"
|
|
310
|
-
onMouseDown={() => startTagPress(tag)}
|
|
311
|
-
onMouseUp={stopTagPress}
|
|
312
|
-
onMouseLeave={stopTagPress}
|
|
313
|
-
onTouchStart={() => startTagPress(tag)}
|
|
314
|
-
onTouchEnd={stopTagPress}
|
|
315
|
-
>
|
|
316
|
-
<button
|
|
317
|
-
type="button"
|
|
318
|
-
onClick={() => toggleFilterTag(tag)}
|
|
319
|
-
>
|
|
320
|
-
<Badge
|
|
321
|
-
variant={selectedTags.includes(tag) ? "default" : "outline"}
|
|
322
|
-
className={`cursor-pointer rounded-md transition-all hover:opacity-80 ${isPressing ? "ring-2 ring-red-400 ring-offset-1" : ""}`}
|
|
323
|
-
>
|
|
324
|
-
{tag}
|
|
325
|
-
</Badge>
|
|
326
|
-
</button>
|
|
327
|
-
{isPressing ? (
|
|
328
|
-
<span className="pointer-events-none absolute inset-0 overflow-hidden rounded-md">
|
|
329
|
-
<span className="absolute inset-0 animate-[grow_800ms_linear_forwards] rounded-md bg-red-400/20" />
|
|
330
|
-
</span>
|
|
331
|
-
) : null}
|
|
332
|
-
</span>
|
|
333
|
-
);
|
|
334
|
-
})}
|
|
335
|
-
{selectedTags.length > 0 ? (
|
|
336
|
-
<button type="button" onClick={() => setSelectedTags([])}>
|
|
337
|
-
<Badge variant="secondary" className="cursor-pointer rounded-md">
|
|
338
|
-
<X className="mr-0.5 size-3" />
|
|
339
|
-
清除
|
|
340
|
-
</Badge>
|
|
341
|
-
</button>
|
|
342
|
-
) : null}
|
|
343
|
-
</div>
|
|
344
|
-
) : null}
|
|
345
|
-
|
|
346
|
-
{/* Storage Stats Panel */}
|
|
347
|
-
<div className="grid grid-cols-2 sm:grid-cols-4 lg:grid-cols-6 gap-3 mb-4">
|
|
348
|
-
{storage ? (
|
|
349
|
-
<>
|
|
350
|
-
<div className="rounded-xl border border-stone-200 bg-white/80 p-3">
|
|
351
|
-
<div className="text-xs text-stone-500">磁盘总量</div>
|
|
352
|
-
<div className="text-lg font-bold text-stone-800">{storage.disk_total_mb >= 1024 ? `${(storage.disk_total_mb / 1024).toFixed(1)} GB` : `${storage.disk_total_mb} MB`}</div>
|
|
353
|
-
</div>
|
|
354
|
-
<div className="rounded-xl border border-stone-200 bg-white/80 p-3">
|
|
355
|
-
<div className="text-xs text-stone-500">剩余空间</div>
|
|
356
|
-
<div className={`text-lg font-bold ${storage.disk_free_mb < 200 ? "text-red-500" : storage.disk_free_mb < 500 ? "text-yellow-500" : "text-green-600"}`}>{storage.disk_free_mb >= 1024 ? `${(storage.disk_free_mb / 1024).toFixed(1)} GB` : `${storage.disk_free_mb} MB`}</div>
|
|
357
|
-
</div>
|
|
358
|
-
<div className="rounded-xl border border-stone-200 bg-white/80 p-3">
|
|
359
|
-
<div className="text-xs text-stone-500">图片数量</div>
|
|
360
|
-
<div className="text-lg font-bold text-stone-800">{storage.image_count}</div>
|
|
361
|
-
</div>
|
|
362
|
-
<div className="rounded-xl border border-stone-200 bg-white/80 p-3">
|
|
363
|
-
<div className="text-xs text-stone-500">图片占用</div>
|
|
364
|
-
<div className="text-lg font-bold text-stone-800">{storage.image_size_mb >= 1024 ? `${(storage.image_size_mb / 1024).toFixed(1)} GB` : `${storage.image_size_mb} MB`}</div>
|
|
365
|
-
</div>
|
|
366
|
-
<div className="rounded-xl border border-stone-200 bg-white/80 p-3 col-span-2 flex items-center gap-2 flex-wrap">
|
|
367
|
-
<span className="text-xs text-stone-500 w-full">快捷操作</span>
|
|
368
|
-
<Button size="sm" variant="outline" className="h-7 text-xs" disabled={storageLoading} onClick={() => { void loadStorage(); }}>
|
|
369
|
-
<RefreshCw className={`size-3 mr-1 ${storageLoading ? "animate-spin" : ""}`} />刷新
|
|
370
|
-
</Button>
|
|
371
|
-
<Button size="sm" variant="outline" className="h-7 text-xs"
|
|
372
|
-
onClick={async () => {
|
|
373
|
-
try { const r = await compressAllImages(); setCompressResult(`已压缩${r.saved_mb}MB`); void loadStorage(); }
|
|
374
|
-
catch { setCompressResult("压缩失败"); }
|
|
375
|
-
}}>
|
|
376
|
-
🗜️ 压缩优化
|
|
377
|
-
</Button>
|
|
378
|
-
<Button size="sm" variant="outline" className="h-7 text-xs border-rose-200 text-rose-600"
|
|
379
|
-
onClick={() => setDeleteMode("byDate")}>
|
|
380
|
-
🗑️ 按日期删除
|
|
381
|
-
</Button>
|
|
382
|
-
<form onSubmit={async (e) => { e.preventDefault();
|
|
383
|
-
try {
|
|
384
|
-
const r = await deleteToTarget(targetFreeMb);
|
|
385
|
-
toast.success(`已删除 ${r.removed} 张图片,释放 ${r.freed_mb ?? 0}MB`);
|
|
386
|
-
void loadStorage();
|
|
387
|
-
} catch { toast.error("清理失败"); }
|
|
388
|
-
}} className="flex items-center gap-1">
|
|
389
|
-
<Button size="sm" variant="outline" className="h-7 text-xs border-amber-200 text-amber-700" type="submit">
|
|
390
|
-
🧹 清理至
|
|
391
|
-
</Button>
|
|
392
|
-
<Input className="h-7 w-14 text-xs text-center px-1" type="number" min={50} value={targetFreeMb}
|
|
393
|
-
onChange={(e) => setTargetFreeMb(Number(e.target.value) || 500)} />
|
|
394
|
-
<span className="text-xs text-stone-400">MB 剩余</span>
|
|
395
|
-
</form>
|
|
396
|
-
{compressResult ? <span className="text-xs text-green-600 ml-1">{compressResult}</span> : null}
|
|
397
|
-
</div>
|
|
398
|
-
</>
|
|
399
|
-
) : (
|
|
400
|
-
<div className="rounded-xl border border-stone-200 bg-white/80 p-3 col-span-full text-center text-sm text-stone-400">
|
|
401
|
-
{storageLoading ? "加载存储信息..." : "存储信息加载失败"}
|
|
402
|
-
</div>
|
|
403
|
-
)}
|
|
404
|
-
</div>
|
|
405
|
-
|
|
406
|
-
{/* Delete by date dialog */}
|
|
407
|
-
<Dialog open={deleteMode === "byDate"} onOpenChange={() => setDeleteMode(null)}>
|
|
408
|
-
<DialogContent className="sm:max-w-md rounded-2xl">
|
|
409
|
-
<DialogHeader><DialogTitle>按日期删除图片</DialogTitle></DialogHeader>
|
|
410
|
-
<div className="space-y-4">
|
|
411
|
-
<div className="flex items-center gap-2">
|
|
412
|
-
<label className="text-sm text-stone-600 shrink-0">删除</label>
|
|
413
|
-
<Input className="h-9 text-sm" type="date" value={deleteStartDate} onChange={(e) => setDeleteStartDate(e.target.value)} />
|
|
414
|
-
<span className="text-sm text-stone-400">之前的图片</span>
|
|
415
|
-
</div>
|
|
416
|
-
<p className="text-xs text-stone-500">此操作不可撤销,将永久删除所有匹配日期的图片及其缩略图。</p>
|
|
417
|
-
</div>
|
|
418
|
-
<DialogFooter>
|
|
419
|
-
<Button variant="ghost" onClick={() => setDeleteMode(null)}>取消</Button>
|
|
420
|
-
<Button variant="destructive" disabled={!deleteStartDate || isDeleting}
|
|
421
|
-
onClick={async () => {
|
|
422
|
-
if (!deleteStartDate) return;
|
|
423
|
-
try {
|
|
424
|
-
setIsDeleting(true);
|
|
425
|
-
const r = await deleteManagedImages({ end_date: deleteStartDate, all_matching: true });
|
|
426
|
-
toast.success(`已删除 ${r.removed} 张图片`);
|
|
427
|
-
setDeleteMode(null);
|
|
428
|
-
void loadStorage();
|
|
429
|
-
void loadImages();
|
|
430
|
-
} catch { toast.error("删除失败"); }
|
|
431
|
-
finally { setIsDeleting(false); }
|
|
432
|
-
}}>
|
|
433
|
-
{isDeleting ? <LoaderCircle className="size-4 animate-spin" /> : null}
|
|
434
|
-
确认删除
|
|
435
|
-
</Button>
|
|
436
|
-
</DialogFooter>
|
|
437
|
-
</DialogContent>
|
|
438
|
-
</Dialog>
|
|
439
|
-
|
|
440
|
-
<Card className="rounded-2xl border-white/80 bg-white/90 shadow-sm">
|
|
441
|
-
<CardContent className="p-0">
|
|
442
|
-
<div className="flex flex-wrap items-center justify-between gap-3 border-b border-stone-100 px-5 py-4">
|
|
443
|
-
<div className="flex flex-wrap items-center gap-3 text-sm text-stone-600">
|
|
444
|
-
<ImageIcon className="size-4" />
|
|
445
|
-
共 {filteredItems.length} 张
|
|
446
|
-
{selectedTags.length > 0 ? <span className="text-stone-400">(筛选自 {items.length} 张)</span> : null}
|
|
447
|
-
<label className="flex items-center gap-2">
|
|
448
|
-
<Checkbox className={IMAGE_MANAGER_CHECKBOX_CLASS} checked={currentPageSelected} onCheckedChange={(checked) => togglePaths(currentRows.map(imageKey), Boolean(checked))} />
|
|
449
|
-
本页全选
|
|
450
|
-
</label>
|
|
451
|
-
<label className="flex items-center gap-2">
|
|
452
|
-
<Checkbox className={IMAGE_MANAGER_CHECKBOX_CLASS} checked={allSelected} onCheckedChange={(checked) => togglePaths(filteredItems.map(imageKey), Boolean(checked))} />
|
|
453
|
-
全选结果
|
|
454
|
-
</label>
|
|
455
|
-
{selectedPaths.length > 0 ? <span>已选 {selectedPaths.length} 张</span> : null}
|
|
456
|
-
</div>
|
|
457
|
-
<div className="flex items-center gap-2">
|
|
458
|
-
<Button variant="ghost" className="h-8 rounded-lg px-3 text-stone-500" onClick={() => void loadImages()} disabled={isLoading}>
|
|
459
|
-
<RefreshCw className={`size-4 ${isLoading ? "animate-spin" : ""}`} />
|
|
460
|
-
刷新
|
|
461
|
-
</Button>
|
|
462
|
-
<button type="button" className="text-sm text-stone-500 hover:text-stone-900 disabled:text-stone-300" onClick={() => setSelectedPaths([])} disabled={selectedPaths.length === 0 || isDeleting}>
|
|
463
|
-
取消选择
|
|
464
|
-
</button>
|
|
465
|
-
<Button variant="outline" className="h-8 rounded-lg border-stone-200 bg-white px-3 text-stone-600 hover:bg-stone-50" onClick={() => void handleBatchDownload()} disabled={selectedPaths.length === 0 || isDownloading || isDeleting}>
|
|
466
|
-
{isDownloading ? <LoaderCircle className="size-4 animate-spin" /> : <Download className="size-4" />}
|
|
467
|
-
下载所选
|
|
468
|
-
</Button>
|
|
469
|
-
<Button variant="outline" className="h-8 rounded-lg border-rose-200 bg-white px-3 text-rose-600 hover:bg-rose-50" onClick={() => setDeleteMode("selected")} disabled={selectedPaths.length === 0 || isDeleting}>
|
|
470
|
-
<Trash2 className="size-4" />
|
|
471
|
-
删除所选
|
|
472
|
-
</Button>
|
|
473
|
-
</div>
|
|
474
|
-
</div>
|
|
475
|
-
<div className="grid gap-0 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
|
|
476
|
-
{currentRows.map((item) => {
|
|
477
|
-
const imageIndex = filteredItems.findIndex((row) => row.url === item.url);
|
|
478
|
-
return (
|
|
479
|
-
<div key={item.rel} className="group border-r border-b border-stone-100 p-4 transition hover:bg-stone-50 dark:hover:bg-white/5">
|
|
480
|
-
<div className="relative">
|
|
481
|
-
<button
|
|
482
|
-
type="button"
|
|
483
|
-
className="relative block aspect-square w-full cursor-zoom-in overflow-hidden rounded-lg bg-stone-100 text-left"
|
|
484
|
-
onClick={() => {
|
|
485
|
-
setLightboxIndex(imageIndex);
|
|
486
|
-
setLightboxOpen(true);
|
|
487
|
-
}}
|
|
488
|
-
>
|
|
489
|
-
<img
|
|
490
|
-
src={item.thumbnail_url || item.url}
|
|
491
|
-
alt={item.name}
|
|
492
|
-
className="h-full w-full object-cover transition group-hover:scale-[1.02]"
|
|
493
|
-
onError={(event) => {
|
|
494
|
-
if (event.currentTarget.src !== item.url) {
|
|
495
|
-
event.currentTarget.src = item.url;
|
|
496
|
-
}
|
|
497
|
-
}}
|
|
498
|
-
/>
|
|
499
|
-
<span className="absolute right-2 bottom-2 rounded-full bg-black/50 p-2 text-white opacity-100 transition sm:opacity-0 sm:group-hover:opacity-100">
|
|
500
|
-
<Maximize2 className="size-4" />
|
|
501
|
-
</span>
|
|
502
|
-
</button>
|
|
503
|
-
<button
|
|
504
|
-
type="button"
|
|
505
|
-
className="absolute top-2 right-2 z-10 inline-flex size-7 items-center justify-center rounded-full bg-black/50 text-white opacity-100 transition hover:bg-red-600 sm:opacity-0 sm:group-hover:opacity-100"
|
|
506
|
-
title="删除图片"
|
|
507
|
-
onClick={(e) => {
|
|
508
|
-
e.stopPropagation();
|
|
509
|
-
openDeleteDialog(item);
|
|
510
|
-
}}
|
|
511
|
-
>
|
|
512
|
-
<Trash2 className="size-3.5" />
|
|
513
|
-
</button>
|
|
514
|
-
</div>
|
|
515
|
-
<div className="mt-3 space-y-2 text-xs text-stone-500">
|
|
516
|
-
<div className="flex items-center justify-between gap-2">
|
|
517
|
-
<div className="flex items-center gap-1 font-medium text-stone-700">
|
|
518
|
-
<CalendarDays className="size-3.5" />
|
|
519
|
-
{item.created_at}
|
|
520
|
-
</div>
|
|
521
|
-
<div className="flex items-center gap-1">
|
|
522
|
-
<Button
|
|
523
|
-
variant="ghost"
|
|
524
|
-
size="icon"
|
|
525
|
-
className="size-8 rounded-lg text-stone-400 hover:bg-stone-100 hover:text-stone-700"
|
|
526
|
-
onClick={() => void handleSingleDownload(item)}
|
|
527
|
-
title="下载图片"
|
|
528
|
-
>
|
|
529
|
-
<Download className="size-4" />
|
|
530
|
-
</Button>
|
|
531
|
-
<Button
|
|
532
|
-
variant="ghost"
|
|
533
|
-
size="icon"
|
|
534
|
-
className="size-8 rounded-lg text-stone-400 hover:bg-stone-100 hover:text-stone-700"
|
|
535
|
-
onClick={() => {
|
|
536
|
-
void navigator.clipboard.writeText(item.url);
|
|
537
|
-
toast.success("图片地址已复制");
|
|
538
|
-
}}
|
|
539
|
-
>
|
|
540
|
-
<Copy className="size-4" />
|
|
541
|
-
</Button>
|
|
542
|
-
<Checkbox className={IMAGE_MANAGER_CHECKBOX_CLASS} checked={selectedSet.has(imageKey(item))} onCheckedChange={(checked) => togglePaths([imageKey(item)], Boolean(checked))} />
|
|
543
|
-
</div>
|
|
544
|
-
</div>
|
|
545
|
-
<div className="flex items-center justify-between gap-2">
|
|
546
|
-
<span>{formatSize(item.size)}</span>
|
|
547
|
-
<span>{item.width && item.height ? `${item.width} x ${item.height}` : "-"}</span>
|
|
548
|
-
</div>
|
|
549
|
-
<div className="flex flex-wrap items-center gap-1">
|
|
550
|
-
{(item.tags ?? []).map((tag) => (
|
|
551
|
-
<Badge key={tag} variant="secondary" className="gap-0.5 rounded-md py-0 pr-0.5 text-[10px]">
|
|
552
|
-
{tag}
|
|
553
|
-
<button
|
|
554
|
-
type="button"
|
|
555
|
-
className="inline-flex size-3.5 items-center justify-center rounded-full hover:bg-stone-300"
|
|
556
|
-
onClick={() => handleRemoveTag(item, tag)}
|
|
557
|
-
>
|
|
558
|
-
<X className="size-2.5" />
|
|
559
|
-
</button>
|
|
560
|
-
</Badge>
|
|
561
|
-
))}
|
|
562
|
-
<Popover open={tagEditTarget?.rel === item.rel} onOpenChange={(open) => { setTagEditTarget(open ? item : null); setTagInput(""); }}>
|
|
563
|
-
<PopoverTrigger asChild>
|
|
564
|
-
<button
|
|
565
|
-
type="button"
|
|
566
|
-
className="inline-flex size-5 items-center justify-center rounded-full border border-dashed border-stone-300 text-stone-400 hover:border-stone-500 hover:text-stone-600"
|
|
567
|
-
title="添加标签"
|
|
568
|
-
>
|
|
569
|
-
<Plus className="size-3" />
|
|
570
|
-
</button>
|
|
571
|
-
</PopoverTrigger>
|
|
572
|
-
<PopoverContent align="start" className="w-56 p-2">
|
|
573
|
-
<div className="space-y-2">
|
|
574
|
-
<div className="text-xs font-medium text-stone-500">添加标签</div>
|
|
575
|
-
<div className="flex gap-1">
|
|
576
|
-
<Input
|
|
577
|
-
value={tagInput}
|
|
578
|
-
onChange={(e) => setTagInput(e.target.value)}
|
|
579
|
-
placeholder="输入标签名"
|
|
580
|
-
className="h-8 text-xs"
|
|
581
|
-
onKeyDown={(e) => {
|
|
582
|
-
if (e.key === "Enter") {
|
|
583
|
-
e.preventDefault();
|
|
584
|
-
handleAddTag(item);
|
|
585
|
-
}
|
|
586
|
-
}}
|
|
587
|
-
/>
|
|
588
|
-
<Button
|
|
589
|
-
size="icon"
|
|
590
|
-
variant="outline"
|
|
591
|
-
className="size-8 shrink-0"
|
|
592
|
-
onClick={() => handleAddTag(item)}
|
|
593
|
-
>
|
|
594
|
-
<Plus className="size-3.5" />
|
|
595
|
-
</Button>
|
|
596
|
-
</div>
|
|
597
|
-
{allTags.filter((t) => !(item.tags ?? []).includes(t)).length > 0 ? (
|
|
598
|
-
<div className="flex flex-wrap gap-1 border-t border-stone-100 pt-2">
|
|
599
|
-
{allTags.filter((t) => !(item.tags ?? []).includes(t)).map((tag) => (
|
|
600
|
-
<button
|
|
601
|
-
key={tag}
|
|
602
|
-
type="button"
|
|
603
|
-
onClick={() => {
|
|
604
|
-
void handleSetTags(item, [...(item.tags ?? []), tag]);
|
|
605
|
-
setTagEditTarget(null);
|
|
606
|
-
}}
|
|
607
|
-
>
|
|
608
|
-
<Badge variant="outline" className="cursor-pointer rounded-md text-[10px] hover:bg-stone-100">
|
|
609
|
-
{tag}
|
|
610
|
-
</Badge>
|
|
611
|
-
</button>
|
|
612
|
-
))}
|
|
613
|
-
</div>
|
|
614
|
-
) : null}
|
|
615
|
-
</div>
|
|
616
|
-
</PopoverContent>
|
|
617
|
-
</Popover>
|
|
618
|
-
</div>
|
|
619
|
-
</div>
|
|
620
|
-
</div>
|
|
621
|
-
)})}
|
|
622
|
-
</div>
|
|
623
|
-
<div className="flex items-center justify-end gap-2 border-t border-stone-100 px-4 py-3 text-sm text-stone-500">
|
|
624
|
-
<span>第 {safePage} / {pageCount} 页,共 {filteredItems.length} 张</span>
|
|
625
|
-
<Button variant="outline" size="icon" className="size-9 rounded-lg border-stone-200 bg-white" disabled={safePage <= 1} onClick={() => setPage((value) => Math.max(1, value - 1))}>
|
|
626
|
-
<ChevronLeft className="size-4" />
|
|
627
|
-
</Button>
|
|
628
|
-
<Button variant="outline" size="icon" className="size-9 rounded-lg border-stone-200 bg-white" disabled={safePage >= pageCount} onClick={() => setPage((value) => Math.min(pageCount, value + 1))}>
|
|
629
|
-
<ChevronRight className="size-4" />
|
|
630
|
-
</Button>
|
|
631
|
-
</div>
|
|
632
|
-
{!isLoading && filteredItems.length === 0 ? <div className="px-6 py-14 text-center text-sm text-stone-500">没有找到图片</div> : null}
|
|
633
|
-
</CardContent>
|
|
634
|
-
</Card>
|
|
635
|
-
|
|
636
|
-
<Dialog open={dialogVisible} onOpenChange={(open) => { if (!open) closeDialog(); }}>
|
|
637
|
-
<DialogContent className="max-w-sm overflow-hidden rounded-2xl">
|
|
638
|
-
<DialogHeader>
|
|
639
|
-
<DialogTitle className="pr-8">确认删除</DialogTitle>
|
|
640
|
-
</DialogHeader>
|
|
641
|
-
<p className="text-sm text-stone-600">
|
|
642
|
-
确定要删除这张图片吗?此操作不可恢复。
|
|
643
|
-
</p>
|
|
644
|
-
{deleteTarget ? (
|
|
645
|
-
<div className="flex items-center gap-3 overflow-hidden rounded-xl border border-stone-200 bg-stone-50 p-3">
|
|
646
|
-
<img
|
|
647
|
-
src={deleteTarget.thumbnail_url || deleteTarget.url}
|
|
648
|
-
alt=""
|
|
649
|
-
className="size-16 shrink-0 rounded-lg object-cover"
|
|
650
|
-
onError={(e) => { if (e.currentTarget.src !== deleteTarget.url) e.currentTarget.src = deleteTarget.url; }}
|
|
651
|
-
/>
|
|
652
|
-
<div className="min-w-0 overflow-hidden text-xs text-stone-500">
|
|
653
|
-
<div className="truncate font-medium text-stone-700">{deleteTarget.name}</div>
|
|
654
|
-
<div className="truncate">{deleteTarget.created_at}</div>
|
|
655
|
-
<div>{formatSize(deleteTarget.size)}</div>
|
|
656
|
-
</div>
|
|
657
|
-
</div>
|
|
658
|
-
) : null}
|
|
659
|
-
<DialogFooter>
|
|
660
|
-
<Button variant="outline" onClick={closeDialog} className="rounded-xl">
|
|
661
|
-
取消
|
|
662
|
-
</Button>
|
|
663
|
-
<Button variant="destructive" onClick={() => void handleDelete()} disabled={isDeleting} className="rounded-xl">
|
|
664
|
-
{isDeleting ? <LoaderCircle className="mr-1 size-4 animate-spin" /> : <Trash2 className="mr-1 size-4" />}
|
|
665
|
-
删除
|
|
666
|
-
</Button>
|
|
667
|
-
</DialogFooter>
|
|
668
|
-
</DialogContent>
|
|
669
|
-
</Dialog>
|
|
670
|
-
|
|
671
|
-
<ImageLightbox
|
|
672
|
-
images={lightboxImages}
|
|
673
|
-
currentIndex={lightboxIndex}
|
|
674
|
-
open={lightboxOpen}
|
|
675
|
-
onOpenChange={setLightboxOpen}
|
|
676
|
-
onIndexChange={setLightboxIndex}
|
|
677
|
-
/>
|
|
678
|
-
<Dialog open={deleteMode === "selected" || deleteMode === "filtered"} onOpenChange={(open) => (!open ? setDeleteMode(null) : null)}>
|
|
679
|
-
<DialogContent showCloseButton={false} className="rounded-2xl p-6">
|
|
680
|
-
<DialogHeader className="gap-2">
|
|
681
|
-
<DialogTitle>{deleteMode === "filtered" ? "删除匹配日期的图片" : "删除所选图片"}</DialogTitle>
|
|
682
|
-
</DialogHeader>
|
|
683
|
-
<p className="text-sm text-stone-600">
|
|
684
|
-
确认删除 {selectedCount} 张图片吗?删除后无法恢复。
|
|
685
|
-
</p>
|
|
686
|
-
<DialogFooter>
|
|
687
|
-
<Button variant="outline" className="rounded-xl" onClick={() => setDeleteMode(null)} disabled={isDeleting}>
|
|
688
|
-
取消
|
|
689
|
-
</Button>
|
|
690
|
-
<Button className="rounded-xl bg-rose-600 text-white hover:bg-rose-700" onClick={() => void confirmDelete()} disabled={isDeleting || selectedCount === 0}>
|
|
691
|
-
{isDeleting ? <LoaderCircle className="size-4 animate-spin" /> : null}
|
|
692
|
-
确认删除
|
|
693
|
-
</Button>
|
|
694
|
-
</DialogFooter>
|
|
695
|
-
</DialogContent>
|
|
696
|
-
</Dialog>
|
|
697
|
-
<Dialog open={Boolean(tagDeleteTarget)} onOpenChange={(open) => { if (!open) setTagDeleteTarget(null); }}>
|
|
698
|
-
<DialogContent className="max-w-sm rounded-2xl">
|
|
699
|
-
<DialogHeader>
|
|
700
|
-
<DialogTitle>删除标签</DialogTitle>
|
|
701
|
-
</DialogHeader>
|
|
702
|
-
<p className="text-sm text-stone-600">
|
|
703
|
-
确定要删除标签 <span className="font-semibold">"{tagDeleteTarget}"</span> 吗?将从所有图片中移除该标签。
|
|
704
|
-
</p>
|
|
705
|
-
<DialogFooter>
|
|
706
|
-
<Button variant="outline" className="rounded-xl" onClick={() => setTagDeleteTarget(null)}>
|
|
707
|
-
取消
|
|
708
|
-
</Button>
|
|
709
|
-
<Button
|
|
710
|
-
variant="destructive"
|
|
711
|
-
className="rounded-xl"
|
|
712
|
-
onClick={() => {
|
|
713
|
-
if (tagDeleteTarget) void handleDeleteTag(tagDeleteTarget);
|
|
714
|
-
setTagDeleteTarget(null);
|
|
715
|
-
}}
|
|
716
|
-
>
|
|
717
|
-
确认删除
|
|
718
|
-
</Button>
|
|
719
|
-
</DialogFooter>
|
|
720
|
-
</DialogContent>
|
|
721
|
-
</Dialog>
|
|
722
|
-
</section>
|
|
723
|
-
);
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
export default function ImageManagerPage() {
|
|
727
|
-
const { isCheckingAuth, session } = useAuthGuard(["admin"]);
|
|
728
|
-
if (isCheckingAuth || !session || session.role !== "admin") {
|
|
729
|
-
return <div className="flex min-h-[40vh] items-center justify-center"><LoaderCircle className="size-5 animate-spin text-stone-400" /></div>;
|
|
730
|
-
}
|
|
731
|
-
return <ImageManagerContent />;
|
|
732
|
-
}
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { CalendarDays, ChevronLeft, ChevronRight, Copy, Download, ImageIcon, LoaderCircle, Maximize2, Plus, RefreshCw, Search, Tag, Trash2, X } from "lucide-react";
|
|
5
|
+
import { toast } from "sonner";
|
|
6
|
+
|
|
7
|
+
import { DateRangeFilter } from "@/components/date-range-filter";
|
|
8
|
+
import { ImageLightbox } from "@/components/image-lightbox";
|
|
9
|
+
import { Badge } from "@/components/ui/badge";
|
|
10
|
+
import { Button } from "@/components/ui/button";
|
|
11
|
+
import { Card, CardContent } from "@/components/ui/card";
|
|
12
|
+
import { Checkbox } from "@/components/ui/checkbox";
|
|
13
|
+
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
|
14
|
+
import { Input } from "@/components/ui/input";
|
|
15
|
+
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
|
16
|
+
import { compressAllImages, deleteImageTag, deleteManagedImages, deleteToTarget, downloadImages, downloadSingleImage, fetchImageStorage, fetchImageTags, fetchManagedImages, setImageTags, type ImageStorageStats, type ManagedImage } from "@/lib/api";
|
|
17
|
+
import { useAuthGuard } from "@/lib/use-auth-guard";
|
|
18
|
+
|
|
19
|
+
const LONG_PRESS_MS = 800;
|
|
20
|
+
const IMAGE_MANAGER_CHECKBOX_CLASS = "border-stone-300 bg-white/80 dark:border-white/35 dark:bg-white/5 data-[state=checked]:border-stone-950 dark:data-[state=checked]:border-white";
|
|
21
|
+
|
|
22
|
+
function formatSize(size: number) {
|
|
23
|
+
return size > 1024 * 1024 ? `${(size / 1024 / 1024).toFixed(2)} MB` : `${Math.ceil(size / 1024)} KB`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function imageKey(item: ManagedImage) {
|
|
27
|
+
return item.rel || item.url;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function useLongPress(onLongPress: () => void, ms = LONG_PRESS_MS) {
|
|
31
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
32
|
+
const activeRef = useRef(false);
|
|
33
|
+
|
|
34
|
+
const start = useCallback((e: React.MouseEvent | React.TouchEvent) => {
|
|
35
|
+
activeRef.current = true;
|
|
36
|
+
timerRef.current = setTimeout(() => {
|
|
37
|
+
if (activeRef.current) {
|
|
38
|
+
onLongPress();
|
|
39
|
+
}
|
|
40
|
+
}, ms);
|
|
41
|
+
}, [onLongPress, ms]);
|
|
42
|
+
|
|
43
|
+
const stop = useCallback(() => {
|
|
44
|
+
activeRef.current = false;
|
|
45
|
+
if (timerRef.current) {
|
|
46
|
+
clearTimeout(timerRef.current);
|
|
47
|
+
timerRef.current = null;
|
|
48
|
+
}
|
|
49
|
+
}, []);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
onMouseDown: start,
|
|
53
|
+
onMouseUp: stop,
|
|
54
|
+
onMouseLeave: stop,
|
|
55
|
+
onTouchStart: start,
|
|
56
|
+
onTouchEnd: stop,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function ImageManagerContent() {
|
|
61
|
+
const [items, setItems] = useState<ManagedImage[]>([]);
|
|
62
|
+
const [startDate, setStartDate] = useState("");
|
|
63
|
+
const [endDate, setEndDate] = useState("");
|
|
64
|
+
const [lightboxIndex, setLightboxIndex] = useState(0);
|
|
65
|
+
const [lightboxOpen, setLightboxOpen] = useState(false);
|
|
66
|
+
const [page, setPage] = useState(1);
|
|
67
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
68
|
+
const [deleteStartDate, setDeleteStartDate] = useState("");
|
|
69
|
+
const [deleteTarget, setDeleteTarget] = useState<ManagedImage | null>(null);
|
|
70
|
+
const [isDeleting, setIsDeleting] = useState(false);
|
|
71
|
+
const [allTags, setAllTags] = useState<string[]>([]);
|
|
72
|
+
const [storage, setStorage] = useState<ImageStorageStats | null>(null);
|
|
73
|
+
const [storageLoading, setStorageLoading] = useState(false);
|
|
74
|
+
const [compressResult, setCompressResult] = useState<string>("");
|
|
75
|
+
const [targetFreeMb, setTargetFreeMb] = useState(500);
|
|
76
|
+
|
|
77
|
+
const loadStorage = useCallback(async () => {
|
|
78
|
+
try {
|
|
79
|
+
setStorageLoading(true);
|
|
80
|
+
const data = await fetchImageStorage();
|
|
81
|
+
setStorage(data);
|
|
82
|
+
} catch { /* ignore */ }
|
|
83
|
+
finally { setStorageLoading(false); }
|
|
84
|
+
}, []);
|
|
85
|
+
|
|
86
|
+
useEffect(() => { void loadStorage(); }, [loadStorage]);
|
|
87
|
+
const [selectedTags, setSelectedTags] = useState<string[]>([]);
|
|
88
|
+
const [tagEditTarget, setTagEditTarget] = useState<ManagedImage | null>(null);
|
|
89
|
+
const [tagInput, setTagInput] = useState("");
|
|
90
|
+
const [dialogVisible, setDialogVisible] = useState(false);
|
|
91
|
+
const deleteTargetRef = useRef<ManagedImage | null>(null);
|
|
92
|
+
const [selectedPaths, setSelectedPaths] = useState<string[]>([]);
|
|
93
|
+
const [deleteMode, setDeleteMode] = useState<"selected" | "filtered" | "byDate" | null>(null);
|
|
94
|
+
const [isDownloading, setIsDownloading] = useState(false);
|
|
95
|
+
|
|
96
|
+
const filteredItems = selectedTags.length > 0
|
|
97
|
+
? items.filter((item) => selectedTags.every((t) => (item.tags ?? []).includes(t)))
|
|
98
|
+
: items;
|
|
99
|
+
|
|
100
|
+
const lightboxImages = filteredItems.map((item) => ({
|
|
101
|
+
id: item.name,
|
|
102
|
+
src: item.url,
|
|
103
|
+
sizeLabel: formatSize(item.size),
|
|
104
|
+
dimensions: item.width && item.height ? `${item.width} x ${item.height}` : undefined,
|
|
105
|
+
}));
|
|
106
|
+
const pageSize = 12;
|
|
107
|
+
const pageCount = Math.max(1, Math.ceil(filteredItems.length / pageSize));
|
|
108
|
+
const safePage = Math.min(page, pageCount);
|
|
109
|
+
const currentRows = filteredItems.slice((safePage - 1) * pageSize, safePage * pageSize);
|
|
110
|
+
const selectedSet = useMemo(() => new Set(selectedPaths), [selectedPaths]);
|
|
111
|
+
const selectedCount = deleteMode === "filtered" ? items.length : deleteMode === "byDate" ? 0 : selectedPaths.length;
|
|
112
|
+
const currentPageSelected = currentRows.length > 0 && currentRows.every((item) => selectedSet.has(imageKey(item)));
|
|
113
|
+
const allSelected = filteredItems.length > 0 && filteredItems.every((item) => selectedSet.has(imageKey(item)));
|
|
114
|
+
|
|
115
|
+
const loadImages = async () => {
|
|
116
|
+
setIsLoading(true);
|
|
117
|
+
try {
|
|
118
|
+
const [data, tagsData] = await Promise.all([
|
|
119
|
+
fetchManagedImages({ start_date: startDate, end_date: endDate }),
|
|
120
|
+
fetchImageTags(),
|
|
121
|
+
]);
|
|
122
|
+
setItems(data.items);
|
|
123
|
+
setAllTags(tagsData.tags);
|
|
124
|
+
setSelectedPaths((current) => current.filter((path) => data.items.some((item) => imageKey(item) === path)));
|
|
125
|
+
setPage(1);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
toast.error(error instanceof Error ? error.message : "加载图片失败");
|
|
128
|
+
} finally {
|
|
129
|
+
setIsLoading(false);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const closeDialog = useCallback(() => {
|
|
134
|
+
setDialogVisible(false);
|
|
135
|
+
setTimeout(() => setDeleteTarget(null), 200);
|
|
136
|
+
}, []);
|
|
137
|
+
|
|
138
|
+
const openDeleteDialog = useCallback((item: ManagedImage) => {
|
|
139
|
+
deleteTargetRef.current = item;
|
|
140
|
+
setDeleteTarget(item);
|
|
141
|
+
setDialogVisible(true);
|
|
142
|
+
}, []);
|
|
143
|
+
|
|
144
|
+
const handleDelete = async () => {
|
|
145
|
+
if (!deleteTarget) return;
|
|
146
|
+
setIsDeleting(true);
|
|
147
|
+
try {
|
|
148
|
+
await deleteManagedImages({ paths: [deleteTarget.rel] });
|
|
149
|
+
setItems((prev) => prev.filter((item) => item.rel !== deleteTarget.rel));
|
|
150
|
+
setSelectedPaths((prev) => prev.filter((p) => p !== imageKey(deleteTarget)));
|
|
151
|
+
toast.success("图片已删除");
|
|
152
|
+
} catch (error) {
|
|
153
|
+
toast.error(error instanceof Error ? error.message : "删除失败");
|
|
154
|
+
} finally {
|
|
155
|
+
setIsDeleting(false);
|
|
156
|
+
closeDialog();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const handleSetTags = async (item: ManagedImage, tags: string[]) => {
|
|
161
|
+
try {
|
|
162
|
+
const result = await setImageTags(item.rel, tags);
|
|
163
|
+
setItems((prev) => prev.map((i) => i.rel === item.rel ? { ...i, tags: result.tags } : i));
|
|
164
|
+
const tagsData = await fetchImageTags();
|
|
165
|
+
setAllTags(tagsData.tags);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
toast.error(error instanceof Error ? error.message : "设置标签失败");
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const handleAddTag = (item: ManagedImage) => {
|
|
172
|
+
const tag = tagInput.trim();
|
|
173
|
+
if (!tag) return;
|
|
174
|
+
const current = item.tags ?? [];
|
|
175
|
+
if (current.includes(tag)) {
|
|
176
|
+
toast.error("标签已存在");
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
void handleSetTags(item, [...current, tag]);
|
|
180
|
+
setTagInput("");
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const handleRemoveTag = (item: ManagedImage, tag: string) => {
|
|
184
|
+
void handleSetTags(item, (item.tags ?? []).filter((t) => t !== tag));
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const toggleFilterTag = (tag: string) => {
|
|
188
|
+
setSelectedTags((prev) => prev.includes(tag) ? prev.filter((t) => t !== tag) : [...prev, tag]);
|
|
189
|
+
setPage(1);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const [pressingTag, setPressingTag] = useState<string | null>(null);
|
|
193
|
+
const pressTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
194
|
+
const [tagDeleteTarget, setTagDeleteTarget] = useState<string | null>(null);
|
|
195
|
+
|
|
196
|
+
const handleDeleteTag = async (tag: string) => {
|
|
197
|
+
try {
|
|
198
|
+
const result = await deleteImageTag(tag);
|
|
199
|
+
setAllTags((prev) => prev.filter((t) => t !== tag));
|
|
200
|
+
setSelectedTags((prev) => prev.filter((t) => t !== tag));
|
|
201
|
+
setItems((prev) => prev.map((item) => ({
|
|
202
|
+
...item,
|
|
203
|
+
tags: (item.tags ?? []).filter((t) => t !== tag),
|
|
204
|
+
})));
|
|
205
|
+
toast.success(`标签"${tag}"已删除,影响 ${result.removed_from} 张图片`);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
toast.error(error instanceof Error ? error.message : "删除标签失败");
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const startTagPress = useCallback((tag: string) => {
|
|
212
|
+
setPressingTag(tag);
|
|
213
|
+
pressTimerRef.current = setTimeout(() => {
|
|
214
|
+
setPressingTag(null);
|
|
215
|
+
setTagDeleteTarget(tag);
|
|
216
|
+
}, LONG_PRESS_MS);
|
|
217
|
+
}, []);
|
|
218
|
+
|
|
219
|
+
const stopTagPress = useCallback(() => {
|
|
220
|
+
setPressingTag(null);
|
|
221
|
+
if (pressTimerRef.current) {
|
|
222
|
+
clearTimeout(pressTimerRef.current);
|
|
223
|
+
pressTimerRef.current = null;
|
|
224
|
+
}
|
|
225
|
+
}, []);
|
|
226
|
+
|
|
227
|
+
const clearFilters = () => {
|
|
228
|
+
setStartDate("");
|
|
229
|
+
setEndDate("");
|
|
230
|
+
setSelectedTags([]);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const togglePaths = (paths: string[], checked: boolean) => {
|
|
234
|
+
setSelectedPaths((current) => checked ? Array.from(new Set([...current, ...paths])) : current.filter((path) => !paths.includes(path)));
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const confirmDelete = async () => {
|
|
238
|
+
if (!deleteMode || selectedCount === 0) return;
|
|
239
|
+
setIsDeleting(true);
|
|
240
|
+
try {
|
|
241
|
+
const data = await deleteManagedImages(deleteMode === "filtered" ? { start_date: startDate, end_date: endDate, all_matching: true } : { paths: selectedPaths });
|
|
242
|
+
toast.success(`已删除 ${data.removed} 张图片`);
|
|
243
|
+
setDeleteMode(null);
|
|
244
|
+
setSelectedPaths([]);
|
|
245
|
+
await loadImages();
|
|
246
|
+
} catch (error) {
|
|
247
|
+
toast.error(error instanceof Error ? error.message : "删除图片失败");
|
|
248
|
+
} finally {
|
|
249
|
+
setIsDeleting(false);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const handleBatchDownload = async () => {
|
|
254
|
+
const paths = deleteMode === "filtered" ? items.map((item) => item.rel) : selectedPaths;
|
|
255
|
+
if (paths.length === 0) return;
|
|
256
|
+
setIsDownloading(true);
|
|
257
|
+
try {
|
|
258
|
+
await downloadImages(paths);
|
|
259
|
+
toast.success(`已下载 ${paths.length} 张图片`);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
toast.error(error instanceof Error ? error.message : "下载失败");
|
|
262
|
+
} finally {
|
|
263
|
+
setIsDownloading(false);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const handleSingleDownload = async (item: ManagedImage) => {
|
|
268
|
+
await downloadSingleImage(item.rel);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
useEffect(() => {
|
|
272
|
+
void loadImages();
|
|
273
|
+
}, [startDate, endDate]);
|
|
274
|
+
|
|
275
|
+
return (
|
|
276
|
+
<section className="space-y-5">
|
|
277
|
+
<div className="flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between">
|
|
278
|
+
<div className="space-y-1">
|
|
279
|
+
<div className="text-xs font-semibold tracking-[0.18em] text-stone-500 uppercase">Images</div>
|
|
280
|
+
<h1 className="text-2xl font-semibold tracking-tight">图片管理</h1>
|
|
281
|
+
</div>
|
|
282
|
+
<div className="flex flex-wrap gap-2">
|
|
283
|
+
<DateRangeFilter startDate={startDate} endDate={endDate} onChange={(start, end) => { setStartDate(start); setEndDate(end); }} />
|
|
284
|
+
<Button variant="outline" onClick={clearFilters} className="h-10 rounded-xl border-stone-200 bg-white px-4 text-stone-700">
|
|
285
|
+
清除筛选条件
|
|
286
|
+
</Button>
|
|
287
|
+
<Button onClick={() => void loadImages()} disabled={isLoading} className="h-10 rounded-xl bg-stone-950 px-4 text-white hover:bg-stone-800">
|
|
288
|
+
{isLoading ? <LoaderCircle className="size-4 animate-spin" /> : <Search className="size-4" />}
|
|
289
|
+
查询
|
|
290
|
+
</Button>
|
|
291
|
+
<Button variant="outline" onClick={() => setDeleteMode("filtered")} disabled={isDeleting || items.length === 0 || (!startDate && !endDate)} className="h-10 rounded-xl border-rose-200 bg-white px-4 text-rose-600 hover:bg-rose-50">
|
|
292
|
+
<Trash2 className="size-4" />
|
|
293
|
+
删除匹配日期
|
|
294
|
+
</Button>
|
|
295
|
+
</div>
|
|
296
|
+
</div>
|
|
297
|
+
|
|
298
|
+
{allTags.length > 0 ? (
|
|
299
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
300
|
+
<span className="text-xs font-medium text-stone-500">
|
|
301
|
+
<Tag className="mr-1 inline size-3.5" />
|
|
302
|
+
标签筛选:
|
|
303
|
+
</span>
|
|
304
|
+
{allTags.map((tag) => {
|
|
305
|
+
const isPressing = pressingTag === tag;
|
|
306
|
+
return (
|
|
307
|
+
<span
|
|
308
|
+
key={tag}
|
|
309
|
+
className="relative inline-flex items-center"
|
|
310
|
+
onMouseDown={() => startTagPress(tag)}
|
|
311
|
+
onMouseUp={stopTagPress}
|
|
312
|
+
onMouseLeave={stopTagPress}
|
|
313
|
+
onTouchStart={() => startTagPress(tag)}
|
|
314
|
+
onTouchEnd={stopTagPress}
|
|
315
|
+
>
|
|
316
|
+
<button
|
|
317
|
+
type="button"
|
|
318
|
+
onClick={() => toggleFilterTag(tag)}
|
|
319
|
+
>
|
|
320
|
+
<Badge
|
|
321
|
+
variant={selectedTags.includes(tag) ? "default" : "outline"}
|
|
322
|
+
className={`cursor-pointer rounded-md transition-all hover:opacity-80 ${isPressing ? "ring-2 ring-red-400 ring-offset-1" : ""}`}
|
|
323
|
+
>
|
|
324
|
+
{tag}
|
|
325
|
+
</Badge>
|
|
326
|
+
</button>
|
|
327
|
+
{isPressing ? (
|
|
328
|
+
<span className="pointer-events-none absolute inset-0 overflow-hidden rounded-md">
|
|
329
|
+
<span className="absolute inset-0 animate-[grow_800ms_linear_forwards] rounded-md bg-red-400/20" />
|
|
330
|
+
</span>
|
|
331
|
+
) : null}
|
|
332
|
+
</span>
|
|
333
|
+
);
|
|
334
|
+
})}
|
|
335
|
+
{selectedTags.length > 0 ? (
|
|
336
|
+
<button type="button" onClick={() => setSelectedTags([])}>
|
|
337
|
+
<Badge variant="secondary" className="cursor-pointer rounded-md">
|
|
338
|
+
<X className="mr-0.5 size-3" />
|
|
339
|
+
清除
|
|
340
|
+
</Badge>
|
|
341
|
+
</button>
|
|
342
|
+
) : null}
|
|
343
|
+
</div>
|
|
344
|
+
) : null}
|
|
345
|
+
|
|
346
|
+
{/* Storage Stats Panel */}
|
|
347
|
+
<div className="grid grid-cols-2 sm:grid-cols-4 lg:grid-cols-6 gap-3 mb-4">
|
|
348
|
+
{storage ? (
|
|
349
|
+
<>
|
|
350
|
+
<div className="rounded-xl border border-stone-200 bg-white/80 p-3">
|
|
351
|
+
<div className="text-xs text-stone-500">磁盘总量</div>
|
|
352
|
+
<div className="text-lg font-bold text-stone-800">{storage.disk_total_mb >= 1024 ? `${(storage.disk_total_mb / 1024).toFixed(1)} GB` : `${storage.disk_total_mb} MB`}</div>
|
|
353
|
+
</div>
|
|
354
|
+
<div className="rounded-xl border border-stone-200 bg-white/80 p-3">
|
|
355
|
+
<div className="text-xs text-stone-500">剩余空间</div>
|
|
356
|
+
<div className={`text-lg font-bold ${storage.disk_free_mb < 200 ? "text-red-500" : storage.disk_free_mb < 500 ? "text-yellow-500" : "text-green-600"}`}>{storage.disk_free_mb >= 1024 ? `${(storage.disk_free_mb / 1024).toFixed(1)} GB` : `${storage.disk_free_mb} MB`}</div>
|
|
357
|
+
</div>
|
|
358
|
+
<div className="rounded-xl border border-stone-200 bg-white/80 p-3">
|
|
359
|
+
<div className="text-xs text-stone-500">图片数量</div>
|
|
360
|
+
<div className="text-lg font-bold text-stone-800">{storage.image_count}</div>
|
|
361
|
+
</div>
|
|
362
|
+
<div className="rounded-xl border border-stone-200 bg-white/80 p-3">
|
|
363
|
+
<div className="text-xs text-stone-500">图片占用</div>
|
|
364
|
+
<div className="text-lg font-bold text-stone-800">{storage.image_size_mb >= 1024 ? `${(storage.image_size_mb / 1024).toFixed(1)} GB` : `${storage.image_size_mb} MB`}</div>
|
|
365
|
+
</div>
|
|
366
|
+
<div className="rounded-xl border border-stone-200 bg-white/80 p-3 col-span-2 flex items-center gap-2 flex-wrap">
|
|
367
|
+
<span className="text-xs text-stone-500 w-full">快捷操作</span>
|
|
368
|
+
<Button size="sm" variant="outline" className="h-7 text-xs" disabled={storageLoading} onClick={() => { void loadStorage(); }}>
|
|
369
|
+
<RefreshCw className={`size-3 mr-1 ${storageLoading ? "animate-spin" : ""}`} />刷新
|
|
370
|
+
</Button>
|
|
371
|
+
<Button size="sm" variant="outline" className="h-7 text-xs"
|
|
372
|
+
onClick={async () => {
|
|
373
|
+
try { const r = await compressAllImages(); setCompressResult(`已压缩${r.saved_mb}MB`); void loadStorage(); }
|
|
374
|
+
catch { setCompressResult("压缩失败"); }
|
|
375
|
+
}}>
|
|
376
|
+
🗜️ 压缩优化
|
|
377
|
+
</Button>
|
|
378
|
+
<Button size="sm" variant="outline" className="h-7 text-xs border-rose-200 text-rose-600"
|
|
379
|
+
onClick={() => setDeleteMode("byDate")}>
|
|
380
|
+
🗑️ 按日期删除
|
|
381
|
+
</Button>
|
|
382
|
+
<form onSubmit={async (e) => { e.preventDefault();
|
|
383
|
+
try {
|
|
384
|
+
const r = await deleteToTarget(targetFreeMb);
|
|
385
|
+
toast.success(`已删除 ${r.removed} 张图片,释放 ${r.freed_mb ?? 0}MB`);
|
|
386
|
+
void loadStorage();
|
|
387
|
+
} catch { toast.error("清理失败"); }
|
|
388
|
+
}} className="flex items-center gap-1">
|
|
389
|
+
<Button size="sm" variant="outline" className="h-7 text-xs border-amber-200 text-amber-700" type="submit">
|
|
390
|
+
🧹 清理至
|
|
391
|
+
</Button>
|
|
392
|
+
<Input className="h-7 w-14 text-xs text-center px-1" type="number" min={50} value={targetFreeMb}
|
|
393
|
+
onChange={(e) => setTargetFreeMb(Number(e.target.value) || 500)} />
|
|
394
|
+
<span className="text-xs text-stone-400">MB 剩余</span>
|
|
395
|
+
</form>
|
|
396
|
+
{compressResult ? <span className="text-xs text-green-600 ml-1">{compressResult}</span> : null}
|
|
397
|
+
</div>
|
|
398
|
+
</>
|
|
399
|
+
) : (
|
|
400
|
+
<div className="rounded-xl border border-stone-200 bg-white/80 p-3 col-span-full text-center text-sm text-stone-400">
|
|
401
|
+
{storageLoading ? "加载存储信息..." : "存储信息加载失败"}
|
|
402
|
+
</div>
|
|
403
|
+
)}
|
|
404
|
+
</div>
|
|
405
|
+
|
|
406
|
+
{/* Delete by date dialog */}
|
|
407
|
+
<Dialog open={deleteMode === "byDate"} onOpenChange={() => setDeleteMode(null)}>
|
|
408
|
+
<DialogContent className="sm:max-w-md rounded-2xl">
|
|
409
|
+
<DialogHeader><DialogTitle>按日期删除图片</DialogTitle></DialogHeader>
|
|
410
|
+
<div className="space-y-4">
|
|
411
|
+
<div className="flex items-center gap-2">
|
|
412
|
+
<label className="text-sm text-stone-600 shrink-0">删除</label>
|
|
413
|
+
<Input className="h-9 text-sm" type="date" value={deleteStartDate} onChange={(e) => setDeleteStartDate(e.target.value)} />
|
|
414
|
+
<span className="text-sm text-stone-400">之前的图片</span>
|
|
415
|
+
</div>
|
|
416
|
+
<p className="text-xs text-stone-500">此操作不可撤销,将永久删除所有匹配日期的图片及其缩略图。</p>
|
|
417
|
+
</div>
|
|
418
|
+
<DialogFooter>
|
|
419
|
+
<Button variant="ghost" onClick={() => setDeleteMode(null)}>取消</Button>
|
|
420
|
+
<Button variant="destructive" disabled={!deleteStartDate || isDeleting}
|
|
421
|
+
onClick={async () => {
|
|
422
|
+
if (!deleteStartDate) return;
|
|
423
|
+
try {
|
|
424
|
+
setIsDeleting(true);
|
|
425
|
+
const r = await deleteManagedImages({ end_date: deleteStartDate, all_matching: true });
|
|
426
|
+
toast.success(`已删除 ${r.removed} 张图片`);
|
|
427
|
+
setDeleteMode(null);
|
|
428
|
+
void loadStorage();
|
|
429
|
+
void loadImages();
|
|
430
|
+
} catch { toast.error("删除失败"); }
|
|
431
|
+
finally { setIsDeleting(false); }
|
|
432
|
+
}}>
|
|
433
|
+
{isDeleting ? <LoaderCircle className="size-4 animate-spin" /> : null}
|
|
434
|
+
确认删除
|
|
435
|
+
</Button>
|
|
436
|
+
</DialogFooter>
|
|
437
|
+
</DialogContent>
|
|
438
|
+
</Dialog>
|
|
439
|
+
|
|
440
|
+
<Card className="rounded-2xl border-white/80 bg-white/90 shadow-sm">
|
|
441
|
+
<CardContent className="p-0">
|
|
442
|
+
<div className="flex flex-wrap items-center justify-between gap-3 border-b border-stone-100 px-5 py-4">
|
|
443
|
+
<div className="flex flex-wrap items-center gap-3 text-sm text-stone-600">
|
|
444
|
+
<ImageIcon className="size-4" />
|
|
445
|
+
共 {filteredItems.length} 张
|
|
446
|
+
{selectedTags.length > 0 ? <span className="text-stone-400">(筛选自 {items.length} 张)</span> : null}
|
|
447
|
+
<label className="flex items-center gap-2">
|
|
448
|
+
<Checkbox className={IMAGE_MANAGER_CHECKBOX_CLASS} checked={currentPageSelected} onCheckedChange={(checked) => togglePaths(currentRows.map(imageKey), Boolean(checked))} />
|
|
449
|
+
本页全选
|
|
450
|
+
</label>
|
|
451
|
+
<label className="flex items-center gap-2">
|
|
452
|
+
<Checkbox className={IMAGE_MANAGER_CHECKBOX_CLASS} checked={allSelected} onCheckedChange={(checked) => togglePaths(filteredItems.map(imageKey), Boolean(checked))} />
|
|
453
|
+
全选结果
|
|
454
|
+
</label>
|
|
455
|
+
{selectedPaths.length > 0 ? <span>已选 {selectedPaths.length} 张</span> : null}
|
|
456
|
+
</div>
|
|
457
|
+
<div className="flex items-center gap-2">
|
|
458
|
+
<Button variant="ghost" className="h-8 rounded-lg px-3 text-stone-500" onClick={() => void loadImages()} disabled={isLoading}>
|
|
459
|
+
<RefreshCw className={`size-4 ${isLoading ? "animate-spin" : ""}`} />
|
|
460
|
+
刷新
|
|
461
|
+
</Button>
|
|
462
|
+
<button type="button" className="text-sm text-stone-500 hover:text-stone-900 disabled:text-stone-300" onClick={() => setSelectedPaths([])} disabled={selectedPaths.length === 0 || isDeleting}>
|
|
463
|
+
取消选择
|
|
464
|
+
</button>
|
|
465
|
+
<Button variant="outline" className="h-8 rounded-lg border-stone-200 bg-white px-3 text-stone-600 hover:bg-stone-50" onClick={() => void handleBatchDownload()} disabled={selectedPaths.length === 0 || isDownloading || isDeleting}>
|
|
466
|
+
{isDownloading ? <LoaderCircle className="size-4 animate-spin" /> : <Download className="size-4" />}
|
|
467
|
+
下载所选
|
|
468
|
+
</Button>
|
|
469
|
+
<Button variant="outline" className="h-8 rounded-lg border-rose-200 bg-white px-3 text-rose-600 hover:bg-rose-50" onClick={() => setDeleteMode("selected")} disabled={selectedPaths.length === 0 || isDeleting}>
|
|
470
|
+
<Trash2 className="size-4" />
|
|
471
|
+
删除所选
|
|
472
|
+
</Button>
|
|
473
|
+
</div>
|
|
474
|
+
</div>
|
|
475
|
+
<div className="grid gap-0 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
|
|
476
|
+
{currentRows.map((item) => {
|
|
477
|
+
const imageIndex = filteredItems.findIndex((row) => row.url === item.url);
|
|
478
|
+
return (
|
|
479
|
+
<div key={item.rel} className="group border-r border-b border-stone-100 p-4 transition hover:bg-stone-50 dark:hover:bg-white/5">
|
|
480
|
+
<div className="relative">
|
|
481
|
+
<button
|
|
482
|
+
type="button"
|
|
483
|
+
className="relative block aspect-square w-full cursor-zoom-in overflow-hidden rounded-lg bg-stone-100 text-left"
|
|
484
|
+
onClick={() => {
|
|
485
|
+
setLightboxIndex(imageIndex);
|
|
486
|
+
setLightboxOpen(true);
|
|
487
|
+
}}
|
|
488
|
+
>
|
|
489
|
+
<img
|
|
490
|
+
src={item.thumbnail_url || item.url}
|
|
491
|
+
alt={item.name}
|
|
492
|
+
className="h-full w-full object-cover transition group-hover:scale-[1.02]"
|
|
493
|
+
onError={(event) => {
|
|
494
|
+
if (event.currentTarget.src !== item.url) {
|
|
495
|
+
event.currentTarget.src = item.url;
|
|
496
|
+
}
|
|
497
|
+
}}
|
|
498
|
+
/>
|
|
499
|
+
<span className="absolute right-2 bottom-2 rounded-full bg-black/50 p-2 text-white opacity-100 transition sm:opacity-0 sm:group-hover:opacity-100">
|
|
500
|
+
<Maximize2 className="size-4" />
|
|
501
|
+
</span>
|
|
502
|
+
</button>
|
|
503
|
+
<button
|
|
504
|
+
type="button"
|
|
505
|
+
className="absolute top-2 right-2 z-10 inline-flex size-7 items-center justify-center rounded-full bg-black/50 text-white opacity-100 transition hover:bg-red-600 sm:opacity-0 sm:group-hover:opacity-100"
|
|
506
|
+
title="删除图片"
|
|
507
|
+
onClick={(e) => {
|
|
508
|
+
e.stopPropagation();
|
|
509
|
+
openDeleteDialog(item);
|
|
510
|
+
}}
|
|
511
|
+
>
|
|
512
|
+
<Trash2 className="size-3.5" />
|
|
513
|
+
</button>
|
|
514
|
+
</div>
|
|
515
|
+
<div className="mt-3 space-y-2 text-xs text-stone-500">
|
|
516
|
+
<div className="flex items-center justify-between gap-2">
|
|
517
|
+
<div className="flex items-center gap-1 font-medium text-stone-700">
|
|
518
|
+
<CalendarDays className="size-3.5" />
|
|
519
|
+
{item.created_at}
|
|
520
|
+
</div>
|
|
521
|
+
<div className="flex items-center gap-1">
|
|
522
|
+
<Button
|
|
523
|
+
variant="ghost"
|
|
524
|
+
size="icon"
|
|
525
|
+
className="size-8 rounded-lg text-stone-400 hover:bg-stone-100 hover:text-stone-700"
|
|
526
|
+
onClick={() => void handleSingleDownload(item)}
|
|
527
|
+
title="下载图片"
|
|
528
|
+
>
|
|
529
|
+
<Download className="size-4" />
|
|
530
|
+
</Button>
|
|
531
|
+
<Button
|
|
532
|
+
variant="ghost"
|
|
533
|
+
size="icon"
|
|
534
|
+
className="size-8 rounded-lg text-stone-400 hover:bg-stone-100 hover:text-stone-700"
|
|
535
|
+
onClick={() => {
|
|
536
|
+
void navigator.clipboard.writeText(item.url);
|
|
537
|
+
toast.success("图片地址已复制");
|
|
538
|
+
}}
|
|
539
|
+
>
|
|
540
|
+
<Copy className="size-4" />
|
|
541
|
+
</Button>
|
|
542
|
+
<Checkbox className={IMAGE_MANAGER_CHECKBOX_CLASS} checked={selectedSet.has(imageKey(item))} onCheckedChange={(checked) => togglePaths([imageKey(item)], Boolean(checked))} />
|
|
543
|
+
</div>
|
|
544
|
+
</div>
|
|
545
|
+
<div className="flex items-center justify-between gap-2">
|
|
546
|
+
<span>{formatSize(item.size)}</span>
|
|
547
|
+
<span>{item.width && item.height ? `${item.width} x ${item.height}` : "-"}</span>
|
|
548
|
+
</div>
|
|
549
|
+
<div className="flex flex-wrap items-center gap-1">
|
|
550
|
+
{(item.tags ?? []).map((tag) => (
|
|
551
|
+
<Badge key={tag} variant="secondary" className="gap-0.5 rounded-md py-0 pr-0.5 text-[10px]">
|
|
552
|
+
{tag}
|
|
553
|
+
<button
|
|
554
|
+
type="button"
|
|
555
|
+
className="inline-flex size-3.5 items-center justify-center rounded-full hover:bg-stone-300"
|
|
556
|
+
onClick={() => handleRemoveTag(item, tag)}
|
|
557
|
+
>
|
|
558
|
+
<X className="size-2.5" />
|
|
559
|
+
</button>
|
|
560
|
+
</Badge>
|
|
561
|
+
))}
|
|
562
|
+
<Popover open={tagEditTarget?.rel === item.rel} onOpenChange={(open) => { setTagEditTarget(open ? item : null); setTagInput(""); }}>
|
|
563
|
+
<PopoverTrigger asChild>
|
|
564
|
+
<button
|
|
565
|
+
type="button"
|
|
566
|
+
className="inline-flex size-5 items-center justify-center rounded-full border border-dashed border-stone-300 text-stone-400 hover:border-stone-500 hover:text-stone-600"
|
|
567
|
+
title="添加标签"
|
|
568
|
+
>
|
|
569
|
+
<Plus className="size-3" />
|
|
570
|
+
</button>
|
|
571
|
+
</PopoverTrigger>
|
|
572
|
+
<PopoverContent align="start" className="w-56 p-2">
|
|
573
|
+
<div className="space-y-2">
|
|
574
|
+
<div className="text-xs font-medium text-stone-500">添加标签</div>
|
|
575
|
+
<div className="flex gap-1">
|
|
576
|
+
<Input
|
|
577
|
+
value={tagInput}
|
|
578
|
+
onChange={(e) => setTagInput(e.target.value)}
|
|
579
|
+
placeholder="输入标签名"
|
|
580
|
+
className="h-8 text-xs"
|
|
581
|
+
onKeyDown={(e) => {
|
|
582
|
+
if (e.key === "Enter") {
|
|
583
|
+
e.preventDefault();
|
|
584
|
+
handleAddTag(item);
|
|
585
|
+
}
|
|
586
|
+
}}
|
|
587
|
+
/>
|
|
588
|
+
<Button
|
|
589
|
+
size="icon"
|
|
590
|
+
variant="outline"
|
|
591
|
+
className="size-8 shrink-0"
|
|
592
|
+
onClick={() => handleAddTag(item)}
|
|
593
|
+
>
|
|
594
|
+
<Plus className="size-3.5" />
|
|
595
|
+
</Button>
|
|
596
|
+
</div>
|
|
597
|
+
{allTags.filter((t) => !(item.tags ?? []).includes(t)).length > 0 ? (
|
|
598
|
+
<div className="flex flex-wrap gap-1 border-t border-stone-100 pt-2">
|
|
599
|
+
{allTags.filter((t) => !(item.tags ?? []).includes(t)).map((tag) => (
|
|
600
|
+
<button
|
|
601
|
+
key={tag}
|
|
602
|
+
type="button"
|
|
603
|
+
onClick={() => {
|
|
604
|
+
void handleSetTags(item, [...(item.tags ?? []), tag]);
|
|
605
|
+
setTagEditTarget(null);
|
|
606
|
+
}}
|
|
607
|
+
>
|
|
608
|
+
<Badge variant="outline" className="cursor-pointer rounded-md text-[10px] hover:bg-stone-100">
|
|
609
|
+
{tag}
|
|
610
|
+
</Badge>
|
|
611
|
+
</button>
|
|
612
|
+
))}
|
|
613
|
+
</div>
|
|
614
|
+
) : null}
|
|
615
|
+
</div>
|
|
616
|
+
</PopoverContent>
|
|
617
|
+
</Popover>
|
|
618
|
+
</div>
|
|
619
|
+
</div>
|
|
620
|
+
</div>
|
|
621
|
+
)})}
|
|
622
|
+
</div>
|
|
623
|
+
<div className="flex items-center justify-end gap-2 border-t border-stone-100 px-4 py-3 text-sm text-stone-500">
|
|
624
|
+
<span>第 {safePage} / {pageCount} 页,共 {filteredItems.length} 张</span>
|
|
625
|
+
<Button variant="outline" size="icon" className="size-9 rounded-lg border-stone-200 bg-white" disabled={safePage <= 1} onClick={() => setPage((value) => Math.max(1, value - 1))}>
|
|
626
|
+
<ChevronLeft className="size-4" />
|
|
627
|
+
</Button>
|
|
628
|
+
<Button variant="outline" size="icon" className="size-9 rounded-lg border-stone-200 bg-white" disabled={safePage >= pageCount} onClick={() => setPage((value) => Math.min(pageCount, value + 1))}>
|
|
629
|
+
<ChevronRight className="size-4" />
|
|
630
|
+
</Button>
|
|
631
|
+
</div>
|
|
632
|
+
{!isLoading && filteredItems.length === 0 ? <div className="px-6 py-14 text-center text-sm text-stone-500">没有找到图片</div> : null}
|
|
633
|
+
</CardContent>
|
|
634
|
+
</Card>
|
|
635
|
+
|
|
636
|
+
<Dialog open={dialogVisible} onOpenChange={(open) => { if (!open) closeDialog(); }}>
|
|
637
|
+
<DialogContent className="max-w-sm overflow-hidden rounded-2xl">
|
|
638
|
+
<DialogHeader>
|
|
639
|
+
<DialogTitle className="pr-8">确认删除</DialogTitle>
|
|
640
|
+
</DialogHeader>
|
|
641
|
+
<p className="text-sm text-stone-600">
|
|
642
|
+
确定要删除这张图片吗?此操作不可恢复。
|
|
643
|
+
</p>
|
|
644
|
+
{deleteTarget ? (
|
|
645
|
+
<div className="flex items-center gap-3 overflow-hidden rounded-xl border border-stone-200 bg-stone-50 p-3">
|
|
646
|
+
<img
|
|
647
|
+
src={deleteTarget.thumbnail_url || deleteTarget.url}
|
|
648
|
+
alt=""
|
|
649
|
+
className="size-16 shrink-0 rounded-lg object-cover"
|
|
650
|
+
onError={(e) => { if (e.currentTarget.src !== deleteTarget.url) e.currentTarget.src = deleteTarget.url; }}
|
|
651
|
+
/>
|
|
652
|
+
<div className="min-w-0 overflow-hidden text-xs text-stone-500">
|
|
653
|
+
<div className="truncate font-medium text-stone-700">{deleteTarget.name}</div>
|
|
654
|
+
<div className="truncate">{deleteTarget.created_at}</div>
|
|
655
|
+
<div>{formatSize(deleteTarget.size)}</div>
|
|
656
|
+
</div>
|
|
657
|
+
</div>
|
|
658
|
+
) : null}
|
|
659
|
+
<DialogFooter>
|
|
660
|
+
<Button variant="outline" onClick={closeDialog} className="rounded-xl">
|
|
661
|
+
取消
|
|
662
|
+
</Button>
|
|
663
|
+
<Button variant="destructive" onClick={() => void handleDelete()} disabled={isDeleting} className="rounded-xl">
|
|
664
|
+
{isDeleting ? <LoaderCircle className="mr-1 size-4 animate-spin" /> : <Trash2 className="mr-1 size-4" />}
|
|
665
|
+
删除
|
|
666
|
+
</Button>
|
|
667
|
+
</DialogFooter>
|
|
668
|
+
</DialogContent>
|
|
669
|
+
</Dialog>
|
|
670
|
+
|
|
671
|
+
<ImageLightbox
|
|
672
|
+
images={lightboxImages}
|
|
673
|
+
currentIndex={lightboxIndex}
|
|
674
|
+
open={lightboxOpen}
|
|
675
|
+
onOpenChange={setLightboxOpen}
|
|
676
|
+
onIndexChange={setLightboxIndex}
|
|
677
|
+
/>
|
|
678
|
+
<Dialog open={deleteMode === "selected" || deleteMode === "filtered"} onOpenChange={(open) => (!open ? setDeleteMode(null) : null)}>
|
|
679
|
+
<DialogContent showCloseButton={false} className="rounded-2xl p-6">
|
|
680
|
+
<DialogHeader className="gap-2">
|
|
681
|
+
<DialogTitle>{deleteMode === "filtered" ? "删除匹配日期的图片" : "删除所选图片"}</DialogTitle>
|
|
682
|
+
</DialogHeader>
|
|
683
|
+
<p className="text-sm text-stone-600">
|
|
684
|
+
确认删除 {selectedCount} 张图片吗?删除后无法恢复。
|
|
685
|
+
</p>
|
|
686
|
+
<DialogFooter>
|
|
687
|
+
<Button variant="outline" className="rounded-xl" onClick={() => setDeleteMode(null)} disabled={isDeleting}>
|
|
688
|
+
取消
|
|
689
|
+
</Button>
|
|
690
|
+
<Button className="rounded-xl bg-rose-600 text-white hover:bg-rose-700" onClick={() => void confirmDelete()} disabled={isDeleting || selectedCount === 0}>
|
|
691
|
+
{isDeleting ? <LoaderCircle className="size-4 animate-spin" /> : null}
|
|
692
|
+
确认删除
|
|
693
|
+
</Button>
|
|
694
|
+
</DialogFooter>
|
|
695
|
+
</DialogContent>
|
|
696
|
+
</Dialog>
|
|
697
|
+
<Dialog open={Boolean(tagDeleteTarget)} onOpenChange={(open) => { if (!open) setTagDeleteTarget(null); }}>
|
|
698
|
+
<DialogContent className="max-w-sm rounded-2xl">
|
|
699
|
+
<DialogHeader>
|
|
700
|
+
<DialogTitle>删除标签</DialogTitle>
|
|
701
|
+
</DialogHeader>
|
|
702
|
+
<p className="text-sm text-stone-600">
|
|
703
|
+
确定要删除标签 <span className="font-semibold">"{tagDeleteTarget}"</span> 吗?将从所有图片中移除该标签。
|
|
704
|
+
</p>
|
|
705
|
+
<DialogFooter>
|
|
706
|
+
<Button variant="outline" className="rounded-xl" onClick={() => setTagDeleteTarget(null)}>
|
|
707
|
+
取消
|
|
708
|
+
</Button>
|
|
709
|
+
<Button
|
|
710
|
+
variant="destructive"
|
|
711
|
+
className="rounded-xl"
|
|
712
|
+
onClick={() => {
|
|
713
|
+
if (tagDeleteTarget) void handleDeleteTag(tagDeleteTarget);
|
|
714
|
+
setTagDeleteTarget(null);
|
|
715
|
+
}}
|
|
716
|
+
>
|
|
717
|
+
确认删除
|
|
718
|
+
</Button>
|
|
719
|
+
</DialogFooter>
|
|
720
|
+
</DialogContent>
|
|
721
|
+
</Dialog>
|
|
722
|
+
</section>
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
export default function ImageManagerPage() {
|
|
727
|
+
const { isCheckingAuth, session } = useAuthGuard(["admin"]);
|
|
728
|
+
if (isCheckingAuth || !session || session.role !== "admin") {
|
|
729
|
+
return <div className="flex min-h-[40vh] items-center justify-center"><LoaderCircle className="size-5 animate-spin text-stone-400" /></div>;
|
|
730
|
+
}
|
|
731
|
+
return <ImageManagerContent />;
|
|
732
|
+
}
|