Commit b45308de authored by Chunchi Che's avatar Chunchi Che

fix avatar

parent bc8e2fea
Pipeline #23658 passed with stages
in 12 minutes and 12 seconds
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
"profileUrl":"https://accounts.moecube.com/profiles", "profileUrl":"https://accounts.moecube.com/profiles",
"athleticWatchUrl":"wss://tiramisu.moecube.com:8923", "athleticWatchUrl":"wss://tiramisu.moecube.com:8923",
"entertainWatchUrl":"wss://tiramisu.moecube.com:7923", "entertainWatchUrl":"wss://tiramisu.moecube.com:7923",
"avatarApi":"https://sapi.moecube.com:444/avatar/avatar/{username}/120/1.png", "userApi":"https://sapi.moecube.com:444/accounts/users/{username}.json",
"streamInterval":20, "streamInterval":20,
"startDelay":1000, "startDelay":1000,
"ui":{ "ui":{
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
"profileUrl":"https://accounts.moecube.com/profiles", "profileUrl":"https://accounts.moecube.com/profiles",
"athleticWatchUrl":"wss://tiramisu.moecube.com:8923", "athleticWatchUrl":"wss://tiramisu.moecube.com:8923",
"entertainWatchUrl":"wss://tiramisu.moecube.com:7923", "entertainWatchUrl":"wss://tiramisu.moecube.com:7923",
"avatarApi":"https://sapi.moecube.com:444/avatar/avatar/{username}/120/1.png", "userApi":"https://sapi.moecube.com:444/accounts/users/{username}.json",
"streamInterval":20, "streamInterval":20,
"startDelay":1000, "startDelay":1000,
"ui":{ "ui":{
......
// Collection of APIs provided by MyCard // Collection of APIs provided by MyCard
export * from "./account"; export * from "./account";
export * from "./match"; export * from "./match";
export * from "./user";
import { useConfig } from "@/config";
const { userApi } = useConfig();
interface WrappedUserInfo {
user: UserInfo;
}
export interface UserInfo {
id: number;
username: string;
name: string;
email: string;
active: boolean;
admin: boolean;
avatar: string;
locale: string;
}
export async function getUserInfo(
username: string,
): Promise<UserInfo | undefined> {
const resp = await fetch(userApi.replace("{username}", username));
if (resp.ok) {
return ((await resp.json()) as WrappedUserInfo).user;
} else {
console.error(`get ${username} info error`);
return undefined;
}
}
...@@ -5,12 +5,13 @@ import React, { useState } from "react"; ...@@ -5,12 +5,13 @@ import React, { useState } from "react";
import useWebSocket, { ReadyState } from "react-use-websocket"; import useWebSocket, { ReadyState } from "react-use-websocket";
import { proxy, useSnapshot } from "valtio"; import { proxy, useSnapshot } from "valtio";
import { getUserInfo } from "@/api";
import { useConfig } from "@/config"; import { useConfig } from "@/config";
import { ScrollableArea } from "../Shared"; import { ScrollableArea } from "../Shared";
import styles from "./WatchContent.module.scss"; import styles from "./WatchContent.module.scss";
const { athleticWatchUrl, avatarApi } = useConfig(); const { athleticWatchUrl } = useConfig();
interface Info { interface Info {
event: "init" | "create" | "update" | "delete"; event: "init" | "create" | "update" | "delete";
...@@ -20,7 +21,7 @@ interface Info { ...@@ -20,7 +21,7 @@ interface Info {
interface Room { interface Room {
id?: string; id?: string;
title?: string; title?: string;
users?: { username: string; position: number }[]; users?: { username: string; position: number; avatar?: string }[];
options: Options; options: Options;
} }
...@@ -61,18 +62,34 @@ export const WatchContent: React.FC = () => { ...@@ -61,18 +62,34 @@ export const WatchContent: React.FC = () => {
case "init": { case "init": {
//@ts-ignore //@ts-ignore
const rooms: Room[] = info.data; const rooms: Room[] = info.data;
rooms.forEach(
(room) =>
room.users?.forEach(
async (user) =>
(user.avatar = (await getUserInfo(user.username))?.avatar),
),
);
setRooms(rooms); setRooms(rooms);
break; break;
} }
case "create": { case "create": {
//@ts-ignore //@ts-ignore
const room: Room = info.data; const room: Room = info.data;
room.users?.forEach(
async (user) =>
(user.avatar = (await getUserInfo(user.username))?.avatar),
);
setRooms((prev) => prev.concat(room)); setRooms((prev) => prev.concat(room));
break; break;
} }
case "update": { case "update": {
//@ts-ignore //@ts-ignore
const room: Room = info.data; const room: Room = info.data;
room.users?.forEach(
async (user) =>
(user.avatar = (await getUserInfo(user.username))?.avatar),
);
setRooms((prev) => { setRooms((prev) => {
const target = prev.find((item) => item.id === room.id); const target = prev.find((item) => item.id === room.id);
if (target) { if (target) {
...@@ -130,18 +147,8 @@ export const WatchContent: React.FC = () => { ...@@ -130,18 +147,8 @@ export const WatchContent: React.FC = () => {
onClick={() => (watchStore.watchID = room.id)} onClick={() => (watchStore.watchID = room.id)}
> >
<div className={styles.avatar}> <div className={styles.avatar}>
<Avatar <Avatar src={room.users?.at(0)?.avatar} />
src={avatarApi.replace( <Avatar src={room.users?.at(1)?.avatar} />
"{username}",
room.users?.at(0)?.username ?? "?",
)}
/>
<Avatar
src={avatarApi.replace(
"{username}",
room.users?.at(1)?.username ?? "?",
)}
/>
</div> </div>
<div className={styles.title}> <div className={styles.title}>
{`${room.users?.at(0)?.username} 与 ${room.users?.at(1) {`${room.users?.at(0)?.username} 与 ${room.users?.at(1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment