Skip to content

Commit 369533a

Browse files
committed
fix: restructure useeffect
1 parent 9e4a6ff commit 369533a

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

package/src/components/MessageInput/hooks/useAudioPreviewManager.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ export const useAudioPreviewManager = (files: LocalAttachment[]) => {
1515
>({});
1616

1717
useEffect(() => {
18-
const updatedStateMap = Object.fromEntries(
19-
files.map((attachment) => {
20-
const id = attachment.localMetadata.id;
21-
const existingConfig = audioAttachmentsStateMap[id];
18+
setAudioAttachmentsStateMap((prevState) => {
19+
const updatedStateMap = Object.fromEntries(
20+
files.map((attachment) => {
21+
const id = attachment.localMetadata.id;
2222

23-
const config: AudioConfig = {
24-
duration: attachment.duration ?? existingConfig?.duration ?? 0,
25-
paused: existingConfig?.paused ?? true,
26-
progress: existingConfig?.progress ?? 0,
27-
};
23+
const config: AudioConfig = {
24+
duration: attachment.duration ?? prevState[id]?.duration ?? 0,
25+
paused: prevState[id]?.paused ?? true,
26+
progress: prevState[id]?.progress ?? 0,
27+
};
2828

29-
return [id, config];
30-
}),
31-
);
29+
return [id, config];
30+
}),
31+
);
3232

33-
setAudioAttachmentsStateMap(updatedStateMap);
34-
// eslint-disable-next-line react-hooks/exhaustive-deps
33+
return updatedStateMap;
34+
});
3535
}, [files]);
3636

3737
// Handler triggered when an audio is loaded in the message input. The initial state is defined for the audio here

0 commit comments

Comments
 (0)