From 4b513f91d8dc906ecf0a71b78a32ac6902b8fe88 Mon Sep 17 00:00:00 2001 From: KRTirtho Date: Tue, 30 Mar 2021 22:14:15 +0600 Subject: [PATCH] Fixes: - restart the app to complete login - no volume caching - cached image memory leak --- src/app.tsx | 59 +++++++++++++++------------ src/components/Login.tsx | 12 +----- src/components/Player.tsx | 14 +++---- src/components/shared/CachedImage.tsx | 28 +++++++++---- src/context/authContext.ts | 3 ++ 5 files changed, 64 insertions(+), 52 deletions(-) diff --git a/src/app.tsx b/src/app.tsx index c6517ed1..3130da7b 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -22,6 +22,7 @@ export enum LocalStorageKeys { credentials = "credentials", refresh_token = "refresh_token", preferences = "user-preferences", + volume = "volume" } export interface Credentials { @@ -47,6 +48,7 @@ const queryClient = new QueryClient({ const initialPreferences: PreferencesContextProperties = { playlistImages: false, }; +const initialCredentials: Credentials = { clientId: "", clientSecret: "" }; //* Application start function RootApp() { @@ -87,7 +89,13 @@ function RootApp() { const cachedCredentials = localStorage.getItem(LocalStorageKeys.credentials); // state const [isLoggedIn, setIsLoggedIn] = useState(false); - const [credentials, setCredentials] = useState({ clientId: "", clientSecret: "" }); + + const [credentials, setCredentials] = useState(() => { + if (cachedCredentials) { + return JSON.parse(cachedCredentials); + } + return initialCredentials; + }); const [preferences, setPreferences] = useState(() => { if (cachedPreferences) { return JSON.parse(cachedPreferences); @@ -98,28 +106,15 @@ function RootApp() { const [currentPlaylist, setCurrentPlaylist] = useState(); useEffect(() => { - setIsLoggedIn(!!cachedCredentials); + const parsedCredentials: Credentials = JSON.parse(cachedCredentials ?? "{}"); + setIsLoggedIn(!!(parsedCredentials.clientId && parsedCredentials.clientSecret)); }, []); - // just saves the preferences - useEffect(() => { - localStorage.setItem(LocalStorageKeys.preferences, JSON.stringify(preferences)); - }, [preferences]); - useEffect(() => { - const onWindowClose = () => { - if (audioPlayer.isRunning()) { - audioPlayer.stop().catch((e) => console.error("Failed to quit MPV player: ", e)); - } - }; - - windowRef.current?.addEventListener(WidgetEventTypes.Close, onWindowClose); - return () => { - windowRef.current?.removeEventListener(WidgetEventTypes.Close, onWindowClose); - }; - }); // for user code login useEffect(() => { - if (isLoggedIn && credentials && !localStorage.getItem(LocalStorageKeys.refresh_token)) { + // saving changed credentials to storage + localStorage.setItem(LocalStorageKeys.credentials, JSON.stringify(credentials)); + if (credentials.clientId && credentials.clientSecret && !localStorage.getItem(LocalStorageKeys.refresh_token)) { const app = express(); app.use(express.json()); @@ -130,6 +125,7 @@ function RootApp() { const { body: authRes } = await spotifyApi.authorizationCodeGrant(req.query.code); setAccess_token(authRes.access_token); localStorage.setItem(LocalStorageKeys.refresh_token, authRes.refresh_token); + setIsLoggedIn(true); return res.end(); } catch (error) { console.error("Failed to fullfil code grant flow: ", error); @@ -148,18 +144,31 @@ function RootApp() { server.close(() => console.log("Closed server")); }; } - }, [isLoggedIn, credentials]); + }, [credentials]); + // just saves the preferences useEffect(() => { - if (cachedCredentials) { - setCredentials(JSON.parse(cachedCredentials)); - } - }, [isLoggedIn]); + localStorage.setItem(LocalStorageKeys.preferences, JSON.stringify(preferences)); + }, [preferences]); + + // window event listeners + useEffect(() => { + const onWindowClose = () => { + if (audioPlayer.isRunning()) { + audioPlayer.stop().catch((e) => console.error("Failed to quit MPV player: ", e)); + } + }; + + windowRef.current?.addEventListener(WidgetEventTypes.Close, onWindowClose); + return () => { + windowRef.current?.removeEventListener(WidgetEventTypes.Close, onWindowClose); + }; + }); return ( - + diff --git a/src/components/Login.tsx b/src/components/Login.tsx index d7b04f00..268bba6e 100644 --- a/src/components/Login.tsx +++ b/src/components/Login.tsx @@ -1,10 +1,9 @@ import React, { useContext, useState } from "react"; import { LineEdit, Text, Button, View } from "@nodegui/react-nodegui"; import authContext from "../context/authContext"; -import { LocalStorageKeys, Credentials } from "../app"; function Login() { - const { setIsLoggedIn } = useContext(authContext); + const { setCredentials: setGlobalCredentials } = useContext(authContext); const [credentials, setCredentials] = useState({ clientId: "", clientSecret: "", @@ -54,14 +53,7 @@ function Login() {