Skip to content

Commit

Permalink
fix: refresh grid view mode on alt generation complete
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Apr 16, 2024
1 parent 1c8adef commit 23a5d49
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion includes/AltGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function generate_alt_text( int $attachment_id, string $user_promp

$completion = json_decode( wp_remote_retrieve_body( $api_response ), true );

if ( $completion['error'] ) {
if ( isset( $completion['error'] ) ) {
return new WP_Error(
$completion['error']['code'],
// translators: %s is the error message from OpenAI's API.
Expand Down
15 changes: 14 additions & 1 deletion src/components/BulkGenerateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default function BulkGenerateModal({

const handleStart = async () => {
setIsGenerating(true);
const generateTasks = [];

for (const [id, details] of altGenerationMap) {
if (!overwriteExisting && details.alt.length > 0) {
Expand All @@ -84,8 +85,15 @@ export default function BulkGenerateModal({
new Map(prevMap.set(id, { ...details, status: "generating" })),
);

generateAltText(id, true, customPrompt)
const task = generateAltText(id, true, customPrompt)
.then((alt) => {
const attachment = attachments.find(
(attachment) => attachment.id === id,
);
if (attachment) {
attachment.alt_text = alt;
}

setAltGenerationMap(
(prevMap) =>
new Map(
Expand All @@ -107,14 +115,19 @@ export default function BulkGenerateModal({
console.error(error);
});

generateTasks.push(task);

// Wait for 1 second before processing the next image to avoid too many requests at once
// TODO:
// Use rate limiting info and implement a better solution
// https://platform.openai.com/docs/guides/rate-limits/rate-limits-in-headers
await sleep(1000);
}

await Promise.all(generateTasks);
setIsGenerating(false);

document.dispatchEvent(new CustomEvent("altTextsGenerated"));
};

return (
Expand Down
9 changes: 9 additions & 0 deletions src/media/extend/extendMediaBulkSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export default (onButtonClick) => {
const selection = this.controller.state().get("selection");
if (!selection.length) return;

selection.models[0].set("alt", "dupa");
selection.models[0].save();

onButtonClick(selection.models.map((model) => model.id));
},
});
Expand All @@ -73,6 +76,12 @@ export default (onButtonClick) => {
priority: -75,
}).render(),
);

document.addEventListener("altTextsGenerated", (event) => {
const library = this.controller.state().get("library");
library._requery(true);
this.controller.trigger("selection:action:done");
});
},
});
};
6 changes: 3 additions & 3 deletions src/media/media-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const App = () => {

// Grid mode
useEffect(() => {
const listener = (e: CustomEvent<number[]>) => {
setSelectedMediaIds(e.detail);
const listener = (e: CustomEvent<{ ids: number[] }>) => {
setSelectedMediaIds(e.detail.ids);
setIsModalOpen(true);
};

Expand Down Expand Up @@ -66,7 +66,7 @@ domReady(() => {
extendMediaBulkSelect((ids) => {
document.dispatchEvent(
new CustomEvent("triggerBulkAltGenerateModal", {
detail: ids,
detail: { ids },
}),
);
});
Expand Down
6 changes: 6 additions & 0 deletions src/types/dom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare global {
interface DocumentEventMap {
triggerBulkAltGenerateModal: { ids: number[] };
altTextsGenerated: CustomEvent;
}
}

0 comments on commit 23a5d49

Please sign in to comment.