Refine async cache invalidation when installing photos

This commit is contained in:
Sam Becker 2025-01-05 17:12:51 -06:00
parent 6dbedd3d6d
commit 3f29a4d0aa
2 changed files with 13 additions and 3 deletions

View File

@ -46,13 +46,14 @@ export default function AdminAddAllUploads({
const router = useRouter();
const addedUploadCount = useRef(0);
const addUploadUrls = async (uploadUrls: string[]) => {
const addUploadUrls = async (uploadUrls: string[], isFinalBatch: boolean) => {
try {
const stream = await addAllUploadsAction({
uploadUrls,
tags: showTags ? tags : undefined,
takenAtLocal: generateLocalPostgresString(),
takenAtNaiveLocal: generateLocalNaivePostgresString(),
shouldRevalidateAllKeysAndPaths: isFinalBatch,
});
for await (const data of readStreamableValue(stream)) {
setButtonText(addedUploadCount.current === 0
@ -152,8 +153,11 @@ export default function AdminAddAllUploads({
const uploadsToAdd = storageUrls.slice();
try {
while (uploadsToAdd.length > 0) {
const nextBatch = uploadsToAdd
.splice(0, UPLOAD_BATCH_SIZE);
await addUploadUrls(
uploadsToAdd.splice(0, UPLOAD_BATCH_SIZE),
nextBatch,
uploadsToAdd.length === 0,
);
}
setButtonText('Complete');

View File

@ -49,6 +49,7 @@ import { createStreamableValue } from 'ai/rsc';
import { convertUploadToPhoto } from './storage';
import { UrlAddStatus } from '@/admin/AdminUploadsClient';
import { convertStringToArray } from '@/utility/string';
import { after } from 'next/server';
// Private actions
@ -77,11 +78,13 @@ export const addAllUploadsAction = async ({
tags,
takenAtLocal,
takenAtNaiveLocal,
shouldRevalidateAllKeysAndPaths = true,
}: {
uploadUrls: string[]
tags?: string
takenAtLocal: string
takenAtNaiveLocal: string
shouldRevalidateAllKeysAndPaths?: boolean
}) =>
runAuthenticatedAdminServerAction(async () => {
const PROGRESS_TASK_COUNT = AI_TEXT_GENERATION_ENABLED ? 5 : 4;
@ -169,10 +172,13 @@ export const addAllUploadsAction = async ({
// eslint-disable-next-line max-len
stream.error(`${error.message} (${addedUploadUrls.length} of ${uploadUrls.length} photos successfully added)`);
}
revalidateAllKeysAndPaths();
stream.done();
})();
if (shouldRevalidateAllKeysAndPaths) {
after(revalidateAllKeysAndPaths)
}
return stream.value;
});