Skip to content

Commit

Permalink
chore: cleanup viz (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
stoffeastrom committed May 27, 2020
1 parent 97d0811 commit 237e656
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 deletions.
9 changes: 9 additions & 0 deletions apis/nucleus/src/hooks/usePropertiesById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import useRpc from './useRpc';
import { useModelStore } from '../stores/modelStore';

export default function usePropertiesById(id) {
const [modelStore] = useModelStore();
const model = modelStore.get(id);
const [properties, ...rest] = useRpc(model, 'getProperties');
return [properties, (p) => model.setProperties(p), ...rest];
}
4 changes: 3 additions & 1 deletion apis/nucleus/src/object/create-session-object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import populateData from './populator';
import init from './initiate';
import { subscribe } from '../stores/modelStore';
import { subscribe, modelStore } from '../stores/modelStore';
/**
* @interface CreateConfig
* @extends BaseConfig
Expand Down Expand Up @@ -43,10 +43,12 @@ export default async function createSessionObject({ type, version, fields, prope
// console.error(e); // eslint-disable-line
}
const model = await halo.app.createSessionObject(mergedProps);
modelStore.set(model.id, model);
const unsubscribe = subscribe(model);
const onDestroy = async () => {
await halo.app.destroySessionObject(model.id);
unsubscribe();
modelStore.set(model.id, undefined);
};
return init(model, { options, element }, halo, error, onDestroy);
}
4 changes: 1 addition & 3 deletions apis/nucleus/src/viz.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function viz({ model, halo, initialError, onDestroy = async () =>
* ctl.destroy();
*/
const api = /** @lends SupernovaController# */ {
model, // TODO - remove
id: model.id,
mount(element) {
if (mountedReference) {
throw new Error('Already mounted');
Expand All @@ -69,7 +69,6 @@ export default function viz({ model, halo, initialError, onDestroy = async () =>
* ctl.destroy();
*/
async destroy() {
// TODO - destroy session object (if created as such)
await onDestroy();
unmountCell();
unmountCell = noopi;
Expand All @@ -84,7 +83,6 @@ export default function viz({ model, halo, initialError, onDestroy = async () =>
},
options(opts) {
setSnOptions(opts);
// return api;
},
exportImage() {
// TODO - check if exportable
Expand Down
8 changes: 4 additions & 4 deletions commands/serve/web/components/Properties.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React, { useCallback, useState } from 'react';

import { Divider, Grid, Checkbox, FormControlLabel } from '@material-ui/core';

import useProperties from '@nebula.js/nucleus/src/hooks/useProperties';
import usePropertiesById from '@nebula.js/nucleus/src/hooks/usePropertiesById';

import Data from './property-panel/Data';
import generateComponents from './AutoComponents';

export default function Properties({ viz, sn, isTemp, storage }) {
const [properties] = useProperties(viz.model);
const [properties, setProperties] = usePropertiesById(viz.id);

const [isReadCacheEnabled, setReadCacheEnabled] = useState(storage.get('readFromCache') !== false);

Expand All @@ -18,7 +18,7 @@ export default function Properties({ viz, sn, isTemp, storage }) {
};

const changed = useCallback(() => {
viz.model.setProperties(properties);
setProperties(properties);
}, [viz, sn, properties]);

if (!sn) {
Expand Down Expand Up @@ -50,7 +50,7 @@ export default function Properties({ viz, sn, isTemp, storage }) {
<Divider />
</>
)}
<Data properties={properties} model={viz.model} sn={sn} />
<Data properties={properties} setProperties={setProperties} sn={sn} />
{generateComponents(properties, changed)}
</div>
);
Expand Down
37 changes: 18 additions & 19 deletions commands/serve/web/components/Stage.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import React, { useEffect, useState, useContext } from 'react';
import useProperties from '@nebula.js/nucleus/src/hooks/useProperties';

import { Grid } from '@material-ui/core';

import usePropertiesById from '@nebula.js/nucleus/src/hooks/usePropertiesById';

import Cell from './Cell';
import NebulaContext from '../contexts/NebulaContext';
import VizContext from '../contexts/VizContext';

export default function Stage({ info, storage, uid }) {
const nebbie = useContext(NebulaContext);
const [model, setModel] = useState(null);
const [properties] = useProperties(model);
const [hasViz, setHasViz] = useState(false);
const { setActiveViz } = useContext(VizContext);
const [properties] = usePropertiesById(uid);

useEffect(() => {
if (!uid) {
Expand All @@ -28,8 +29,8 @@ export default function Stage({ info, storage, uid }) {
},
});
res.then((v) => {
setModel(v.model);
setActiveViz(v);
setHasViz(true);
});
return () => {
res.then((v) => v.destroy());
Expand All @@ -41,22 +42,20 @@ export default function Stage({ info, storage, uid }) {
storage.props(info.supernova.name, properties);
}, [properties]);

if (!model) {
return null;
}

return (
<Grid
item
container
justify="center"
style={{
height: '100%',
}}
>
<Grid item xs>
<Cell id={model.id} />
hasViz && (
<Grid
item
container
justify="center"
style={{
height: '100%',
}}
>
<Grid item xs>
<Cell id={uid} />
</Grid>
</Grid>
</Grid>
)
);
}
4 changes: 2 additions & 2 deletions commands/serve/web/components/property-panel/Data.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { List, ListItem, Typography } from '@material-ui/core';

import HyperCube from './HyperCube';

export default function Data({ model, sn, properties }) {
export default function Data({ setProperties, sn, properties }) {
if (!sn) {
return null;
}
Expand All @@ -19,7 +19,7 @@ export default function Data({ model, sn, properties }) {
<List>
{targets.map((t) => (
<ListItem key={t.propertyPath} divider disableGutters>
<HyperCube target={t} properties={properties} model={model} />
<HyperCube target={t} properties={properties} setProperties={setProperties} />
</ListItem>
))}
</List>
Expand Down
10 changes: 5 additions & 5 deletions commands/serve/web/components/property-panel/HyperCube.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const getValue = (data, reference, defaultValue) => {
return dataContainer;
};

export default function HyperCube({ model, target, properties }) {
export default function HyperCube({ setProperties, target, properties }) {
const handler = useMemo(
() =>
hcHandler({
Expand All @@ -38,22 +38,22 @@ export default function HyperCube({ model, target, properties }) {

const onDimensionAdded = (a) => {
handler.addDimension(typeof a === 'object' ? { qLibraryId: a.qId } : a);
model.setProperties(properties);
setProperties(properties);
};

const onDimensionRemoved = (idx) => {
handler.removeDimension(idx);
model.setProperties(properties);
setProperties(properties);
};

const onMeasureAdded = (a) => {
handler.addMeasure(typeof a === 'object' ? { qLibraryId: a.qId } : a);
model.setProperties(properties);
setProperties(properties);
};

const onMeasureRemoved = (idx) => {
handler.removeMeasure(idx);
model.setProperties(properties);
setProperties(properties);
};

return (
Expand Down

0 comments on commit 237e656

Please sign in to comment.