Skip to content

Commit ded6e2c

Browse files
committed
#110 Refactor useCurrentAgent, set only once
1 parent da5d7ab commit ded6e2c

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

data-browser/src/routes/SettingsAgent.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,23 @@ const SettingsAgent: React.FunctionComponent = () => {
7474
}
7575
}
7676

77+
function setAgentIfChanged(oldAgent: Agent, newAgent: Agent) {
78+
if (JSON.stringify(oldAgent) !== JSON.stringify(newAgent)) {
79+
setAgent(newAgent);
80+
}
81+
}
82+
7783
/** Called when the secret or the subject is updated manually */
7884
async function handleUpdateSubjectAndKey() {
7985
renewSecret();
8086
setError(null);
8187

8288
try {
83-
const agent = new Agent(privateKey, subject);
84-
await agent.getPublicKey();
85-
await agent.checkPublicKey();
86-
setAgent(agent);
89+
const newAgent = new Agent(privateKey, subject);
90+
await newAgent.getPublicKey();
91+
await newAgent.checkPublicKey();
92+
93+
setAgentIfChanged(agent, newAgent);
8794
} catch (e) {
8895
const err = new Error('Invalid Agent' + e);
8996
setError(err);
@@ -105,11 +112,11 @@ const SettingsAgent: React.FunctionComponent = () => {
105112
setError(null);
106113

107114
try {
108-
const agent = Agent.fromSecret(updateSecret);
109-
await agent.checkPublicKey();
110-
setAgent(agent);
111-
setPrivateKey(agent.privateKey);
112-
setSubject(agent.subject);
115+
const newAgent = Agent.fromSecret(updateSecret);
116+
await newAgent.checkPublicKey();
117+
setAgentIfChanged(agent, newAgent);
118+
setPrivateKey(newAgent.privateKey);
119+
setSubject(newAgent.subject);
113120
} catch (e) {
114121
const err = new Error('Invalid secret. ' + e);
115122
setError(err);

data-browser/src/views/EndpointPage.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ function EndpointPage({ resource }: EndpointProps): JSX.Element {
5858
key={param}
5959
propertyURL={param}
6060
resource={virtualResource}
61-
// autoFocus={i == 0}
6261
/>
6362
);
6463
})}

lib/src/store.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,12 @@ export class Store {
278278
setAgent(agent: Agent): void {
279279
this.agent = agent;
280280
// TODO: maybe iterate over all loaded resources, check if they have an Unauthorized error, and retry these.
281-
this.resources.forEach(r => {
282-
if (r.isUnauthorized()) {
283-
this.fetchResource(r.getSubject());
284-
}
285-
});
281+
agent &&
282+
this.resources.forEach(r => {
283+
if (r.isUnauthorized()) {
284+
this.fetchResource(r.getSubject());
285+
}
286+
});
286287
}
287288

288289
/** Sets the Base URL, without the trailing slash. */

react/src/useCurrentAgent.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,24 @@ export const useCurrentAgent = (): [Agent | null, (agent?: Agent) => void] => {
1616
AGENT_LOCAL_STORAGE_KEY,
1717
null,
1818
);
19+
const store = useStore();
1920
// In memory representation of the full Agent
20-
const [agent, setAgent] = useState<Agent>(null);
21+
const [stateAgent, setStateAgent] = useState<Agent>(store.getAgent());
2122
// Also update the Agent inside the store
22-
const store = useStore();
2323

24-
// Set the initial agent, is set using Store
25-
useEffect(() => {
26-
if (agent == undefined) {
27-
setAgent(store.getAgent());
28-
}
29-
}, []);
30-
31-
// When the localStorage JSON agent is updated, also update the in-memory agent
24+
// When the localStorage JSON agent is updated, also update the in-memory agent and the Store
3225
useEffect(() => {
3326
if (agentJSON == null) {
34-
setAgent(null);
27+
setStateAgent(null);
28+
store.setAgent(null);
3529
return;
3630
}
37-
setAgent(Agent.fromJSON(agentJSON));
31+
const newAgent = Agent.fromJSON(agentJSON);
32+
setStateAgent(newAgent);
33+
store.setAgent(newAgent);
3834
}, [agentJSON]);
3935

40-
// ... and when the in memory agent is updated, update the store agent
41-
useEffect(() => {
42-
store.setAgent(agent);
43-
}, [agent]);
44-
45-
return [agent, setAgentJSON];
36+
return [stateAgent, setAgentJSON];
4637
};
4738

4839
/** Gets the Agent from local storage, if any. Useful when initializing app */

0 commit comments

Comments
 (0)