Skip to content

Commit eb044d4

Browse files
committed
feat: 修改用户名,简化代码
1 parent 7d672fa commit eb044d4

File tree

15 files changed

+395
-447
lines changed

15 files changed

+395
-447
lines changed

src/api/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useMainStore } from "@/hooks/useMainStore";
2+
13
interface CommonResponse<T> {
24
success: boolean;
35
error: string;
@@ -48,7 +50,7 @@ export async function fetcher<T>(method: FetcherMethod, path: string, data?: any
4850
}
4951

5052
// auto refresh token
51-
if (!lastestRefreshTokenAt || Date.now() - lastestRefreshTokenAt > 1000 * 60 * 60) {
53+
if (document.cookie && (!lastestRefreshTokenAt || Date.now() - lastestRefreshTokenAt > 1000 * 60 * 60)) {
5254
lastestRefreshTokenAt = Date.now();
5355
fetch("/api/v1/refresh-token")
5456
}

src/components/install-commands.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
} from "@/components/ui/dropdown-menu"
77
import { Button, ButtonProps } from "@/components/ui/button"
88
import { forwardRef, useState } from "react"
9-
import { useConfig } from "@/hooks/useConfig"
10-
import { ConfigEssential } from "@/types"
9+
import useSettings from "@/hooks/useSetting"
10+
import { ModelConfig } from "@/types"
1111
import { Check, Clipboard } from "lucide-react"
1212
import { toast } from "sonner"
1313

@@ -21,15 +21,15 @@ enum OSTypes {
2121

2222
export const InstallCommandsMenu = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
2323
const [copy, setCopy] = useState(false);
24-
const { config } = useConfig();
24+
const settings = useSettings();
2525
const { t } = useTranslation();
2626

2727
const switchState = async (type: number) => {
2828
if (!copy) {
2929
try {
3030
setCopy(true);
31-
if (config)
32-
await navigator.clipboard.writeText(generateCommand(type, config));
31+
if (settings)
32+
await navigator.clipboard.writeText(generateCommand(type, settings));
3333
} catch (e) {
3434
console.error(e);
3535
toast(t("Error"), {
@@ -60,7 +60,7 @@ export const InstallCommandsMenu = forwardRef<HTMLButtonElement, ButtonProps>((p
6060
);
6161
})
6262

63-
const generateCommand = (type: number, { agent_secret_key, install_host, listen_port, tls }: ConfigEssential) => {
63+
const generateCommand = (type: number, { agent_secret_key, install_host, listen_port, tls }: ModelConfig) => {
6464
if (!install_host)
6565
throw new Error("You have not specify the installed host.");
6666

src/components/profile.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ import { useTranslation } from "react-i18next";
3232
const profileFormSchema = z.object({
3333
original_password: z.string().min(5).max(72),
3434
new_password: z.string().min(8).max(72),
35+
new_username: z.string().min(1).max(32),
3536
});
3637

3738
export const ProfileCard = ({ className }: { className: string }) => {
3839
const { t } = useTranslation();
40+
const { profile, setProfile } = useMainStore();
41+
3942
const form = useForm<z.infer<typeof profileFormSchema>>({
4043
resolver: zodResolver(profileFormSchema),
4144
defaultValues: {
4245
original_password: '',
4346
new_password: '',
47+
new_username: profile?.username,
4448
},
4549
resetOptions: {
4650
keepDefaultValues: false,
4751
}
4852
})
4953

50-
const { setProfile } = useMainStore();
5154
const [open, setOpen] = useState(false);
5255

5356
const onSubmit = async (values: z.infer<typeof profileFormSchema>) => {
@@ -69,18 +72,33 @@ export const ProfileCard = ({ className }: { className: string }) => {
6972
<Dialog open={open} onOpenChange={setOpen}>
7073
<DialogTrigger asChild>
7174
<Button variant="outline" className={className}>
72-
{t("UpdatePassword")}
75+
{t("UpdateProfile")}
7376
</Button>
7477
</DialogTrigger>
7578
<DialogContent className="sm:max-w-xl">
7679
<ScrollArea className="max-h-[calc(100dvh-5rem)] p-3">
7780
<div className="items-center mx-1">
7881
<DialogHeader>
79-
<DialogTitle>{t("UpdatePassword")}</DialogTitle>
82+
<DialogTitle>{t("UpdateProfile")}</DialogTitle>
8083
<DialogDescription />
8184
</DialogHeader>
8285
<Form {...form}>
8386
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2">
87+
<FormField
88+
control={form.control}
89+
name="new_username"
90+
render={({ field }) => (
91+
<FormItem>
92+
<FormLabel>{t("NewUsername")}</FormLabel>
93+
<FormControl>
94+
<Input
95+
{...field}
96+
/>
97+
</FormControl>
98+
<FormMessage />
99+
</FormItem>
100+
)}
101+
/>
84102
<FormField
85103
control={form.control}
86104
name="original_password"

src/hooks/useConfig.tsx

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/hooks/useConfigStore.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/hooks/useSetting.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { swrFetcher } from "@/api/api";
2+
import { ModelConfig } from "@/types";
3+
import useSWR from "swr";
4+
5+
export default function useSetting() {
6+
const { data } = useSWR<ModelConfig>(
7+
"/api/v1/setting",
8+
swrFetcher
9+
);
10+
return data;
11+
}

src/locales/en/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
"Refresh": "Refresh",
104104
"CopyPath": "copy path",
105105
"Goto": "Go to",
106-
"UpdatePassword": "change password",
106+
"UpdateProfile": "Update profile",
107+
"NewUsername": "New username",
107108
"OriginalPassword": "original password",
108109
"NewPassword": "New Password",
109110
"EditDDNS": "Edit DDNS",

src/locales/it/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
"Refresh": "aggiornare",
105105
"CopyPath": "percorso di copia",
106106
"Goto": "Vai a",
107-
"UpdatePassword": "cambiare la password",
107+
"UpdateProfile": "Aggiorna il profilo",
108+
"NewUsername": "Nuovo nome utente",
108109
"OriginalPassword": "password originale",
109110
"NewPassword": "Nuova parola d'ordine",
110111
"EditDDNS": "Modifica DDNS",

src/locales/zh-CN/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
"Refresh": "刷新",
105105
"CopyPath": "复制路径",
106106
"Goto": "前往",
107-
"UpdatePassword": "更改密码",
107+
"UpdateProfile": "更新个人资料",
108+
"NewUsername": "新用户名",
108109
"OriginalPassword": "原始密码",
109110
"NewPassword": "新密码",
110111
"EditDDNS": "编辑DDNS",

src/locales/zh-TW/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
"Refresh": "刷新",
105105
"CopyPath": "複製路徑",
106106
"Goto": "前往",
107-
"UpdatePassword": "更改密碼",
107+
"UpdateProfile": "更新個人資料",
108+
"NewUsername": "新用戶名",
108109
"OriginalPassword": "原始密碼",
109110
"NewPassword": "新密碼",
110111
"EditDDNS": "編輯DDNS",

0 commit comments

Comments
 (0)