-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces useOpenGexf by proper atom and actions
Details: - Adds typings and helpers for AsyncAction - Adds graph/files.ts, with files lifecycle logic - Replaces all useOpenGexf usages by the new atom and actions - Deletes useOpenGexf
- Loading branch information
Showing
15 changed files
with
142 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { asyncAction } from "../utils/producers"; | ||
import { FileState, LocalFile, RemoteFile } from "./types"; | ||
import { parse } from "graphology-gexf"; | ||
import Graph from "graphology"; | ||
import { resetStates } from "../context/dataContexts"; | ||
import { getEmptyFileState, initializeGraphDataset } from "./utils"; | ||
import { preferencesActions } from "../preferences"; | ||
import { resetCamera } from "../sigma"; | ||
import { atom } from "../utils/atoms"; | ||
import { graphDatasetActions } from "./index"; | ||
|
||
/** | ||
* Public API: | ||
* *********** | ||
*/ | ||
export const fileStateAtom = atom<FileState>(getEmptyFileState()); | ||
|
||
/** | ||
* Actions: | ||
* ******** | ||
*/ | ||
export const openRemoteFile = asyncAction(async (remote: RemoteFile) => { | ||
const { setGraphDataset } = graphDatasetActions; | ||
const { addRemoteFile } = preferencesActions; | ||
|
||
if (fileStateAtom.get().type === "loading") throw new Error("A file is already being loaded"); | ||
|
||
fileStateAtom.set({ type: "loading" }); | ||
try { | ||
const response = await fetch(remote.url); | ||
const gexf = await response.text(); | ||
const graph = parse(Graph, gexf, { allowUndeclaredAttributes: true, addMissingNodes: true }); | ||
graph.setAttribute("title", remote.filename); | ||
resetStates(); | ||
setGraphDataset({ ...initializeGraphDataset(graph), origin: remote }); | ||
addRemoteFile(remote); | ||
resetCamera({ forceRefresh: true }); | ||
} catch (e) { | ||
fileStateAtom.set({ type: "error", message: (e as Error).message }); | ||
throw e; | ||
} finally { | ||
fileStateAtom.set({ type: "idle" }); | ||
} | ||
}); | ||
|
||
export const openLocalFile = asyncAction(async (file: LocalFile) => { | ||
const { setGraphDataset } = graphDatasetActions; | ||
|
||
if (fileStateAtom.get().type === "loading") throw new Error("A file is already being loaded"); | ||
|
||
fileStateAtom.set({ type: "loading" }); | ||
try { | ||
const content = await file.source.text(); | ||
const graph = parse(Graph, content, { allowUndeclaredAttributes: true }); | ||
graph.setAttribute("title", file.filename); | ||
resetStates(); | ||
setGraphDataset({ ...initializeGraphDataset(graph), origin: file }); | ||
resetCamera({ forceRefresh: true }); | ||
} catch (e) { | ||
fileStateAtom.set({ type: "error", message: (e as Error).message }); | ||
throw e; | ||
} finally { | ||
fileStateAtom.set({ type: "idle" }); | ||
} | ||
}); | ||
|
||
export const fileActions = { | ||
openRemoteFile, | ||
openLocalFile, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.