-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathDangerousConfirmationPrompt.tsx
More file actions
159 lines (144 loc) · 4.86 KB
/
DangerousConfirmationPrompt.tsx
File metadata and controls
159 lines (144 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {TextInput} from './TextInput.js'
import {InlineToken, TokenItem, TokenizedText} from './TokenizedText.js'
import {InfoTable, InfoTableProps} from './Prompts/InfoTable.js'
import {handleCtrlC} from '../../ui.js'
import useLayout from '../hooks/use-layout.js'
import {messageWithPunctuation} from '../utilities.js'
import {AbortSignal} from '../../../../public/node/abort.js'
import useAbortSignal from '../hooks/use-abort-signal.js'
import useDeferredUnmount from '../hooks/use-deferred-unmount.js'
import usePrompt, {PromptState} from '../hooks/use-prompt.js'
import React, {FunctionComponent, useCallback, useEffect, useState} from 'react'
import {Box, useInput, Text} from 'ink'
import figures from 'figures'
export interface DangerousConfirmationPromptProps {
message: string
confirmation: string
infoTable?: InfoTableProps['table']
onSubmit: (value: boolean) => void
abortSignal?: AbortSignal
}
const DangerousConfirmationPrompt: FunctionComponent<DangerousConfirmationPromptProps> = ({
message,
confirmation,
infoTable,
onSubmit,
abortSignal,
}) => {
const validateAnswer = useCallback(
(value: string): TokenItem<InlineToken> | undefined => {
return value === confirmation ? undefined : ['Value must be exactly', {userInput: confirmation}]
},
[confirmation],
)
const {oneThird, twoThirds} = useLayout()
const {promptState, setPromptState, answer, setAnswer} = usePrompt<string>({
initialAnswer: '',
})
const unmountInk = useDeferredUnmount()
const [error, setError] = useState<TokenItem<InlineToken> | undefined>(undefined)
const color = promptState === PromptState.Error ? 'red' : 'cyan'
const underline = new Array(oneThird - 3).fill('▔')
const {isAborted} = useAbortSignal(abortSignal)
useInput((input, key) => {
handleCtrlC(input, key)
if (key.escape) {
setPromptState(PromptState.Cancelled)
setError(undefined)
}
if (key.return) {
const error = validateAnswer(answer)
if (error) {
setPromptState(PromptState.Error)
setError(error)
} else {
setPromptState(PromptState.Submitted)
}
}
})
useEffect(() => {
if (promptState === PromptState.Submitted) {
onSubmit(true)
unmountInk()
} else if (promptState === PromptState.Cancelled) {
onSubmit(false)
unmountInk()
}
}, [onSubmit, promptState, unmountInk])
const completed = promptState === PromptState.Submitted || promptState === PromptState.Cancelled
return isAborted ? null : (
<Box flexDirection="column" marginBottom={1} width={twoThirds}>
<Box>
<Box marginRight={2}>
<Text>?</Text>
</Box>
<TokenizedText item={messageWithPunctuation(message)} />
</Box>
{completed ? (
<CompletedPrompt {...{cancelled: promptState === PromptState.Cancelled}} />
) : (
<>
<Box flexDirection="column" gap={1} marginTop={1} marginLeft={3}>
{infoTable ? (
<Box
paddingLeft={2}
borderStyle="bold"
borderLeft
borderRight={false}
borderTop={false}
borderBottom={false}
flexDirection="column"
gap={1}
>
<InfoTable table={infoTable} />
</Box>
) : null}
<Box>
<TokenizedText item={['Type', {userInput: confirmation}, 'to confirm, or press Escape to cancel.']} />
</Box>
</Box>
<Box flexDirection="column" width={oneThird}>
<Box>
<Box marginRight={2}>
<Text color={color}>{`>`}</Text>
</Box>
<Box flexGrow={1}>
<TextInput
value={answer}
onChange={(answer) => {
setAnswer(answer)
setPromptState(PromptState.Idle)
}}
defaultValue=""
color={color}
/>
</Box>
</Box>
<Box marginLeft={3}>
<Text color={color}>{underline}</Text>
</Box>
{promptState === PromptState.Error && error ? (
<Box marginLeft={3}>
<Text color={color}>
<TokenizedText item={error} />
</Text>
</Box>
) : null}
</Box>
</>
)}
</Box>
)
}
interface CompletedPromptProps {
cancelled: boolean
}
const CompletedPrompt: FunctionComponent<CompletedPromptProps> = ({cancelled}) => (
<Box>
<Box width={3}>
{cancelled ? <Text color="red">{figures.cross}</Text> : <Text color="cyan">{figures.tick}</Text>}
</Box>
<Box flexGrow={1}>{cancelled ? <Text color="red">Cancelled</Text> : <Text color="cyan">Confirmed</Text>}</Box>
</Box>
)
export {DangerousConfirmationPrompt}