From 08ff21a98735378c3ae97fed6870e65abaab0bfd Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Tue, 7 Nov 2023 11:26:04 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9D=8C=20AirDCPP=20code=20cleaned=20up=20fro?= =?UTF-8?q?m=20App.tsx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/components/App.tsx | 73 +------------------ .../AirDCPPSettings/AirDCPPSettingsForm.tsx | 19 +++-- src/client/store/index.ts | 46 ++++++++++-- 3 files changed, 55 insertions(+), 83 deletions(-) diff --git a/src/client/components/App.tsx b/src/client/components/App.tsx index b56bac3..6eae757 100644 --- a/src/client/components/App.tsx +++ b/src/client/components/App.tsx @@ -27,72 +27,8 @@ import { * 2. Handles errors in case the connection to AirDC++ is not established or terminated * @returns void */ -const AirDCPPSocketComponent = (): ReactElement => { - // const airDCPPConfiguration = useContext(AirDCPPSocketContext); - // const dispatch = useDispatch(); - // - // useEffect(() => { - // const initializeAirDCPPEventListeners = async () => { - // if ( - // !isUndefined(airDCPPConfiguration.airDCPPState) && - // !isEmpty(airDCPPConfiguration.airDCPPState.settings) && - // !isEmpty(airDCPPConfiguration.airDCPPState.socket) - // ) { - // await airDCPPConfiguration.airDCPPState.socket.addListener( - // "queue", - // "queue_bundle_added", - // async (data) => { - // console.log("JEMEN:", data); - // }, - // ); - // // download tick listener - // await airDCPPConfiguration.airDCPPState.socket.addListener( - // `queue`, - // "queue_bundle_tick", - // async (downloadProgressData) => { - // dispatch({ - // type: AIRDCPP_DOWNLOAD_PROGRESS_TICK, - // downloadProgressData, - // }); - // }, - // ); - // // download complete listener - // await airDCPPConfiguration.airDCPPState.socket.addListener( - // `queue`, - // "queue_bundle_status", - // async (bundleData) => { - // let count = 0; - // if (bundleData.status.completed && bundleData.status.downloaded) { - // // dispatch the action for raw import, with the metadata - // if (count < 1) { - // console.log(`[AirDCPP]: Download complete.`); - // dispatch({ - // type: LS_SINGLE_IMPORT, - // meta: { remote: true }, - // data: bundleData, - // }); - // count += 1; - // } - // } - // }, - // ); - // console.log( - // "[AirDCPP]: Listener registered - listening to queue bundle download ticks", - // ); - // console.log( - // "[AirDCPP]: Listener registered - listening to queue bundle changes", - // ); - // console.log( - // "[AirDCPP]: Listener registered - listening to transfer completion", - // ); - // } - // }; - // initializeAirDCPPEventListeners(); - // }, [airDCPPConfiguration]); - return <>; -}; + export const App = (): ReactElement => { - console.log("randitva"); // useEffect(() => { // // Check if there is a sessionId in localStorage // const sessionId = localStorage.getItem("sessionId"); @@ -110,12 +46,7 @@ export const App = (): ReactElement => { // }); // } // }, []); - return ( - <> - {/* The rest of your application */} - {/* */}; - - ); + return <>{/* The rest of your application */}; }; export default App; diff --git a/src/client/components/Settings/AirDCPPSettings/AirDCPPSettingsForm.tsx b/src/client/components/Settings/AirDCPPSettings/AirDCPPSettingsForm.tsx index 426acb3..4f97895 100644 --- a/src/client/components/Settings/AirDCPPSettings/AirDCPPSettingsForm.tsx +++ b/src/client/components/Settings/AirDCPPSettings/AirDCPPSettingsForm.tsx @@ -6,18 +6,21 @@ import { useStore } from "../../../store/index"; import { useShallow } from "zustand/react/shallow"; export const AirDCPPSettingsForm = (): ReactElement => { - // const airDCPPSettings = useContext(AirDCPPSocketContext); + // cherry-picking selectors for: + // 1. initial values for the form + // 2. If initial values are present, get the socket information to display const { airDCPPSocketConnected, - disconnectionInfo, - socketConnectionInformation, + airDCPPDisconnectionInfo, + airDCPPSocketConnectionInformation, airDCPPClientConfiguration, } = useStore( useShallow((state) => ({ airDCPPSocketConnected: state.airDCPPSocketConnected, - disconnectionInfo: state.disconnectionInfo, + airDCPPDisconnectionInfo: state.airDCPPDisconnectionInfo, airDCPPClientConfiguration: state.airDCPPClientConfiguration, - socketConnectionInformation: state.socketConnectionInformation, + airDCPPSocketConnectionInformation: + state.airDCPPSocketConnectionInformation, })), ); @@ -44,8 +47,10 @@ export const AirDCPPSettingsForm = (): ReactElement => { formHeading={"Configure AirDC++"} /> - {!isEmpty(socketConnectionInformation) ? ( - + {!isEmpty(airDCPPSocketConnectionInformation) ? ( + ) : null} {!isEmpty(airDCPPClientConfiguration) ? ( diff --git a/src/client/store/index.ts b/src/client/store/index.ts index 3d82c4d..36ab163 100644 --- a/src/client/store/index.ts +++ b/src/client/store/index.ts @@ -1,13 +1,14 @@ import { create } from "zustand"; -import { isEmpty, isUndefined } from "lodash"; +import { isEmpty } from "lodash"; import AirDCPPSocket from "../services/DcppSearchService"; import axios from "axios"; export const useStore = create((set, get) => ({ + // AirDC++ state airDCPPSocketConnected: false, - disconnectionInfo: {}, + airDCPPDisconnectionInfo: {}, airDCPPClientConfiguration: {}, - socketConnectionInformation: {}, + airDCPPSocketConnectionInformation: {}, setAirDCPPSocketConnectionStatus: () => set((value) => ({ airDCPPSocketConnected: value, @@ -15,6 +16,7 @@ export const useStore = create((set, get) => ({ getAirDCPPConnectionStatus: () => { const airDCPPSocketConnectionStatus = get().airDCPPSocketConnected; }, + // Socket.io state })); const { getState, setState } = useStore; @@ -44,11 +46,45 @@ const initializeAirDCPPSocket = async (configuration) => { airDCPPSocketConnected: false, }); }; + // AirDC++ Socket-related connection and post-connection // Attempt connection - const socketConnectionInformation = await initializedAirDCPPSocket.connect(); + const airDCPPSocketConnectionInformation = + await initializedAirDCPPSocket.connect(); setState({ - socketConnectionInformation, + airDCPPSocketConnectionInformation, }); + + // Set up event listeners + initializedAirDCPPSocket.addListener( + `queue`, + "queue_bundle_tick", + async (downloadProgressData) => { + console.log(downloadProgressData); + }, + ); + initializedAirDCPPSocket.addListener( + "queue", + "queue_bundle_added", + async (data) => { + console.log("JEMEN:", data); + }, + ); + + initializedAirDCPPSocket.addListener( + `queue`, + "queue_bundle_status", + async (bundleData) => { + let count = 0; + if (bundleData.status.completed && bundleData.status.downloaded) { + // dispatch the action for raw import, with the metadata + if (count < 1) { + console.log(`[AirDCPP]: Download complete.`); + + count += 1; + } + } + }, + ); }; // 1. get settings from mongo