From 2009ce1b7221b6792196a995e550cda478677049 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 29 Jun 2023 15:03:38 -0700 Subject: [PATCH] Fix state of articleId preventing labels from saving on reader view --- packages/web/lib/hooks/useSetPageLabels.tsx | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/web/lib/hooks/useSetPageLabels.tsx b/packages/web/lib/hooks/useSetPageLabels.tsx index 339fe7a06..41c748720 100644 --- a/packages/web/lib/hooks/useSetPageLabels.tsx +++ b/packages/web/lib/hooks/useSetPageLabels.tsx @@ -13,7 +13,7 @@ export type LabelsDispatcher = (action: { export const useSetPageLabels = ( articleId?: string ): [{ labels: Label[] }, LabelsDispatcher] => { - const saveLabels = (labels: Label[]) => { + const saveLabels = (labels: Label[], articleId: string) => { ;(async () => { const labelIds = labels.map((l) => l.id) if (articleId) { @@ -30,7 +30,8 @@ export const useSetPageLabels = ( const labelsReducer = ( state: { labels: Label[] - throttledSave: (labels: Label[]) => void + articleId: string | undefined + throttledSave: (labels: Label[], articleId: string) => void }, action: { type: string @@ -51,8 +52,8 @@ export const useSetPageLabels = ( } } case 'SAVE': { - if (articleId) { - state.throttledSave(action.labels) + if (state.articleId) { + state.throttledSave(action.labels, state.articleId) } else { showErrorToast('Unable to update labels', { position: 'bottom-right', @@ -69,11 +70,15 @@ export const useSetPageLabels = ( } const debouncedSave = useCallback( - throttle((labels: Label[]) => saveLabels(labels), 2000), + throttle( + (labels: Label[], articleId: string) => saveLabels(labels, articleId), + 2000 + ), [] ) const [labels, dispatchLabels] = useReducer(labelsReducer, { labels: [], + articleId: articleId, throttledSave: debouncedSave, })