Skip to content

Commit 470b18f

Browse files
committed
Add local state management to prevent race conditions when toggling starred items in Timeline
1 parent 1bbaea8 commit 470b18f

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

  • packages/ui/src/components/editor/panel/Timeline

‎packages/ui/src/components/editor/panel/Timeline/Timeline.tsx‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useState } from 'react'
12
import styles from './Timeline.module.scss'
23
import cn from 'classnames'
34
import dayjs from 'dayjs'
@@ -30,9 +31,17 @@ export const Timeline: React.FC<Props> = ({
3031
on_delete,
3132
on_edit
3233
}) => {
34+
const [local_items, set_local_items] = useState(items)
35+
const [locked, set_locked] = useState(false)
36+
37+
useEffect(() => {
38+
set_local_items(items)
39+
set_locked(false)
40+
}, [items])
41+
3342
return (
3443
<div className={styles.timeline}>
35-
{items.map((item) => (
44+
{local_items.map((item) => (
3645
<div
3746
key={item.id}
3847
className={styles.item}
@@ -70,7 +79,16 @@ export const Timeline: React.FC<Props> = ({
7079
title={item.is_starred ? 'Unstar' : 'Star'}
7180
on_click={(e) => {
7281
e.stopPropagation()
73-
on_toggle_starred?.(item.id)
82+
if (locked) return
83+
set_locked(true)
84+
set_local_items((prev) =>
85+
prev.map((i) =>
86+
i.id == item.id
87+
? { ...i, is_starred: !i.is_starred }
88+
: i
89+
)
90+
)
91+
on_toggle_starred(item.id)
7492
}}
7593
/>
7694
{on_edit && item.can_edit && (

0 commit comments

Comments
 (0)