From 4ebe543f6ad61e3b0b7460bbaa6175c62cd5df96 Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Thu, 26 Aug 2021 21:47:05 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Refactoring=20the=20airdcpp=20re?= =?UTF-8?q?ducer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/actions/airdcpp.actions.tsx | 102 +++++++++++++++------ src/client/components/AcquisitionPanel.tsx | 13 ++- src/client/components/ComicDetail.tsx | 12 ++- src/client/components/DownloadsPanel.tsx | 4 +- src/client/reducers/airdcpp.reducer.ts | 21 ++--- 5 files changed, 102 insertions(+), 50 deletions(-) diff --git a/src/client/actions/airdcpp.actions.tsx b/src/client/actions/airdcpp.actions.tsx index 1b24986..54e7e06 100644 --- a/src/client/actions/airdcpp.actions.tsx +++ b/src/client/actions/airdcpp.actions.tsx @@ -11,8 +11,9 @@ import { AIRDCPP_RESULT_DOWNLOAD_INITIATED, AIRDCPP_DOWNLOAD_PROGRESS_TICK, AIRDCPP_BUNDLES_FETCHED, + AIRDCPP_SEARCH_IN_PROGRESS, } from "../constants/action-types"; -import { each, isNil } from "lodash"; +import { each, isNil, isUndefined } from "lodash"; import axios from "axios"; interface SearchData { @@ -26,33 +27,82 @@ function sleep(ms: number): Promise { } export const search = (data: SearchData) => async (dispatch) => { - await SocketService.connect("admin", "password", true); - const instance: SearchInstance = await SocketService.post("search"); + try { + if (!SocketService.isConnected()) { + await SocketService.connect("admin", "password", true); + } + const instance: SearchInstance = await SocketService.post("search"); - SocketService.addListener( - `search/${instance.id}`, - "search_hub_searches_sent", - async (searchInfo) => { - dispatch({ - type: AIRDCPP_HUB_SEARCHES_SENT, - searchInfo, - instance, - }); - }, - ); + // We want to get notified about every new result in order to make the user experience better + await SocketService.addListener( + `search/${instance.id}`, + "search_result_added", + (groupedResult) => { + dispatch({ + type: AIRDCPP_SEARCH_RESULTS_RECEIVED, + groupedResult: groupedResult.result, + }); + // ...add the received result in the UI + // (it's probably a good idea to have some kind of throttling for the UI updates as there can be thousands of results) + }, + ); - await SocketService.post( - `search/${instance.id}/hub_search`, - data, - ); + // We also want to update the existing items in our list when new hits arrive for the previously listed files/directories + await SocketService.addListener( + `search/${instance.id}`, + "search_result_updated", + async (groupedResult) => { + console.log(groupedResult); + dispatch({ + type: AIRDCPP_SEARCH_RESULTS_RECEIVED, + results: groupedResult, + }); + // ...update properties of the existing result in the UI + }, + ); - await sleep(10000); - const results = await SocketService.get(`search/${instance.id}/results/0/25`); - dispatch({ - type: AIRDCPP_SEARCH_RESULTS_RECEIVED, - results, - }); - return results; + await SocketService.addListener( + `search/${instance.id}`, + "search_hub_searches_sent", + async (searchInfo) => { + await sleep(5000); + // The search can now be considered to be "complete" + + // Check the number of received results (in real use cases we should know that even without calling the API) + const currentInstance = await SocketService.get( + `search/${instance.id}`, + ); + if (currentInstance.result_count === 0) { + console.log("ASDASDASDASDD"); + // ...nothing was received, show an informative message to the user + } + + // If there's an "in progress" indicator in the UI, that could also be disabled here + dispatch({ + type: AIRDCPP_HUB_SEARCHES_SENT, + searchInfo, + instance, + }); + }, + ); + await SocketService.post( + `search/${instance.id}/hub_search`, + data, + ); + + // await sleep(10000); + // const results = await SocketService.get( + // `search/${instance.id}/results/0/25`, + // ); + + // dispatch({ + // type: AIRDCPP_SEARCH_RESULTS_RECEIVED, + // results, + // }); + } catch (error) { + console.log("ERO", error); + throw error; + } }; export const downloadAirDCPPItem = @@ -111,7 +161,7 @@ export const getDownloadProgress = if (!SocketService.isConnected()) { await SocketService.connect("admin", "password", true); } - SocketService.addListener( + await SocketService.addListener( `queue`, "queue_bundle_tick", async (downloadProgressData) => { diff --git a/src/client/components/AcquisitionPanel.tsx b/src/client/components/AcquisitionPanel.tsx index f04841f..f709ff8 100644 --- a/src/client/components/AcquisitionPanel.tsx +++ b/src/client/components/AcquisitionPanel.tsx @@ -16,7 +16,7 @@ export const AcquisitionPanel = ( const sanitizedVolumeName = volumeName.replace(/[^a-zA-Z0-9 ]/g, ""); const issueName = props.comicBookMetadata.sourcedMetadata.comicvine.name; const airDCPPSearchResults = useSelector( - (state: RootState) => state.airdcpp.results, + (state: RootState) => { console.log(state); return state.airdcpp.results;} ); const isAirDCPPSearchInProgress = useSelector( (state: RootState) => state.airdcpp.isAirDCPPSearchInProgress, @@ -35,13 +35,15 @@ export const AcquisitionPanel = ( }, [dispatch], ); + const dcppQuery = { query: { - pattern: `${sanitizedVolumeName}`, + pattern: `${sanitizedVolumeName.replace(/#/g, "")}`, + // pattern: "Templier T2.cbr", extensions: ["cbz", "cbr"], }, hub_urls: ["nmdcs://piter.feardc.net:411"], - priority: 1, + priority: 5, }; const downloadDCPPResult = useCallback( @@ -102,7 +104,8 @@ export const AcquisitionPanel = ( {/* AirDC++ results */}
- {!isNil(airDCPPSearchResults) && ( + ASDASD {JSON.stringify(airDCPPSearchResults)} + {/* {!isNil(airDCPPSearchResults) && ( @@ -169,7 +172,7 @@ export const AcquisitionPanel = ( })}
- )} + )} */}
); diff --git a/src/client/components/ComicDetail.tsx b/src/client/components/ComicDetail.tsx index 5c2bd15..10a3b5d 100644 --- a/src/client/components/ComicDetail.tsx +++ b/src/client/components/ComicDetail.tsx @@ -85,7 +85,7 @@ export const ComicDetail = ({}: ComicDetailProps): ReactElement => { name: "Volume Information", icon: , content: isComicBookMetadataAvailable ? ( - <> +
@@ -137,26 +137,28 @@ export const ComicDetail = ({}: ComicDetailProps): ReactElement => { )} >
- +
) : null, }, { id: 2, icon: , name: "Other Metadata", - content:
bastard
, + content:
bastard
, }, { id: 3, icon: , name: "Acquisition", - content: , + content: ( + + ), }, { id: 4, icon: , name: "Downloads", - content: , + content: , }, ]; const MetadataTabGroup = () => { diff --git a/src/client/components/DownloadsPanel.tsx b/src/client/components/DownloadsPanel.tsx index 102a483..f0ad53b 100644 --- a/src/client/components/DownloadsPanel.tsx +++ b/src/client/components/DownloadsPanel.tsx @@ -60,11 +60,11 @@ export const DownloadsPanel = ( {!isNil(props.data) && props.data && map(props.data, (bundle) => ( - <> +
{bundle.name}
{bundle.target}
{bundle.size}
- +
))} diff --git a/src/client/reducers/airdcpp.reducer.ts b/src/client/reducers/airdcpp.reducer.ts index 229436e..8ce47b2 100644 --- a/src/client/reducers/airdcpp.reducer.ts +++ b/src/client/reducers/airdcpp.reducer.ts @@ -10,32 +10,30 @@ import { LOCATION_CHANGE } from "connected-react-router"; const initialState = { isAirDCPPSearchInProgress: false, - searchStatus: "", searchInfo: null, searchInstance: null, downloadResult: null, bundleDBImportResult: null, + searchResults: [], }; function airdcppReducer(state = initialState, action) { switch (action.type) { + case AIRDCPP_SEARCH_RESULTS_RECEIVED: + console.log("mad", state.searchResults); + return { + ...state, + isAirDCPPSearchInProgress: true, + searchResults: [...state.searchResults, action.groupedResult], + }; case AIRDCPP_SEARCH_IN_PROGRESS: return { ...state, isAirDCPPSearchInProgress: true, }; - - case AIRDCPP_SEARCH_RESULTS_RECEIVED: - return { - ...state, - isAirDCPPSearchInProgress: false, - searchStatus: "Search complete", - results: action.results, - }; case AIRDCPP_HUB_SEARCHES_SENT: return { ...state, - searchStatus: "Hub searches sent", isAirDCPPSearchInProgress: true, searchInfo: action.searchInfo, searchInstance: action.instance, @@ -52,7 +50,6 @@ function airdcppReducer(state = initialState, action) { downloadProgressData: action.downloadProgressData, }; case AIRDCPP_BUNDLES_FETCHED: - console.log(action) return { ...state, bundles: action.bundles, @@ -63,7 +60,7 @@ function airdcppReducer(state = initialState, action) { }; default: - return state; + return { ...state }; } }