-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathedit-git-bot-dialog.tsx
More file actions
181 lines (173 loc) · 5.78 KB
/
edit-git-bot-dialog.tsx
File metadata and controls
181 lines (173 loc) · 5.78 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { Input } from "@/components/ui/input"
import { Field, FieldContent, FieldLabel } from "@/components/ui/field"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { useState, useEffect } from "react"
import { apiRequest } from "@/utils/requestUtils"
import { toast } from "sonner"
import type { DomainGitBot } from "@/api/Api"
import { ConstsGitPlatform, ConstsHostStatus } from "@/api/Api"
import Icon from "@/components/common/Icon"
import { Badge } from "@/components/ui/badge"
import { useCommonData } from "../common-data"
import { getHostBadges } from "@/utils/common"
interface EditGitBotDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
bot: DomainGitBot | null
onSuccess: () => void
}
export function EditGitBotDialog({ open, onOpenChange, bot, onSuccess }: EditGitBotDialogProps) {
const [name, setName] = useState("")
const [token, setToken] = useState("")
const [selectedHostId, setSelectedHostId] = useState<string>("")
const [platform, setPlatform] = useState<ConstsGitPlatform>(ConstsGitPlatform.GitPlatformGitLab)
const [loading, setLoading] = useState(false)
const { hosts } = useCommonData()
useEffect(() => {
if (open && bot) {
setName(bot.name || "")
setToken(bot.token || "")
setPlatform(bot.platform || ConstsGitPlatform.GitPlatformGitLab)
// 设置宿主机
if (bot.host?.id) {
setSelectedHostId(bot.host.id)
} else {
setSelectedHostId("public_host")
}
}
}, [open, bot])
const handleSubmit = async () => {
if (!bot?.id) {
toast.error("机器人信息不完整")
return
}
setLoading(true)
await apiRequest('v1UsersGitBotsUpdate', {
id: bot.id,
host_id: selectedHostId,
platform: platform,
name: name || undefined,
token: token || undefined,
}, [], (resp) => {
if (resp.code === 0) {
toast.success("更新成功")
onOpenChange(false)
onSuccess()
} else {
toast.error("更新失败: " + resp.message)
}
})
setLoading(false)
}
const handleCancel = () => {
setName("")
setToken("")
setSelectedHostId("")
setPlatform(ConstsGitPlatform.GitPlatformGitLab)
onOpenChange(false)
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent>
<DialogHeader>
<DialogTitle>修改审查机器人</DialogTitle>
</DialogHeader>
<Field>
<FieldLabel>备注名称</FieldLabel>
<FieldContent>
<Input
placeholder="输入备注名称"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</FieldContent>
</Field>
<Field>
<FieldLabel>宿主机</FieldLabel>
<FieldContent>
<Select value={selectedHostId} onValueChange={setSelectedHostId}>
<SelectTrigger className="w-full">
<SelectValue placeholder="请选择宿主机" />
</SelectTrigger>
<SelectContent>
<SelectItem value={"public_host"}>
<div className="flex items-center gap-2">
<span>MonkeyCode</span>
<Badge variant="outline">平台内置</Badge>
</div>
</SelectItem>
{hosts.map((host) => {
return (
<SelectItem key={host.id} value={host.id!} disabled={host.status !== ConstsHostStatus.HostStatusOnline}>
<div className="flex items-center gap-2">
<span>{host.remark || `${host.name}-${host.external_ip}`}</span>
{getHostBadges(host)}
</div>
</SelectItem>
)
})}
</SelectContent>
</Select>
</FieldContent>
</Field>
<Field>
<FieldLabel>Git 平台类型</FieldLabel>
<FieldContent>
<Select value={platform} onValueChange={(value) => setPlatform(value as ConstsGitPlatform)}>
<SelectTrigger className="w-full">
<SelectValue placeholder="请选择" />
</SelectTrigger>
<SelectContent>
<SelectItem value={ConstsGitPlatform.GitPlatformGitLab}>
<Icon name="GitLab" />GitLab
</SelectItem>
<SelectItem value={ConstsGitPlatform.GitPlatformGithub}>
<Icon name="GitHub-Uncolor" />GitHub
</SelectItem>
<SelectItem value={ConstsGitPlatform.GitPlatformGitee}>
<Icon name="Gitee" />Gitee
</SelectItem>
<SelectItem value={ConstsGitPlatform.GitPlatformGitea}>
<Icon name="Gitea" />Gitea
</SelectItem>
</SelectContent>
</Select>
</FieldContent>
</Field>
<Field>
<FieldLabel>Access Token</FieldLabel>
<FieldContent>
<Input
type="password"
placeholder="留空则不修改"
value={token}
onChange={(e) => setToken(e.target.value)}
/>
</FieldContent>
</Field>
<DialogFooter>
<Button variant="outline" onClick={handleCancel} disabled={loading}>
取消
</Button>
<Button onClick={handleSubmit} disabled={loading}>
{loading ? "保存中..." : "保存"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}