Skip to content

Commit

Permalink
ECOPROJECT-2540: List of reports are constantly changing (#74)
Browse files Browse the repository at this point in the history
* List of reports are constantly changing

Signed-off-by: Montse Ortega <mortegag@redhat.com>

* List of reports changing all the time

Signed-off-by: Montse Ortega <mortegag@redhat.com>

---------

Signed-off-by: Montse Ortega <mortegag@redhat.com>
  • Loading branch information
ammont82 authored Jan 22, 2025
1 parent 45df403 commit 5a33676
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ declare namespace DiscoverySources {
agents: Agent[]|undefined;
isLoadingAgents: boolean;
errorLoadingAgents?: Error;
listAgents: () => Promise<Agent[]>;
listAgents: () => Promise<Agent[]|undefined>;
deleteAgent: (agent: Agent) => Promise<Agent>;
isDeletingAgent: boolean;
errorDeletingAgent?: Error;
selectAgent: (agent:Agent) => void;
agentSelected: Agent;
selectSourceById: (sourceId:string)=>void;
getSourceById: (sourceId: string) => Source;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ export const Provider: React.FC<PropsWithChildren> = (props) => {

const [agentSelected, setAgentSelected] = useState<Agent | null>(null)

const [sourcesLoaded, setSourcesLoaded] = useState(false);

const sourceApi = useInjection<SourceApiInterface>(Symbols.SourceApi);
const agentsApi = useInjection<AgentApiInterface>(Symbols.AgentApi);

const [listAgentsState, listAgents] = useAsyncFn(async () => {
if (!sourcesLoaded) return;
const agents = await agentsApi.listAgents();
return agents;
});
},[sourcesLoaded]);

const [listSourcesState, listSources] = useAsyncFn(async () => {
const sources = await sourceApi.listSources();
setSourcesLoaded(true);
return sources;
});

Expand Down Expand Up @@ -82,23 +86,38 @@ export const Provider: React.FC<PropsWithChildren> = (props) => {
setIsPolling(false);
}
}, [isPolling]);

useInterval(() => {
listSources();
listAgents();
if (!listSourcesState.loading){
listSources().then(() => {
if (sourcesLoaded) {
listAgents();
}
});
}
}, pollingDelay);

const selectSource = useCallback((source: Source|null) => {
setSourceSelected(source);
}, []);

const selectSourceById = useCallback((sourceId: string) => {
const source = listSourcesState.value?.find(source => source.id === sourceId);
setSourceSelected(source||null);
}, [listSourcesState.value]);
if (!listSourcesState.loading){
const source = listSourcesState.value?.find(source => source.id === sourceId);
setSourceSelected(source||null);
}
else {
listSources().then((_sources)=>{
const source = _sources.find(source => source.id === sourceId);
setSourceSelected(source||null);
})
}

}, [listSources, listSourcesState]);


const selectAgent = useCallback(async (agent: Agent) => {
setAgentSelected(agent);
setAgentSelected(agent);
if (agent && agent.sourceId!==null) await selectSourceById(agent.sourceId ?? '');
}, [selectSourceById]);

Expand All @@ -111,6 +130,11 @@ export const Provider: React.FC<PropsWithChildren> = (props) => {
const deletedAgent = await agentsApi.deleteAgent({id: agent.id});
return deletedAgent;
});

const getSourceById = useCallback((sourceId: string) => {
const source = listSourcesState.value?.find(source => source.id === sourceId);
return source;
}, [listSourcesState.value]);

const ctx: DiscoverySources.Context = {
sources: listSourcesState.value ?? [],
Expand All @@ -137,7 +161,8 @@ export const Provider: React.FC<PropsWithChildren> = (props) => {
errorDeletingAgent: deleteAgentState.error,
selectAgent,
agentSelected: agentSelected,
selectSourceById
selectSourceById,
getSourceById
};

return <Context.Provider value={ctx}>{children}</Context.Provider>;
Expand Down
11 changes: 1 addition & 10 deletions apps/demo/src/migration-wizard/steps/connect/ConnectStep.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from "react";
import React, { useCallback, useState } from "react";
import {
Stack,
StackItem,
Expand Down Expand Up @@ -31,14 +31,6 @@ export const ConnectStep: React.FC = () => {
setShouldShowDiscoverySetupModal((lastState) => !lastState);
}, []);
const hasAgents = discoverySourcesContext.agents && discoverySourcesContext.agents.length > 0;
const [firstAgent, ..._otherAgents] = discoverySourcesContext.agents || [];

useEffect(() => {
if (!discoverySourcesContext.agentSelected && firstAgent) {
discoverySourcesContext.selectAgent(firstAgent);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [firstAgent]);

return (
<Stack hasGutter>
Expand Down Expand Up @@ -110,7 +102,6 @@ export const ConnectStep: React.FC = () => {
const sshKey = form["discoverySourceSshKey"].value as string;
await discoverySourcesContext.downloadSource(sshKey);
toggleDiscoverySourceSetupModal();
//await discoverySourcesContext.listSources();
await discoverySourcesContext.listAgents();
}}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Time } from "#/common/Time";

export const VALUE_NOT_AVAILABLE = "-";
export const DEFAULT_POLLING_DELAY = 3 * Time.Second;
export const DEFAULT_POLLING_DELAY = 1 * Time.Second;
Loading

0 comments on commit 5a33676

Please sign in to comment.