-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecretModal.tsx
More file actions
118 lines (110 loc) · 5.24 KB
/
SecretModal.tsx
File metadata and controls
118 lines (110 loc) · 5.24 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
import type {Secret} from "../../util/secret.ts";
import {useEffect, useState} from "react";
import {closeModal} from "../../util/modal.ts";
import {Eye, EyeSlash} from "../../icons/Eye.tsx";
type SecretModalProps = {
handleSecret: (s: Secret) => Promise<void>
existingSecret?: Secret
}
export const SecretModal = (props: SecretModalProps) => {
const [name, setName] = useState(props.existingSecret?.key ?? "")
const [secret, setSecret] = useState(props.existingSecret?.value ?? "")
const [url, setUrl] = useState(props.existingSecret?.url ?? "")
const [tags, setTags] = useState<string[]>(props.existingSecret?.tags ?? [])
const [tag, setTag] = useState("")
const [passwordVisible, setPasswordVisible] = useState(false)
useEffect(() => {
setName(props.existingSecret?.key ?? "")
setSecret(props.existingSecret?.value ?? "")
setUrl(props.existingSecret?.url ?? "")
setTags(props.existingSecret?.tags ?? [])
}, [props.existingSecret]);
async function onSubmit() {
if (!checkInput()) {
return
}
const s: Secret = {
id: props.existingSecret?.id,
key: name,
value: secret,
url: url,
tags: tags,
}
await props.handleSecret(s)
closeModal()
}
function checkInput(): boolean {
return name.length > 0 && secret.length > 0
}
function removeTag(tag: string): () => void {
const tagFilter = (t: string) => t !== tag;
return () => setTags(prevState => prevState.filter(tagFilter));
}
function togglePassword() {
const isPassword = document.getElementById("input-password")?.getAttribute("type") == "password"
setPasswordVisible(isPassword)
}
return <dialog id="add_secret_modal" className="modal">
<div className="modal-box">
<form>
<fieldset className="fieldset">
<legend className="fieldset-legend">Add Secret</legend>
<div className="flex flex-col gap-4">
<label className="input w-full">
Name
<input type="text" placeholder="MyNewSecret" value={name}
onChange={(e) => setName(e.target.value)}
className="grow validator" minLength={1} required
title="Can not be empty"/>
</label>
<label className="input w-full">
Secret
<input id="input-password" type={passwordVisible ? "text" : "password"} placeholder="*****"
value={secret}
onChange={(e) => setSecret(e.target.value)}
className="grow validator" minLength={1} required
title="Can not be empty"/>
<button type="button" onClick={togglePassword} className="btn btn-ghost btn-xs">
{passwordVisible ? <Eye/> : <EyeSlash/>}
</button>
</label>
<label className="input w-full">
URL
<input type="text" placeholder="https://example.com" value={url}
onChange={(e) => setUrl(e.target.value)}
className="grow"/>
<span className="badge badge-neutral badge-xs">Optional</span>
</label>
<label className="input w-full">
Add Tag
<input type="text" placeholder="example" value={tag}
onChange={(e) => setTag(e.target.value)}
onKeyDown={(e) => {
if (e.code == "Space" || e.key == " ") {
setTags([...tags, tag])
setTag("")
}
}}
disabled={tags.length >= 3} className="grow"/>
<span className="badge badge-neutral badge-xs"><4</span>
<kbd className="kbd kbd-sm">␣</kbd>
</label>
<div className="flex flex-col gap-2">
{tags.map((tag) => (
<button key={tag} className="badge badge-neutral btn" onClick={removeTag(tag)}>
<p className="truncate max-w-56">{tag}</p>
</button>
))}
</div>
<div className="modal-action">
<button className="btn btn-primary" onClick={onSubmit}>Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
<form method="dialog" className="modal-backdrop">
<button>close</button>
</form>
</dialog>
}