1 "use client"
2
3 import * as React from "react"
4
5 import { Input } from "@/components/input"
6 import { Label } from "@/components/label"
7 import { Prompt } from "@/components/prompt"
8
9 export interface RenderPromptProps {
10 open: boolean
11 title: string
12 description: string
13 variant?: "danger" | "confirmation"
14 verificationText?: string
15 verificationInstruction?: string
16 cancelText?: string
17 confirmText?: string
18 onConfirm: () => void
19 onCancel: () => void
20 }
21
22 export const RenderPrompt = ({
23 /**
24 * @ignore
25 */
26 open,
27 /**
28 * The variant of the prompt.
29 */
30 variant = "danger",
31 /**
32 * The prompt's title.
33 */
34 title,
35 /**
36 * The prompt's description.
37 */
38 description,
39 /**
40 * The text the user has to input in order to confirm the action.
41 */
42 verificationText,
43 /**
44 * The instruction for the verification text. Useful for passing a translated string to use instead of the default english one.
45 * Should be in the format: "Please type {val} to confirm:"
46 */
47 verificationInstruction = "Please type {val} to confirm:",
48 /**
49 * The label for the Cancel button.
50 */
51 cancelText = "Cancel",
52 /**
53 * Label for the Confirm button.
54 */
55 confirmText = "Confirm",
56 /**
57 * @ignore
58 */
59 onConfirm,
60 /**
61 * @ignore
62 */
63 onCancel,
64 }: RenderPromptProps) => {
65 const [userInput, setUserInput] = React.useState("")
66
67 const handleUserInput = (event: React.ChangeEvent<HTMLInputElement>) => {
68 setUserInput(event.target.value)
69 }
70
71 const validInput = React.useMemo(() => {
72 if (!verificationText) {
73 return true
74 }
75
76 return userInput === verificationText
77 }, [userInput, verificationText])
78
79 const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
80 event.preventDefault()
81
82 if (!verificationText) {
83 return
84 }
85
86 if (validInput) {
87 onConfirm()
88 }
89 }
90
91 React.useEffect(() => {
92 const handleEscape = (event: KeyboardEvent) => {
93 if (event.key === "Escape" && open) {
94 onCancel()
95 }
96 }
97
98 document.addEventListener("keydown", handleEscape)
99
100 return () => {
101 document.removeEventListener("keydown", handleEscape)
102 }
103 }, [onCancel, open])
104
105 let instructionParts = verificationInstruction.includes("{val}")
106 ? verificationInstruction.split("{val}")
107 : ["Please type", "to confirm:"]
108
109 if (instructionParts.length !== 2) {
110 instructionParts = ["Please type", "to confirm:"]
111 }
112
113 return (
114 <Prompt open={open} variant={variant}>
115 <Prompt.Content>
116 <form onSubmit={handleFormSubmit}>
117 <Prompt.Header>
118 <Prompt.Title>{title}</Prompt.Title>
119 <Prompt.Description>{description}</Prompt.Description>
120 </Prompt.Header>
121 {verificationText && (
122 <div className="border-ui-border-base mt-6 flex flex-col gap-y-4 border-y p-6">
123 <Label htmlFor="verificationText" className="text-ui-fg-subtle">
124 {instructionParts[0]}{" "}
125 <span className="text-ui-fg-base txt-compact-medium-plus">
126 {verificationText}
127 </span>{" "}
128 {instructionParts[1]}
129 </Label>
130 <Input
131 autoFocus
132 autoComplete="off"
133 id="verificationText"
134 placeholder={verificationText}
135 onChange={handleUserInput}
136 />
137 </div>
138 )}
139 <Prompt.Footer>
140 <Prompt.Cancel onClick={onCancel}>{cancelText}</Prompt.Cancel>
141 <Prompt.Action
142 disabled={!validInput}
143 type={verificationText ? "submit" : "button"}
144 onClick={verificationText ? undefined : onConfirm}
145 >
146 {confirmText}
147 </Prompt.Action>
148 </Prompt.Footer>
149 </form>
150 </Prompt.Content>
151 </Prompt>
152 )
153 }
154 RenderPrompt.displayName = "RenderPrompt"