Skip to content

Commit d237885

Browse files
committed
fix(themer): clear the image picker busy state when a pick lands while hidden
An <Activity> that goes hidden unmounts the effects of its children while keeping their state, so useImagePalette's mount ref reads as unmounted and the finally never let go of busy. The picker then showed a spinner with no work in progress once the sidebar, or the theme list, came back into view. Clearing busy now only asks whether the pick is still the latest one.
1 parent 5841759 commit d237885

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎plugins/@sanity/themer/src/tool/useImagePalette.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ export function useImagePalette(onPalette: (palette: ImagePalette, file: File) =
4040

4141
setBusy(true)
4242

43+
// Whether this pick is the one the busy state belongs to: a later pick
44+
// takes it over
45+
const isLatest = () => latestPickRef.current === pick
4346
// Whether this pick still matters: it is the latest one and the
4447
// component that asked for it is still around
45-
const isCurrent = () => mountedRef.current && latestPickRef.current === pick
48+
const isCurrent = () => mountedRef.current && isLatest()
4649

4750
const extract = async () => {
4851
const palette = extractImagePalette(await readImagePixels(file))
@@ -61,7 +64,10 @@ export function useImagePalette(onPalette: (palette: ImagePalette, file: File) =
6164
if (isCurrent()) toast.push({status: 'error', title: 'Could not read that image'})
6265
})
6366
.finally(() => {
64-
if (isCurrent()) setBusy(false)
67+
// Not held back by the mount: an `<Activity>` that goes hidden
68+
// unmounts the effects while it keeps the state, so a pick that
69+
// lands while hidden has to let go of the busy state all the same
70+
if (isLatest()) setBusy(false)
6571
})
6672
},
6773
[toast],

0 commit comments

Comments
 (0)