Skip to content

Commit

Permalink
Random recent thought on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
evwilliams committed Jan 21, 2024
1 parent 9c2a584 commit 35c4861
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/data/ThoughtStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ export const useThoughts = () => {
db.thoughts.orderBy('sortValue').reverse().toArray()
)
const [activeThought, setActiveThought] = useState<Thought>()
const [firstLoad, setFirstLoad] = useState<boolean>(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])

Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 35c4861

Please sign in to comment.