Add the optional Immich integration and manual gallery push
Immich connections stay server-side with an encrypted API key, albums can be imported, and photos are only sent back when an event's push button is pressed: originals upload once, edited photos re-render, and edits hashes plus checksums skip anything already in Immich. This also lands the accompanying editor, cloud and administration work that was still uncommitted. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
+335
-16
@@ -9,6 +9,7 @@ import ExportModal from './components/ExportModal';
|
||||
import CompareView from './components/CompareView';
|
||||
import FilterBar from './components/FilterBar';
|
||||
import ImportModal from './components/ImportModal';
|
||||
import ImmichPanel from './components/ImmichPanel';
|
||||
import LibraryGrid from './components/LibraryGrid';
|
||||
import MergeModal from './components/MergeModal';
|
||||
import PasteSettingsModal from './components/PasteSettingsModal';
|
||||
@@ -22,18 +23,23 @@ import {
|
||||
currentUser,
|
||||
deleteEventRemote,
|
||||
deletePhotoRemote,
|
||||
createImmichEventAlbum,
|
||||
getHealth,
|
||||
getImmichStatus,
|
||||
getQuota,
|
||||
immichEventQueue,
|
||||
login as apiLogin,
|
||||
logout as apiLogout,
|
||||
pullAll,
|
||||
pullProofs,
|
||||
pushEvent,
|
||||
publishImmichOriginal,
|
||||
pushImmichPhoto,
|
||||
pushPhoto,
|
||||
register as apiRegister,
|
||||
uploadOriginal,
|
||||
} from './lib/api';
|
||||
import type { Health, ProofSummary, QuotaState, SessionUser } from './lib/api';
|
||||
import type { Health, ImmichStatus, ProofSummary, QuotaState, SessionUser } from './lib/api';
|
||||
import {
|
||||
deleteEventRecord,
|
||||
deletePhotoRecord,
|
||||
@@ -136,6 +142,41 @@ export default function App() {
|
||||
const [cloudUser, setCloudUser] = useState<SessionUser | null>(null);
|
||||
const [cloudOpen, setCloudOpen] = useState(false);
|
||||
const [adminOpen, setAdminOpen] = useState(false);
|
||||
const [immichOpen, setImmichOpen] = useState(false);
|
||||
const [immichStatus, setImmichStatus] = useState<ImmichStatus | null>(null);
|
||||
const [immichProgress, setImmichProgress] = useState<{
|
||||
done: number;
|
||||
total: number;
|
||||
current: string;
|
||||
} | null>(null);
|
||||
const immichStatusRef = useRef<ImmichStatus | null>(null);
|
||||
immichStatusRef.current = immichStatus;
|
||||
const immichBusyRef = useRef(false);
|
||||
|
||||
// The Immich entry points only appear when this account has opted in.
|
||||
useEffect(() => {
|
||||
if (!cloudUser) {
|
||||
setImmichStatus(null);
|
||||
return;
|
||||
}
|
||||
let alive = true;
|
||||
getImmichStatus()
|
||||
.then((status) => {
|
||||
if (alive) setImmichStatus(status);
|
||||
})
|
||||
.catch(() => {
|
||||
if (alive) setImmichStatus(null);
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [cloudUser]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
},
|
||||
[],
|
||||
);
|
||||
const [cloudBusy, setCloudBusy] = useState(false);
|
||||
const [cloudError, setCloudError] = useState<string | null>(null);
|
||||
const [cloudNote, setCloudNote] = useState<string | null>(null);
|
||||
@@ -337,6 +378,16 @@ export default function App() {
|
||||
[queueSave],
|
||||
);
|
||||
|
||||
const immichEventIds = useMemo(
|
||||
() => new Set(events.filter((event) => event.immich).map((event) => event.id)),
|
||||
[events],
|
||||
);
|
||||
const canSendToImmich = useCallback(
|
||||
(photo: PhotoRecord) =>
|
||||
Boolean(immichStatus?.enabled && (photo.immich || immichEventIds.has(photo.eventId))),
|
||||
[immichStatus?.enabled, immichEventIds],
|
||||
);
|
||||
|
||||
const livePhotos = useMemo(() => photos.filter((p) => !p.deletedAt), [photos]);
|
||||
const trashCount = photos.length - livePhotos.length;
|
||||
const proofCounts = useMemo(() => {
|
||||
@@ -475,19 +526,56 @@ export default function App() {
|
||||
[activeId],
|
||||
);
|
||||
|
||||
const createEvent = useCallback(async (name: string) => {
|
||||
const event: EventRecord = {
|
||||
id: uid(),
|
||||
name: name.trim() || `Event ${new Date().toLocaleDateString()}`,
|
||||
createdAt: Date.now(),
|
||||
date: Date.now(),
|
||||
order: eventsRef.current.length,
|
||||
workspace: userRef.current?.id,
|
||||
};
|
||||
await putEvent(event);
|
||||
if (userRef.current) pushEvent(event).catch(() => undefined);
|
||||
setEvents((prev) => [...prev, event].sort(byOrder));
|
||||
return event;
|
||||
const createEvent = useCallback(
|
||||
async (name: string, options: { immichGallery?: boolean } = {}) => {
|
||||
const event: EventRecord = {
|
||||
id: uid(),
|
||||
name: name.trim() || `Event ${new Date().toLocaleDateString()}`,
|
||||
createdAt: Date.now(),
|
||||
date: Date.now(),
|
||||
order: eventsRef.current.length,
|
||||
workspace: userRef.current?.id,
|
||||
};
|
||||
await putEvent(event);
|
||||
// The gallery endpoint looks the event up server-side, so let this land first.
|
||||
if (userRef.current) await pushEvent(event).catch(() => undefined);
|
||||
let created = event;
|
||||
if (options.immichGallery && userRef.current && immichStatusRef.current?.enabled) {
|
||||
try {
|
||||
const result = await createImmichEventAlbum(event.id, event.name);
|
||||
created = result.event;
|
||||
await putEvent(created);
|
||||
setToast(`Immich gallery created for ${created.name}`);
|
||||
} catch (err) {
|
||||
setToast(
|
||||
`Event created, but the Immich gallery failed: ${
|
||||
err instanceof Error ? err.message : 'unknown error'
|
||||
}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
const nextEvents = [...eventsRef.current.filter((e) => e.id !== created.id), created].sort(byOrder);
|
||||
// ImportModal creates an event and immediately asks for its Immich gallery.
|
||||
// Update the ref now so that follow-up call can find the new event.
|
||||
eventsRef.current = nextEvents;
|
||||
setEvents(nextEvents);
|
||||
return created;
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const createImmichGalleryForEvent = useCallback(async (id: string) => {
|
||||
const event = eventsRef.current.find((item) => item.id === id);
|
||||
if (!event) throw new Error('No such event');
|
||||
const result = await createImmichEventAlbum(id, event.name);
|
||||
await putEvent(result.event);
|
||||
const nextEvents = eventsRef.current
|
||||
.map((item) => (item.id === id ? result.event : item))
|
||||
.sort(byOrder);
|
||||
eventsRef.current = nextEvents;
|
||||
setEvents(nextEvents);
|
||||
setToast(`Immich gallery created for ${event.name}`);
|
||||
return result.event;
|
||||
}, []);
|
||||
|
||||
const renameEvent = useCallback(async (id: string, name: string) => {
|
||||
@@ -1236,6 +1324,169 @@ export default function App() {
|
||||
}
|
||||
}, [activePhoto, bridgeEvent]);
|
||||
|
||||
/**
|
||||
* Renders each edit and uploads the queue one at a time. Checksums make a
|
||||
* repeat send of an unchanged edit a no-op on the server.
|
||||
*/
|
||||
const pushImmichQueue = useCallback(async (targets: PhotoRecord[]) => {
|
||||
if (immichBusyRef.current) return;
|
||||
const queue = targets.filter(
|
||||
(photo) => photo.immich || immichEventIds.has(photo.eventId),
|
||||
);
|
||||
const localCount = targets.length - queue.length;
|
||||
if (!queue.length) {
|
||||
setToast('None of the selected photos can go to Immich yet');
|
||||
return;
|
||||
}
|
||||
immichBusyRef.current = true;
|
||||
let sent = 0;
|
||||
let skipped = 0;
|
||||
let failed = 0;
|
||||
let trashed = 0;
|
||||
let warning = '';
|
||||
setImmichProgress({ done: 0, total: queue.length, current: queue[0].name });
|
||||
try {
|
||||
for (let i = 0; i < queue.length; i += 1) {
|
||||
const photo = queue[i];
|
||||
setImmichProgress({ done: i, total: queue.length, current: photo.name });
|
||||
try {
|
||||
const blob = await renderPhotoBlob(photo, {
|
||||
...DEFAULT_EXPORT,
|
||||
format: 'jpeg',
|
||||
quality: 92,
|
||||
resize: 'none',
|
||||
});
|
||||
const result = await pushImmichPhoto(photo.id, await blobToBase64(blob));
|
||||
if (result.alreadyPushed) skipped += 1;
|
||||
else sent += 1;
|
||||
if (result.trashed) trashed += 1;
|
||||
if (result.warning && !warning) warning = result.warning;
|
||||
} catch (err) {
|
||||
failed += 1;
|
||||
if (!warning) warning = err instanceof Error ? err.message : 'A photo could not be sent';
|
||||
}
|
||||
setImmichProgress({ done: i + 1, total: queue.length, current: photo.name });
|
||||
// Let the progress paint before the next full-resolution render.
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
} finally {
|
||||
immichBusyRef.current = false;
|
||||
setImmichProgress(null);
|
||||
}
|
||||
const parts = [];
|
||||
if (sent) parts.push(`Sent ${sent} edit${sent === 1 ? '' : 's'}`);
|
||||
if (skipped) parts.push(`${skipped} already in Immich`);
|
||||
if (localCount) parts.push(`${localCount} not from Immich`);
|
||||
if (trashed) parts.push(`${trashed} original${trashed === 1 ? '' : 's'} in trash`);
|
||||
if (failed) parts.push(`${failed} failed`);
|
||||
let message = parts.length ? parts.join(' · ') : 'Nothing to send';
|
||||
if (warning) message += ` · ${warning}`;
|
||||
setToast(message);
|
||||
}, [immichEventIds]);
|
||||
|
||||
/**
|
||||
* Manual push for one event. Photos Immich has never seen upload their
|
||||
* original from the server; photos edited since the last push are rendered
|
||||
* here so the gallery receives the current look. Nothing runs on its own -
|
||||
* this only happens when the button is pressed, and each finished upload is
|
||||
* recorded so a repeat press skips everything that is already up to date.
|
||||
*/
|
||||
const syncEventGallery = useCallback(async (targetEventId: string) => {
|
||||
if (immichBusyRef.current) {
|
||||
setToast('An Immich upload is already running');
|
||||
return;
|
||||
}
|
||||
if (!immichStatusRef.current?.enabled) {
|
||||
setToast('Turn on Immich in the Immich sync panel first');
|
||||
return;
|
||||
}
|
||||
|
||||
let plan: { photoId: string; mode: 'original' | 'edit' }[];
|
||||
try {
|
||||
plan = (await immichEventQueue(targetEventId)).queue;
|
||||
} catch (err) {
|
||||
setToast(err instanceof Error ? err.message : 'Could not read the Immich queue');
|
||||
return;
|
||||
}
|
||||
const byId = new Map(
|
||||
photosRef.current.filter((photo) => !photo.deletedAt).map((photo) => [photo.id, photo]),
|
||||
);
|
||||
const queue: { entry: { photoId: string; mode: 'original' | 'edit' }; photo: PhotoRecord }[] = [];
|
||||
for (const entry of plan) {
|
||||
const photo = byId.get(entry.photoId);
|
||||
if (!photo) continue;
|
||||
// A photo Immich already has only needs a new upload once it is edited.
|
||||
if (entry.mode === 'edit' && !isEdited(photo.edits)) continue;
|
||||
queue.push({ entry, photo });
|
||||
}
|
||||
if (!queue.length) {
|
||||
setToast('Everything in this event is already in Immich');
|
||||
return;
|
||||
}
|
||||
|
||||
immichBusyRef.current = true;
|
||||
let sent = 0;
|
||||
let skipped = 0;
|
||||
let failed = 0;
|
||||
let warning = '';
|
||||
setImmichProgress({ done: 0, total: queue.length, current: queue[0].photo.name });
|
||||
try {
|
||||
for (let i = 0; i < queue.length; i += 1) {
|
||||
const { entry, photo } = queue[i];
|
||||
setImmichProgress({ done: i, total: queue.length, current: photo.name });
|
||||
try {
|
||||
const result = entry.mode === 'edit'
|
||||
? await pushImmichPhoto(
|
||||
photo.id,
|
||||
await blobToBase64(
|
||||
await renderPhotoBlob(photo, {
|
||||
...DEFAULT_EXPORT,
|
||||
format: 'jpeg',
|
||||
quality: 92,
|
||||
resize: 'none',
|
||||
}),
|
||||
),
|
||||
)
|
||||
: await publishImmichOriginal(photo.id);
|
||||
if (result.alreadyPushed || result.skipped) skipped += 1;
|
||||
else sent += 1;
|
||||
if (result.warning && !warning) warning = result.warning;
|
||||
} catch (err) {
|
||||
failed += 1;
|
||||
if (!warning) {
|
||||
warning = `${photo.name}: ${
|
||||
err instanceof Error ? err.message : 'could not be sent'
|
||||
}`;
|
||||
}
|
||||
}
|
||||
setImmichProgress({ done: i + 1, total: queue.length, current: photo.name });
|
||||
// Let the progress paint before the next render or upload.
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
} finally {
|
||||
immichBusyRef.current = false;
|
||||
setImmichProgress(null);
|
||||
}
|
||||
|
||||
const parts = [];
|
||||
if (sent) parts.push(`Sent ${sent} photo${sent === 1 ? '' : 's'}`);
|
||||
if (skipped) parts.push(`${skipped} already in Immich`);
|
||||
if (failed) parts.push(`${failed} failed`);
|
||||
let message = parts.length ? parts.join(' · ') : 'Nothing to send';
|
||||
if (warning) message += ` · ${warning}`;
|
||||
setToast(message);
|
||||
}, []);
|
||||
|
||||
const openImmichAsset = useCallback((photo: PhotoRecord) => {
|
||||
const status = immichStatusRef.current;
|
||||
if (!status?.url || !photo.immich) return;
|
||||
window.open(
|
||||
`${status.url.replace(/\/+$/, '')}/photos/${encodeURIComponent(photo.immich.assetId)}`,
|
||||
'_blank',
|
||||
'noopener,noreferrer',
|
||||
);
|
||||
}, []);
|
||||
|
||||
const handleCloudLogin = useCallback(
|
||||
async (email: string, password: string) => {
|
||||
setCloudBusy(true);
|
||||
@@ -1754,9 +2005,11 @@ export default function App() {
|
||||
setTrashOpen(false);
|
||||
setEventId(id);
|
||||
}}
|
||||
onCreate={async (name) => {
|
||||
await createEvent(name);
|
||||
immichEnabled={Boolean(immichStatus?.enabled)}
|
||||
onCreate={async (name, immichGallery) => {
|
||||
await createEvent(name, { immichGallery });
|
||||
}}
|
||||
onSyncGallery={syncEventGallery}
|
||||
onRename={renameEvent}
|
||||
onDelete={removeEvent}
|
||||
onReorderEvents={reorderEvents}
|
||||
@@ -1855,6 +2108,15 @@ export default function App() {
|
||||
onBulkRate={(rating) => bulkUpdate(selected, { rating })}
|
||||
onBulkFlag={(flag) => bulkUpdate(selected, { flag })}
|
||||
onBulkColor={(color) => bulkUpdate(selected, { color })}
|
||||
onSendToImmich={
|
||||
selected.some((id) => {
|
||||
const photo = photos.find((item) => item.id === id);
|
||||
return photo ? canSendToImmich(photo) : false;
|
||||
})
|
||||
? () => void pushImmichQueue(photos.filter((photo) => selected.includes(photo.id)))
|
||||
: undefined
|
||||
}
|
||||
immichBusy={Boolean(immichProgress)}
|
||||
onDelete={() => (trashOpen ? void removePhotos(selected) : trashPhotos(selected))}
|
||||
onExport={openExport}
|
||||
onCompare={() => setCompareOpen(true)}
|
||||
@@ -1899,6 +2161,11 @@ export default function App() {
|
||||
onLocalClipboard={setLocalClipboard}
|
||||
onExport={openExport}
|
||||
onPrint={() => setPrintOpen(true)}
|
||||
canPushImmich={canSendToImmich(activePhoto)}
|
||||
onPushImmich={() => void pushImmichQueue([activePhoto])}
|
||||
onOpenImmich={
|
||||
activePhoto.immich ? () => openImmichAsset(activePhoto) : undefined
|
||||
}
|
||||
presets={[...PRESETS, ...customPresets]}
|
||||
onSavePreset={savePreset}
|
||||
onDelete={() => trashPhotos([activePhoto.id])}
|
||||
@@ -1949,6 +2216,8 @@ export default function App() {
|
||||
setToast(`Imported ${imported.length} photo${imported.length === 1 ? '' : 's'}`);
|
||||
}}
|
||||
onCreateEvent={createEvent}
|
||||
immichEnabled={Boolean(immichStatus?.enabled)}
|
||||
onCreateImmichGallery={createImmichGalleryForEvent}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -2061,6 +2330,10 @@ export default function App() {
|
||||
setCloudOpen(false);
|
||||
setAdminOpen(true);
|
||||
}}
|
||||
onImmich={() => {
|
||||
setCloudOpen(false);
|
||||
setImmichOpen(true);
|
||||
}}
|
||||
onClose={() => setCloudOpen(false)}
|
||||
/>
|
||||
)}
|
||||
@@ -2071,11 +2344,23 @@ export default function App() {
|
||||
onClose={() => setAdminOpen(false)}
|
||||
onChanged={() => {
|
||||
void getHealth().then((health) => setCloudHealth(health));
|
||||
void getImmichStatus().then(setImmichStatus).catch(() => undefined);
|
||||
}}
|
||||
onRestored={() => void refreshCatalogFromServer()}
|
||||
/>
|
||||
)}
|
||||
|
||||
{immichOpen && cloudUser && (
|
||||
<ImmichPanel
|
||||
onClose={() => setImmichOpen(false)}
|
||||
onChanged={() => {
|
||||
void getImmichStatus().then(setImmichStatus).catch(() => undefined);
|
||||
void refreshCatalogFromServer();
|
||||
}}
|
||||
onToast={setToast}
|
||||
/>
|
||||
)}
|
||||
|
||||
{menu &&
|
||||
(() => {
|
||||
const targets = photos.filter((p) => menu.ids.includes(p.id));
|
||||
@@ -2133,6 +2418,21 @@ export default function App() {
|
||||
disabled: menu.ids.length < 2,
|
||||
onSelect: () => setMergeOpen(true),
|
||||
},
|
||||
...(targets.some(canSendToImmich)
|
||||
? ([
|
||||
{
|
||||
label:
|
||||
targets.filter(canSendToImmich).length === 1
|
||||
? 'Send edit to Immich'
|
||||
: `Send ${targets.filter(canSendToImmich).length} edits to Immich`,
|
||||
disabled: Boolean(immichProgress),
|
||||
onSelect: () => void pushImmichQueue(targets),
|
||||
},
|
||||
...(lead.immich
|
||||
? [{ label: 'Open in Immich', onSelect: () => openImmichAsset(lead) }]
|
||||
: []),
|
||||
] as MenuEntry[])
|
||||
: []),
|
||||
{ label: 'Export…', onSelect: openExport },
|
||||
{ label: 'Print…', onSelect: () => setPrintOpen(true) },
|
||||
{
|
||||
@@ -2168,6 +2468,25 @@ export default function App() {
|
||||
/>
|
||||
)}
|
||||
|
||||
{immichProgress && (
|
||||
<div className="toast immich-progress" role="status" aria-live="polite">
|
||||
<div className="immich-progress-head">
|
||||
<strong>Sending to Immich</strong>
|
||||
<span>
|
||||
{immichProgress.done} / {immichProgress.total}
|
||||
</span>
|
||||
</div>
|
||||
<div className="quota-meter">
|
||||
<span
|
||||
style={{
|
||||
width: `${immichProgress.total ? Math.round((immichProgress.done / immichProgress.total) * 100) : 0}%`,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<span className="muted small">{immichProgress.current}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{toast && (
|
||||
<div className="toast">
|
||||
<span>{toast}</span>
|
||||
|
||||
Reference in New Issue
Block a user