diff --git a/src/data/ThoughtStore.ts b/src/data/ThoughtStore.ts index 754585c..60f56ab 100644 --- a/src/data/ThoughtStore.ts +++ b/src/data/ThoughtStore.ts @@ -11,14 +11,15 @@ export const useThoughts = () => { db.thoughts.orderBy('sortValue').reverse().toArray() ) const [activeThought, setActiveThought] = useState() + const [firstLoad, setFirstLoad] = useState(true) useEffect(() => { if ( - sortedThoughts && - sortedThoughts.length > 0 && - (!activeThought || activeThought != sortedThoughts[0]) + sortedThoughts?.length && + (!activeThought || activeThought !== sortedThoughts[0]) ) { - setActiveThought(sortedThoughts[0]) + firstLoad ? focusRecentThought() : setActiveThought(sortedThoughts[0]) + setFirstLoad(false) } }, [sortedThoughts]) @@ -49,6 +50,14 @@ export const useThoughts = () => { }) } + const focusRecentThought = () => { + if (!sortedThoughts?.length) return + + // Get a thought from the first 40% of thoughts + const index = Math.floor(0.4 * sortedThoughts.length) + focusThought(sortedThoughts[index]) + } + const soonIndex = (length: number) => { return length * 0.15 + (length / 2) * Math.random() }