patching user displayName and profileImageUrl

This commit is contained in:
Zamitto 2024-06-18 12:02:16 -03:00
parent af69509c61
commit 59b2096d06
6 changed files with 33 additions and 10 deletions

View file

@ -24,6 +24,7 @@ import "./library/remove-game";
import "./library/remove-game-from-library"; import "./library/remove-game-from-library";
import "./misc/open-external"; import "./misc/open-external";
import "./misc/show-open-dialog"; import "./misc/show-open-dialog";
import "./misc/image-path-to-base-64";
import "./torrenting/cancel-game-download"; import "./torrenting/cancel-game-download";
import "./torrenting/pause-game-download"; import "./torrenting/pause-game-download";
import "./torrenting/resume-game-download"; import "./torrenting/resume-game-download";

View file

@ -0,0 +1,14 @@
import mime from "mime";
import { registerEvent } from "../register-event";
import fs from "node:fs";
const imagePathToBase64 = async (
_event: Electron.IpcMainInvokeEvent,
filePath: string
) => {
const buffer = fs.readFileSync(filePath);
const mimeType = mime.getType(filePath);
return `data:${mimeType};base64,${buffer.toString("base64")}`;
};
registerEvent("imagePathToBase64", imagePathToBase64);

View file

@ -4,12 +4,11 @@ import axios from "axios";
import fs from "node:fs"; import fs from "node:fs";
import mime from "mime"; import mime from "mime";
const patchUserProfile = (displayName: string, imageUrl?: string) => { const patchUserProfile = (displayName: string, profileImageUrl?: string) => {
return; if (profileImageUrl) {
if (imageUrl) {
return HydraApi.patch("/profile", { return HydraApi.patch("/profile", {
displayName, displayName,
imageUrl, profileImageUrl,
}); });
} else { } else {
return HydraApi.patch("/profile", { return HydraApi.patch("/profile", {

View file

@ -106,6 +106,8 @@ contextBridge.exposeInMainWorld("electron", {
getVersion: () => ipcRenderer.invoke("getVersion"), getVersion: () => ipcRenderer.invoke("getVersion"),
getDefaultDownloadsPath: () => ipcRenderer.invoke("getDefaultDownloadsPath"), getDefaultDownloadsPath: () => ipcRenderer.invoke("getDefaultDownloadsPath"),
openExternal: (src: string) => ipcRenderer.invoke("openExternal", src), openExternal: (src: string) => ipcRenderer.invoke("openExternal", src),
imagePathToBase64: (filePath: string) =>
ipcRenderer.invoke("imagePathToBase64", filePath),
showOpenDialog: (options: Electron.OpenDialogOptions) => showOpenDialog: (options: Electron.OpenDialogOptions) =>
ipcRenderer.invoke("showOpenDialog", options), ipcRenderer.invoke("showOpenDialog", options),
platform: process.platform, platform: process.platform,

View file

@ -96,6 +96,7 @@ declare global {
/* Misc */ /* Misc */
openExternal: (src: string) => Promise<void>; openExternal: (src: string) => Promise<void>;
imagePathToBase64: (filePath: string) => Promise<string>;
getVersion: () => Promise<string>; getVersion: () => Promise<string>;
ping: () => string; ping: () => string;
getDefaultDownloadsPath: () => Promise<string>; getDefaultDownloadsPath: () => Promise<string>;

View file

@ -18,22 +18,28 @@ export const UserEditProfileModal = ({
}: UserEditProfileModalProps) => { }: UserEditProfileModalProps) => {
const [displayName, setDisplayName] = useState(userProfile.displayName); const [displayName, setDisplayName] = useState(userProfile.displayName);
const [newImagePath, setNewImagePath] = useState<string | null>(null); const [newImagePath, setNewImagePath] = useState<string | null>(null);
const [newImageBase64, setNewImageBase64] = useState<string | null>(null);
const handleChangeProfileAvatar = async () => { const handleChangeProfileAvatar = async () => {
const { filePaths } = await window.electron.showOpenDialog({ const { filePaths } = await window.electron.showOpenDialog({
properties: ["openFile"], properties: ["openFile"],
filters: [ filters: [
{ {
name: "Profile avatar", name: "Profile image",
extensions: ["jpg", "png", "gif"], extensions: ["jpg", "png", "gif", "webp", "jpeg"],
}, },
], ],
}); });
const path = filePaths[0]; if (filePaths && filePaths.length > 0) {
console.log(path); const path = filePaths[0];
setNewImagePath(path); window.electron.imagePathToBase64(path).then((base64) => {
setNewImageBase64(base64);
});
setNewImagePath(path);
}
}; };
const handleSaveProfile = async () => { const handleSaveProfile = async () => {
@ -67,7 +73,7 @@ export const UserEditProfileModal = ({
<img <img
className={styles.profileAvatar} className={styles.profileAvatar}
alt={userProfile.displayName} alt={userProfile.displayName}
src={newImagePath ?? userProfile.profileImageUrl} src={newImageBase64 ?? userProfile.profileImageUrl}
/> />
) : ( ) : (
<PersonIcon size={72} /> <PersonIcon size={72} />