Skip to content

Commit

Permalink
fix: cant upload a topic again due to id conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
keyserj committed Feb 15, 2024
1 parent 3f4a4b6 commit fe319d3
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/web/topic/store/utilActions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { v4 as uuid } from "uuid";
import { StorageValue } from "zustand/middleware";

import { errorWithData } from "../../../common/errorHandling";
import { emitter } from "../../common/event";
import { TopicStoreState, initialState, playgroundUsername, useTopicStore } from "./store";
import { Edge, Node } from "../utils/graph";
import {
TopicStoreState,
UserScores,
initialState,
playgroundUsername,
useTopicStore,
} from "./store";
import { isPlaygroundTopic } from "./utils";

export const getPersistState = () => {
Expand All @@ -19,6 +27,49 @@ export const getScoringUsernames = () => {
return Object.keys(userScores);
};

/**
* Generate new ids for nodes and edges to avoid conflicts with the topic that this was downloaded
* from.
*
* Also, ensure that related scores, claims, and edges are updated accordingly.
*/
const ensureUnique = (nodes: Node[], edges: Edge[], userScores: UserScores) => {
[...nodes, ...edges].forEach((graphPart) => {
const newId = uuid();

// update edges
edges.forEach((edge) => {
// eslint-disable-next-line functional/immutable-data, no-param-reassign
if (edge.source === graphPart.id) edge.source = newId;
// eslint-disable-next-line functional/immutable-data, no-param-reassign
if (edge.target === graphPart.id) edge.target = newId;
});

// update claims
[...nodes, ...edges].forEach((otherGraphPart) => {
if (otherGraphPart.data.arguedDiagramPartId === graphPart.id) {
// eslint-disable-next-line functional/immutable-data, no-param-reassign
otherGraphPart.data.arguedDiagramPartId = newId;
}
});

// update scores
Object.entries(userScores).forEach(([_username, partScores]) => {
const partScore = partScores[graphPart.id];
if (partScore) {
// eslint-disable-next-line functional/immutable-data, no-param-reassign
partScores[newId] = partScore;
// eslint-disable-next-line functional/immutable-data, @typescript-eslint/no-dynamic-delete, no-param-reassign
delete partScores[graphPart.id];
}
});

// update graph part
// eslint-disable-next-line functional/immutable-data, no-param-reassign
graphPart.id = newId;
});
};

export const setTopicData = (state: TopicStoreState, sessionUsername?: string) => {
// Don't override topic - this way, topic data from playground can be downloaded and uploaded as
// a means of saving playground data to the db.
Expand All @@ -33,6 +84,8 @@ export const setTopicData = (state: TopicStoreState, sessionUsername?: string) =
const myUsername = isPlaygroundTopic(topic) ? playgroundUsername : sessionUsername;
const userScores = myScores && myUsername ? { [myUsername]: myScores } : {};

ensureUnique(state.nodes, state.edges, userScores);

useTopicStore.setState({ ...state, topic, userScores }, false, "setState");

emitter.emit("loadedTopicData");
Expand Down

0 comments on commit fe319d3

Please sign in to comment.