From c1ee3354378084a94a4ec19e2be6e696973fd58b Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Fri, 12 Mar 2021 17:46:20 +1100 Subject: [PATCH] Fix bug with adding image sources quickly --- src/contexts/ImageSourceContext.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/contexts/ImageSourceContext.js b/src/contexts/ImageSourceContext.js index 3f39f05..f9609f6 100644 --- a/src/contexts/ImageSourceContext.js +++ b/src/contexts/ImageSourceContext.js @@ -85,11 +85,24 @@ export function useImageSource(data, defaultSources, unknownSource, thumbnail) { function addImageSource(file) { if (file) { - const url = URL.createObjectURL(new Blob([file])); - setImageSources((prevSources) => ({ - ...prevSources, - [id]: { url, id, references: 1 }, - })); + setImageSources((prevSources) => { + if (id in prevSources) { + // Check if the image source is already added + return { + ...prevSources, + [id]: { + ...prevSources[id], + references: prevSources[id].references + 1, + }, + }; + } else { + const url = URL.createObjectURL(new Blob([file])); + return { + ...prevSources, + [id]: { url, id, references: 1 }, + }; + } + }); } }