Sync custom icons from server

This commit is contained in:
Luo Tim 2024-08-19 00:24:37 +08:00
parent e7dea52c58
commit cb42bb0d42
3 changed files with 46 additions and 13 deletions

View File

@ -7,6 +7,7 @@ class APIUtil {
private accessKey: string | undefined; private accessKey: string | undefined;
private webappUrl: string | undefined; private webappUrl: string | undefined;
private currentMessageId: string | undefined; private currentMessageId: string | undefined;
private devchatAvatarUrl: string | undefined;
constructor() { constructor() {
@ -19,6 +20,20 @@ class APIUtil {
} }
return APIUtil.instance; return APIUtil.instance;
} }
config(baseUrl: string, accessKey: string) {
this.baseUrl = baseUrl;
this.accessKey = accessKey;
this.fetchWebappUrl().then(url => {
this.webappUrl = url;
this.fetchDevchatAvatarUrl().then(url => {
this.devchatAvatarUrl = url;
})
}).catch(err => {
console.error(err);
})
}
async fetchWebappUrl() { async fetchWebappUrl() {
try { try {
const res = await axios.get( const res = await axios.get(
@ -48,16 +63,6 @@ class APIUtil {
} }
} }
config(baseUrl: string, accessKey: string) {
this.baseUrl = baseUrl;
this.accessKey = accessKey;
this.fetchWebappUrl().then(url => {
this.webappUrl = url;
}).catch(err => {
console.error(err);
})
}
async createMessage(message: object) { async createMessage(message: object) {
this.currentMessageId = `msg-${uuidv4()}`; this.currentMessageId = `msg-${uuidv4()}`;
try { try {
@ -106,6 +111,25 @@ class APIUtil {
return null; return null;
} }
} }
async fetchDevchatAvatarUrl() {
try {
if (!this.webappUrl) this.webappUrl = await this.fetchWebappUrl();
const res = await axios.get(
`${this.webappUrl}/api/v1/plugin/icons/filename/gui`,
{headers: { Authorization: `Bearer ${this.accessKey}` }}
)
return `${this.webappUrl}/api/v1/plugin/icons/${res?.data?.filename}`
} catch(err) {
console.error(err);
return "unknown";
}
}
async getDevchatAvatarUrl() {
if (!this.devchatAvatarUrl) this.devchatAvatarUrl = await this.fetchDevchatAvatarUrl();
return this.devchatAvatarUrl;
}
} }

View File

@ -15,6 +15,7 @@ import SvgAvatarDevChat from "../MessageAvatar/avatar_devchat.svg";
import messageUtil from "@/util/MessageUtil"; import messageUtil from "@/util/MessageUtil";
import { useRouter } from "@/views/router"; import { useRouter } from "@/views/router";
import { useMst } from "@/views/stores/RootStore"; import { useMst } from "@/views/stores/RootStore";
import APIUtil from "@/util/APIUtil";
const useStyles = createStyles((theme) => ({ const useStyles = createStyles((theme) => ({
logoName: { logoName: {
@ -27,6 +28,7 @@ export default function Head() {
const { classes } = useStyles(); const { classes } = useStyles();
const { i18n } = useTranslation(); const { i18n } = useTranslation();
const { config } = useMst(); const { config } = useMst();
const [devchatAvatarSrc, setDevchatAvatarSrc] = React.useState("unknown")
useEffect(() => { useEffect(() => {
const lang = config.getLanguage(); const lang = config.getLanguage();
@ -35,6 +37,7 @@ export default function Head() {
} else { } else {
i18n.changeLanguage("zh"); i18n.changeLanguage("zh");
} }
APIUtil.getDevchatAvatarUrl().then(url => setDevchatAvatarSrc(url))
}, []); }, []);
const openSetting = () => { const openSetting = () => {
@ -72,7 +75,7 @@ export default function Head() {
height: 40, height: 40,
}} }}
> >
<Avatar color="indigo" size={25} radius="xl" src={SvgAvatarDevChat} /> <Avatar color="indigo" size={25} radius="xl" src={devchatAvatarSrc} onError={()=>setDevchatAvatarSrc(SvgAvatarDevChat)} />
<Text weight="bold" className={classes.logoName}> <Text weight="bold" className={classes.logoName}>
DevChat DevChat
</Text> </Text>

View File

@ -1,4 +1,4 @@
import React from "react"; import React, { useEffect } from "react";
import { import {
Text, Text,
Flex, Flex,
@ -23,6 +23,7 @@ import { useTranslation } from "react-i18next";
import { useMst } from "@/views/stores/RootStore"; import { useMst } from "@/views/stores/RootStore";
import { IMessage } from "@/views/stores/ChatStore"; import { IMessage } from "@/views/stores/ChatStore";
import { IChatContext } from "@/views/stores/InputStore"; import { IChatContext } from "@/views/stores/InputStore";
import APIUtil from "@/util/APIUtil";
interface IProps { interface IProps {
item?: IMessage; item?: IMessage;
@ -46,6 +47,11 @@ const MessageAvatar = observer((props: IProps) => {
const { input, chat } = useMst(); const { input, chat } = useMst();
const [done, setDone] = React.useState(false); const [done, setDone] = React.useState(false);
const { t } = useTranslation(); const { t } = useTranslation();
const [devchatAvatarSrc, setDevchatAvatarSrc] = React.useState("")
useEffect(() => {
APIUtil.getDevchatAvatarUrl().then(url => setDevchatAvatarSrc(url))
}, [])
return ( return (
<Flex <Flex
m="10px 0 10px 0" m="10px 0 10px 0"
@ -56,7 +62,7 @@ const MessageAvatar = observer((props: IProps) => {
wrap="wrap" wrap="wrap"
> >
{avatarType === "bot" ? ( {avatarType === "bot" ? (
<Avatar color="indigo" size={25} radius="xl" src={SvgAvatarDevChat} /> <Avatar color="indigo" size={25} radius="xl" src={devchatAvatarSrc} onError={()=>setDevchatAvatarSrc(SvgAvatarDevChat)} />
) : ( ) : (
<Avatar color="cyan" size={25} radius="xl" src={SvgAvatarUser} /> <Avatar color="cyan" size={25} radius="xl" src={SvgAvatarUser} />
)} )}