Skip to content

Commit

Permalink
Persist writing session timer on first save when routes change
Browse files Browse the repository at this point in the history
  • Loading branch information
0xi4o committed Aug 11, 2024
1 parent d635757 commit 81d9db1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
6 changes: 1 addition & 5 deletions app/components/common/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,7 @@ const Editor = ({
}

const handleTitleChange = (title: string) => {
if (editorData.title.trim() === '') {
setEditorData({ title }, { ignoreAutoSave: isTitleFirstEdit })
} else {
setEditorData({ title })
}
setEditorData({ title }, { ignoreAutoSave: isTitleFirstEdit })
}

const handleWordCountChange = (count: number) => {
Expand Down
6 changes: 5 additions & 1 deletion app/components/editor/writing-session-timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
FormEvent,
ReactNode,
SetStateAction,
useContext,
useEffect,
useState,
} from 'react'
Expand Down Expand Up @@ -40,6 +41,7 @@ import {
TooltipTrigger,
} from '~/components/ui/tooltip'
import { useToast } from '~/components/ui/use-toast'
import { AureliusContext, AureliusProviderData } from '~/lib/providers/aurelius'
import {
WritingSessionDialogProps,
WritingSessionSettings,
Expand Down Expand Up @@ -107,9 +109,11 @@ const WritingSessionTimer = ({
writingSessionOpen,
writingSessionSettings,
}: WritingSessionTimerProps) => {
const { sessionTimer } = useContext<AureliusProviderData>(AureliusContext)

const [elapsedMinutes, setElapsedMinutes] = useState(0)
const [startingWordCount, setStartingWordCount] = useState(wordCount)
const sessionTimer = useTimer()

const { toast } = useToast()

const pauseWritingSession = () => {
Expand Down
6 changes: 6 additions & 0 deletions app/lib/providers/aurelius.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { ReactNode, Suspense, createContext } from 'react'
import { Timer, useTimer } from 'react-use-precision-timer'

import { useQuery } from '@evolu/react'
import LoadingScreen from '~/components/common/loading-screen'
import { SettingsRow, settingsQuery } from '~/services/evolu/client'

export type AureliusProviderData = {
sessionTimer: Timer
settings: SettingsRow
}

export const AureliusContext = createContext<AureliusProviderData>({
// @ts-expect-error: tradeoff between having this comment in multiple places or one place
sessionTimer: null,
// @ts-expect-error: tradeoff between having this comment in multiple places or one place
settings: null,
})
Expand All @@ -19,8 +23,10 @@ type AureliusProviderProps = {

const AureliusProvider = ({ children }: AureliusProviderProps) => {
const { rows: settings } = useQuery(settingsQuery)
const sessionTimer = useTimer()

const data: AureliusProviderData = {
sessionTimer,
settings: settings[0],
}

Expand Down

0 comments on commit 81d9db1

Please sign in to comment.