From 71b5affd6a139cceda490fb1d01a5c7f92a975ec Mon Sep 17 00:00:00 2001 From: afonso Date: Tue, 23 Apr 2024 20:25:08 +0100 Subject: [PATCH] CELE-17 feat: Add usage example for helper functions --- .../src/components/RightComponent.tsx | 55 ++++++++++++++++--- .../src/helpers/initialWorkspacesHelper.ts | 7 ++- .../frontend/src/helpers/workspacesHelper.ts | 27 ++++----- .../visualizer/frontend/src/models.ts | 7 +-- 4 files changed, 67 insertions(+), 29 deletions(-) diff --git a/applications/visualizer/frontend/src/components/RightComponent.tsx b/applications/visualizer/frontend/src/components/RightComponent.tsx index 94d19f6e..5f9ee21f 100644 --- a/applications/visualizer/frontend/src/components/RightComponent.tsx +++ b/applications/visualizer/frontend/src/components/RightComponent.tsx @@ -1,16 +1,24 @@ import {Box, Button, List, ListItem, ListItemText, Typography} from "@mui/material"; import {useGlobalContext} from "../contexts/GlobalContext.tsx"; -import {activateNeuron, deactivateNeuron} from "../helpers/workspacesHelper.ts"; +import { + activateDataset, + activateNeuron, + changeViewerVisibility, + deactivateDataset, + deactivateNeuron, updateViewerSynchronizationStatus +} from "../helpers/workspacesHelper.ts"; +import {ViewerSynchronizationPair, ViewerType} from "../models.ts"; const testNeuron = {id: 'neuron3', label: 'Neuron 3'}; +const testDataset = {id: 'dataset1', name: 'Dataset 1'}; export default function RightComponent() { const {workspaces, currentWorkspaceId, updateWorkspace} = useGlobalContext(); const workspace = workspaces[currentWorkspaceId]; function withWorkspaceUpdate(modifyWorkspace) { - return function (workspace, ...args) { + return function (...args) { const updatedWorkspace = modifyWorkspace(workspace, ...args); updateWorkspace(workspace.id, updatedWorkspace); return updatedWorkspace; @@ -19,6 +27,10 @@ export default function RightComponent() { const addNeuronAndUpdate = withWorkspaceUpdate(activateNeuron); const removeNeuronAndUpdate = withWorkspaceUpdate(deactivateNeuron); + const addDatasetAndUpdate = withWorkspaceUpdate(activateDataset); + const removeDatasetAndUpdate = withWorkspaceUpdate(deactivateDataset); + const toggleViewerVisibility = withWorkspaceUpdate(changeViewerVisibility); + const toggleSyncStatus = withWorkspaceUpdate(updateViewerSynchronizationStatus); if (!workspace) { return ( @@ -28,6 +40,10 @@ export default function RightComponent() { ); } + const viewerToToggle = ViewerType.Graph; + const currentVisibility = workspace.viewers[viewerToToggle]; + const syncPair = ViewerSynchronizationPair.Graph_InstanceDetails; + const currentSyncStatus = workspace.synchronizations[syncPair]; return ( @@ -37,20 +53,41 @@ export default function RightComponent() { - + + + + Viewers: - {Object.entries(workspace.viewers).map(([id, viewer]) => ( - - + {Object.entries(workspace.viewers).map(([type, isVisible]) => ( + + ))} diff --git a/applications/visualizer/frontend/src/helpers/initialWorkspacesHelper.ts b/applications/visualizer/frontend/src/helpers/initialWorkspacesHelper.ts index edf09dc5..93ff95d4 100644 --- a/applications/visualizer/frontend/src/helpers/initialWorkspacesHelper.ts +++ b/applications/visualizer/frontend/src/helpers/initialWorkspacesHelper.ts @@ -11,7 +11,12 @@ export const createEmptyWorkspace = (name: string): Workspace => { return { id: workspaceId, name: name, - viewers: {}, + viewers: { + [ViewerType.Graph]: true, + [ViewerType.ThreeD]: true, + [ViewerType.EM]: false, + [ViewerType.InstanceDetails]: false, + }, datasets: {}, neurons: {}, synchronizations: { diff --git a/applications/visualizer/frontend/src/helpers/workspacesHelper.ts b/applications/visualizer/frontend/src/helpers/workspacesHelper.ts index 5f21a4ab..f72e559a 100644 --- a/applications/visualizer/frontend/src/helpers/workspacesHelper.ts +++ b/applications/visualizer/frontend/src/helpers/workspacesHelper.ts @@ -1,4 +1,4 @@ -import {Dataset, Neuron, NeuronGroup, ViewerSynchronizationPair, Workspace} from "../models.ts"; +import {Dataset, Neuron, NeuronGroup, ViewerSynchronizationPair, ViewerType, Workspace} from "../models.ts"; export function activateNeuron(workspace: Workspace, neuron: Neuron): Workspace { return { @@ -38,18 +38,15 @@ export function deactivateDataset(workspace: Workspace, datasetId: string): Work }; } -export function changeViewerVisibility(workspace: Workspace, viewerId: string, isVisible: boolean): Workspace { - if (!workspace.viewers[viewerId]) { +export function changeViewerVisibility(workspace: Workspace, viewerId: ViewerType, isVisible: boolean): Workspace { + if (workspace.viewers[viewerId] === undefined) { throw new Error('Viewer not found'); } return { ...workspace, viewers: { ...workspace.viewers, - [viewerId]: { - ...workspace.viewers[viewerId], - isVisible: isVisible - } + [viewerId]: isVisible, } }; } @@ -70,17 +67,21 @@ export function addNeuronToGroup(workspace: Workspace, neuronId: string, groupId if (!neuron) { throw new Error('Neuron not found'); } - if (!workspace.neuronGroups[groupId]) { + const group = workspace.neuronGroups[groupId]; + if (!group) { throw new Error('Neuron group not found'); } + const updatedNeurons = new Set(group.neurons); + updatedNeurons.add(neuronId); + return { ...workspace, - neurons: { - ...workspace.neurons, - [neuronId]: { - ...neuron, - groupId: groupId + neuronGroups: { + ...workspace.neuronGroups, + [groupId]: { + ...group, + neurons: updatedNeurons } } }; diff --git a/applications/visualizer/frontend/src/models.ts b/applications/visualizer/frontend/src/models.ts index 2d0e474e..7f1647b7 100644 --- a/applications/visualizer/frontend/src/models.ts +++ b/applications/visualizer/frontend/src/models.ts @@ -16,7 +16,7 @@ export enum ViewerSynchronizationPair { export interface Workspace { id: string; name: string; - viewers: Record; + viewers: Record; datasets: Record; neurons: Record; synchronizations: Record; @@ -26,11 +26,6 @@ export interface Workspace { layoutManager: unknown; } -interface Viewer { - type: ViewerType; - isVisible: boolean; -} - export interface Dataset { id: string; name: string;