Skip to content

Commit 429aaaa

Browse files
committed
cache user id to avoid repeated requests on graph load
1 parent e0ed779 commit 429aaaa

2 files changed

Lines changed: 64 additions & 16 deletions

File tree

src/graph_view/GraphView.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,19 @@ const GraphView = (params: {
160160
graphName: SETTINGS.EXPORT_GRAPH,
161161
};
162162

163-
const [currentUser, setCurrentUser] = useState<string>("");
163+
// Seeding from the cache keeps the iframe src stable from the first
164+
// render — an src change mid-load reboots the graph viewer
165+
const cachedUserId = InfraNodus.getCachedUserId();
164166

165-
const [iframeGraphUser, setIframeGraphUser] = useState<string>("");
167+
const [currentUser, setCurrentUser] = useState<string>(cachedUserId ?? "");
168+
169+
const [iframeGraphUser, setIframeGraphUser] = useState<string>(
170+
cachedUserId ? `&user=${cachedUserId}` : ""
171+
);
166172

167173
useEffect(() => {
168174
const fetchUserId = async () => {
175+
if (currentUser) return;
169176
if (auth_token) {
170177
try {
171178
const userResponse = await InfraNodus.getUserId({

src/infranodus/index.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -903,23 +903,64 @@ class InfraNodus {
903903
}
904904
}
905905

906-
public static async getUserId(params: { headerToken: string }) {
907-
try {
908-
const postResult = await this.genericPost(
909-
"api/v1/userId",
910-
{
911-
headerToken: params.headerToken,
912-
},
913-
{ credentials: "include" }
914-
);
906+
// Cached InfraNodus user id, keyed by the API key it was fetched with —
907+
// the id never changes for a given key, so one fetch per session is
908+
// enough. Keeping the iframe URL stable from the first render also
909+
// prevents the graph viewer from rebooting mid-load when the id arrives
910+
private static userIdCache: { apiKey: string; userId: string } | null =
911+
null;
912+
private static userIdInFlight: {
913+
apiKey: string;
914+
promise: Promise<{ userId?: string; error?: boolean }>;
915+
} | null = null;
916+
917+
public static getCachedUserId(): string | null {
918+
const apiKey = SETTINGS.INFRANODUS_API_KEY;
919+
return apiKey && InfraNodus.userIdCache?.apiKey === apiKey
920+
? InfraNodus.userIdCache.userId
921+
: null;
922+
}
915923

916-
console.log("postResult", postResult);
924+
public static async getUserId(params: {
925+
headerToken: string;
926+
}): Promise<{ userId?: string; error?: boolean }> {
927+
const apiKey = params.headerToken;
917928

918-
return { userId: postResult.data.userId };
919-
} catch (err) {
920-
console.error("Error when submitting content to InfraNodus", err);
921-
return { error: true };
929+
if (InfraNodus.userIdCache?.apiKey === apiKey) {
930+
return { userId: InfraNodus.userIdCache.userId };
922931
}
932+
if (InfraNodus.userIdInFlight?.apiKey === apiKey) {
933+
return InfraNodus.userIdInFlight.promise;
934+
}
935+
936+
const promise = (async () => {
937+
try {
938+
const postResult = await this.genericPost(
939+
"api/v1/userId",
940+
{
941+
headerToken: apiKey,
942+
},
943+
{ credentials: "include" }
944+
);
945+
946+
const userId = postResult.data.userId;
947+
if (userId) InfraNodus.userIdCache = { apiKey, userId };
948+
949+
return { userId };
950+
} catch (err) {
951+
console.error(
952+
"Error when getting the user id from InfraNodus",
953+
err
954+
);
955+
return { error: true };
956+
} finally {
957+
if (InfraNodus.userIdInFlight?.apiKey === apiKey)
958+
InfraNodus.userIdInFlight = null;
959+
}
960+
})();
961+
962+
InfraNodus.userIdInFlight = { apiKey, promise };
963+
return promise;
923964
}
924965
}
925966

0 commit comments

Comments
 (0)