Skip to content

Commit 9f057e1

Browse files
committed
Refactor the commit timer logic into a dedicated use_commit_timer hook
1 parent d39c026 commit 9f057e1

3 files changed

Lines changed: 51 additions & 33 deletions

File tree

‎packages/ui/src/components/editor/panel/modals/CommitMessageModal/CommitMessageModal.tsx‎

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { useState, useEffect, useCallback, useRef } from 'react'
1+
import { useState, useCallback } from 'react'
22
import styles from './CommitMessageModal.module.scss'
33
import { Button } from '../../Button'
44
import { Modal } from '../Modal'
55
import TextareaAutosize from 'react-textarea-autosize'
6+
import { use_commit_timer } from './hooks/use-commit-timer'
67

78
type Props = {
89
commit_message: string
@@ -12,46 +13,20 @@ type Props = {
1213

1314
export const CommitMessageModal: React.FC<Props> = (props) => {
1415
const [message, set_message] = useState(props.commit_message)
15-
const [countdown, setCountdown] = useState(5)
16-
const [is_timer_active, set_is_timer_active] = useState(true)
17-
const interval_ref = useRef<NodeJS.Timeout>()
1816

1917
const handle_accept = useCallback(() => {
2018
if (message.trim()) {
2119
props.on_accept(message)
2220
}
2321
}, [message, props.on_accept])
2422

25-
useEffect(() => {
26-
if (!is_timer_active) {
27-
return
28-
}
29-
interval_ref.current = setInterval(() => {
30-
setCountdown((prev) => {
31-
if (prev <= 1) {
32-
if (interval_ref.current) clearInterval(interval_ref.current)
33-
return 0
34-
}
35-
return prev - 1
36-
})
37-
}, 1000)
38-
return () => {
39-
if (interval_ref.current) clearInterval(interval_ref.current)
40-
}
41-
}, [is_timer_active])
42-
43-
useEffect(() => {
44-
if (countdown == 0 && is_timer_active) {
45-
handle_accept()
46-
}
47-
}, [countdown, is_timer_active, handle_accept])
48-
49-
const stop_timer = () => {
50-
set_is_timer_active(false)
51-
}
23+
const commit_timer_hook = use_commit_timer({
24+
on_accept: handle_accept,
25+
initial_countdown: 5
26+
})
5227

5328
return (
54-
<div onMouseDown={stop_timer}>
29+
<div onMouseDown={commit_timer_hook.stop_timer}>
5530
<Modal
5631
title="Commit changes"
5732
content_slot={
@@ -75,7 +50,9 @@ export const CommitMessageModal: React.FC<Props> = (props) => {
7550
</Button>
7651
<Button on_click={handle_accept} disabled={!message.trim()}>
7752
Commit
78-
{is_timer_active && countdown > 0 && ` (${countdown})`}
53+
{commit_timer_hook.is_timer_active &&
54+
commit_timer_hook.countdown > 0 &&
55+
` (${commit_timer_hook.countdown})`}
7956
</Button>
8057
</>
8158
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './use-commit-timer'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { useState, useEffect, useRef, useCallback } from 'react'
2+
3+
export const use_commit_timer = (params: {
4+
on_accept: () => void
5+
initial_countdown: number
6+
}) => {
7+
const [countdown, setCountdown] = useState(params.initial_countdown)
8+
const [is_timer_active, set_is_timer_active] = useState(true)
9+
const interval_ref = useRef<NodeJS.Timeout>()
10+
11+
useEffect(() => {
12+
if (!is_timer_active) {
13+
return
14+
}
15+
interval_ref.current = setInterval(() => {
16+
setCountdown((prev) => {
17+
if (prev <= 1) {
18+
if (interval_ref.current) clearInterval(interval_ref.current)
19+
return 0
20+
}
21+
return prev - 1
22+
})
23+
}, 1000)
24+
return () => {
25+
if (interval_ref.current) clearInterval(interval_ref.current)
26+
}
27+
}, [is_timer_active])
28+
29+
useEffect(() => {
30+
if (countdown == 0 && is_timer_active) {
31+
params.on_accept()
32+
}
33+
}, [countdown, is_timer_active, params.on_accept])
34+
35+
const stop_timer = useCallback(() => {
36+
set_is_timer_active(false)
37+
}, [])
38+
39+
return { countdown, is_timer_active, stop_timer }
40+
}

0 commit comments

Comments
 (0)