1- import { useState , useEffect , useCallback , useRef } from 'react'
1+ import { useState , useCallback } from 'react'
22import styles from './CommitMessageModal.module.scss'
33import { Button } from '../../Button'
44import { Modal } from '../Modal'
55import TextareaAutosize from 'react-textarea-autosize'
6+ import { use_commit_timer } from './hooks/use-commit-timer'
67
78type Props = {
89 commit_message : string
@@ -12,46 +13,20 @@ type Props = {
1213
1314export 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 }
0 commit comments