Skip to content

Commit

Permalink
tomatoros counter
Browse files Browse the repository at this point in the history
  • Loading branch information
tonymtz committed May 3, 2024
1 parent 8923fce commit 621e1e5
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 4 deletions.
1 change: 1 addition & 0 deletions components/molecules/tomato-counter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './tomato-counter.component'
18 changes: 18 additions & 0 deletions components/molecules/tomato-counter/tomato-counter.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FC } from 'react'
import { Flex } from 'theme-ui'

import { Interval, useIntervalsStore } from '~/stores/intervals'

export const TomatoCounter: FC = () => {
const { intervals } = useIntervalsStore()

function isWorkInterval (interval: Interval) {
return interval.type === 'WORK'
}

return (
<Flex sx={ { gap: '1em', justifyContent: 'center' } }>
<h3>Tomatoros: { intervals.filter(isWorkInterval).length }</h3>
</Flex>
)
}
7 changes: 6 additions & 1 deletion contexts/timer/timer-context.provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { ReactNode, useCallback, useEffect, useRef } from 'react'

import { useNotificationsContext } from '~/contexts/notifications'
import { useIntervalsStore } from '~/stores/intervals'
import { useSettingsStore } from '~/stores/settings'
import { useTimerStore } from '~/stores/time'
import { NOTIFICATION } from '~/utils/config'
Expand All @@ -25,6 +26,7 @@ export const useTimerContext = () => {

export const TimerProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const { notify } = useNotificationsContext()
const { addInterval } = useIntervalsStore()
const { reset, setTotalTime, start, stop, tick, time } = useTimerStore()
const [currentSegment, workLength, shortLength, longLength] = useSettingsStore(state => [
state.currentSegment,
Expand Down Expand Up @@ -58,9 +60,12 @@ export const TimerProvider: React.FC<{ children: ReactNode }> = ({ children }) =
useEffect(() => {
if (time < 1) {
onStopTimer()
addInterval({
type: currentSegment,
})
notify(NOTIFICATION)
}
}, [notify, onStopTimer, time])
}, [addInterval, currentSegment, notify, onStopTimer, time])

const onSegmentChange = useCallback((totalTime: number) => {
setTotalTime(totalTime)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tomatoro",
"version": "3.0.184",
"version": "3.0.185",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
2 changes: 2 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react'
import { Box, Divider } from 'theme-ui'

import { Screen } from '~/components/atoms/screen'
import { TomatoCounter } from '~/components/molecules/tomato-counter'
import { NotificationsWarn } from '~/components/organisms/notifications-warn'
import { GetInTouch } from '~/components/templates/get-in-touch'
import { HowItWorks } from '~/components/templates/how-it-works'
Expand Down Expand Up @@ -34,6 +35,7 @@ export default function Home ({ banners }: { banners: Banner[] }) {
<Box pt={ 4 } pb={ 5 }>
<NotificationsWarn/>
<TimerWithSelector/>
<TomatoCounter/>
</Box>

<Divider/>
Expand Down
2 changes: 2 additions & 0 deletions stores/intervals/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './intervals.store'
export * from './intervals-store.types'
15 changes: 15 additions & 0 deletions stores/intervals/intervals-store.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SegmentType } from '~/utils/config'

export interface Interval {
type: SegmentType
}

export interface IntervalsState {
intervals: Interval[]
}

export interface IntervalsStore extends IntervalsState {
addInterval(interval: Interval): void

resetIntervals(): void
}
54 changes: 54 additions & 0 deletions stores/intervals/intervals.store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { create, useStore } from 'zustand'
import { devtools, persist } from 'zustand/middleware'

import { Interval, IntervalsState, IntervalsStore } from './intervals-store.types'

const initialState: IntervalsState = {
intervals: [],
}

const intervalsStore = create<
IntervalsStore,
[
['zustand/devtools', never],
['zustand/persist', IntervalsState],
]
>(
devtools(
persist(
(set) => ({
...initialState,

addInterval: (payload: Interval) => set(
(state) => {
return { intervals: [...state.intervals, payload] }
},
false,
{ type: 'intervals/addInterval', payload },
),

resetIntervals: () => set(
() => ({ ...initialState }),
false,
{ type: 'intervals/resetSetting' },
),
}),
{
name: 'intervals',
},
),
),
)

// bounded stored
export function useIntervalsStore (): IntervalsStore
export function useIntervalsStore<T> (
selector: (state: IntervalsStore) => T,
equals?: (a: T, b: T) => boolean,
): T
export function useIntervalsStore<T> (
selector?: (state: IntervalsStore) => T,
equals?: (a: T, b: T) => boolean,
) {
return useStore(intervalsStore, selector!, equals)
}

0 comments on commit 621e1e5

Please sign in to comment.