Skip to content

Commit 9cb8386

Browse files
committed
[file] get error when loading a gephi lite file incompatible
1 parent 72de058 commit 9cb8386

5 files changed

Lines changed: 21 additions & 9 deletions

File tree

packages/gephi-lite/src/components/modals/open/LocalFileModal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const OpenLocalFileForm: FC<OpenLocalFileFormProps> = ({ id, onStatusChan
2323
status: { type: importStateType },
2424
} = useFile();
2525

26+
useEffect(() => {}, []);
27+
2628
const onSubmit = useCallback(
2729
async (file: File) => {
2830
onStatusChange({ type: "loading" });
@@ -61,13 +63,13 @@ export const OpenLocalFileForm: FC<OpenLocalFileFormProps> = ({ id, onStatusChan
6163
if (file) onSubmit(file);
6264
}}
6365
>
64-
{importStateType === "error" && <p className="text-center text-danger">{t("graph.open.local.error")}</p>}
6566
<DropInput
6667
value={file}
6768
onChange={(file) => setFile(file)}
6869
helpText={t("graph.open.local.dragndrop_text")}
6970
accept={{ "application/graph": [".gexf", ".graphml"], "application/json": [".json"] }}
7071
>
72+
{importStateType === "error" && <p className="text-center text-danger">{t("graph.open.local.error")}</p>}
7173
{!file && (
7274
<button className="gl-btn gl-btn-outline mb-2">
7375
<PiFolderOpen /> {t("graph.open.local.button_text")}

packages/gephi-lite/src/core/file/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function getLocalStorageFileState(): FileState {
3232
return {
3333
...getEmptyFileState(),
3434
...state,
35+
status: { type: "idle" },
3536
};
3637
}
3738

@@ -119,11 +120,10 @@ export const open = asyncAction(async (file: FileTypeWithoutFormat) => {
119120

120121
// Reset the camera
121122
resetCamera({ forceRefresh: true });
123+
fileAtom.set((prev) => ({ ...prev, status: { type: "idle" } }));
122124
} catch (e) {
123125
fileAtom.set((prev) => ({ ...prev, status: { type: "error", message: (e as Error).message } }));
124126
throw e;
125-
} finally {
126-
fileAtom.set((prev) => ({ ...prev, status: { type: "idle" } }));
127127
}
128128
});
129129

packages/gephi-lite/src/core/file/utils.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { FieldModel, gephiLiteParse } from "@gephi/gephi-lite-sdk";
22
import Graph from "graphology";
33
import gexf from "graphology-gexf/browser";
44
import graphml from "graphology-graphml/browser";
5+
import { parse as parseVersion } from "semver";
56

7+
import { config } from "../../config";
68
import { userAtom } from "../user";
79
import { FileFormat, FileTypeWithoutFormat, GephiLiteFileFormat, fileFormatExt } from "./types";
810

@@ -88,10 +90,18 @@ export async function extractGraphFromFile(
8890
case "json": {
8991
const jsonContent = gephiLiteParse(fileContent);
9092
if ("type" in jsonContent && jsonContent.type === "gephi-lite") {
91-
return {
92-
format: "gephi-lite",
93-
data: jsonContent,
94-
};
93+
const version = parseVersion(jsonContent.version);
94+
console.log("version", version, config.version);
95+
if (version && version.major === config.version.major && version.minor === config.version.minor) {
96+
return {
97+
format: "gephi-lite",
98+
data: jsonContent,
99+
};
100+
} else {
101+
throw new Error(
102+
`Your file is from an older version of gephi-lite (${version?.toString()}) which is not compatible with the actual version`,
103+
);
104+
}
95105
} else {
96106
return {
97107
format: "graphology",

packages/gephi-lite/src/locales/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
},
326326
"local": {
327327
"dragndrop_text": "Drag'n'drop a GEXF, GRAPHML or Graphology JSON file ",
328-
"error": "An error occurred while opening the file. Please check that the file is a valid.",
328+
"error": "An error occurred while opening the file. Please check that the file is a valid and compatible with the actual version.",
329329
"success": "File {{filename}} has been successfully loaded",
330330
"title": "Open a local file"
331331
},

packages/gephi-lite/src/locales/fr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@
486486
"local": {
487487
"button_text": "Sélectionner un fichier local",
488488
"dragndrop_text": "Faites glisser-déposer un fichier GEXF, GRAPHML ou JSON au format Graphology.",
489-
"error": "Une erreur s'est produite lors de l'ouverture du fichier. Veuillez vérifier que le fichier est valide.",
489+
"error": "Une erreur s'est produite lors de l'ouverture du fichier. Veuillez vérifier que le fichier est valide et compatible avec la version courante.",
490490
"success": "Le fichier {{filename}} a bien été ouvert",
491491
"title": "Ouvrir un fichier local"
492492
},

0 commit comments

Comments
 (0)