Skip to content

Commit

Permalink
Merge pull request #294 from stone-lyl/feat-add-node
Browse files Browse the repository at this point in the history
feat: move Save from SlotComponents to a first-class citizen in dataStoryControls
  • Loading branch information
ajthinking authored Sep 15, 2024
2 parents f9d7b1d + e108802 commit 243176a
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 48 deletions.
9 changes: 4 additions & 5 deletions packages/docs/components/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo } from 'react';
import { DataStory, WorkspaceApiJSClient, WorkspaceSocketClient } from '@data-story/ui';
import { SaveComponent } from './Save';
import { useRequestApp } from './hooks/useRequestApp';
import { ToastComponent } from './Toast';

export default Playground;

Expand All @@ -20,11 +20,10 @@ function Playground({ mode }: {mode?: 'js' | 'node'}) {
<div className="w-full" style={{ height: 'calc(100vh - 72px)' }} data-cy="playground">
<DataStory
client={client}
slotComponents={[
<SaveComponent/>,
]}
initSidebarKey="explorer"
/>
>
<ToastComponent />
</DataStory>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { ControlButton } from '@xyflow/react';
import React, { useCallback } from 'react';
import React from 'react';
import {
type DataStoryControlsType,
DataStoryEvents,
DataStoryEventType,
SaveIcon,
useDataStoryControls,
useDataStoryEvent
} from '@data-story/ui';
import { Bounce, toast, ToastContainer, ToastOptions } from 'react-toastify';
Expand All @@ -32,37 +28,28 @@ export function successToast(content: string): void {
}

const initToast = (event: DataStoryEventType) => {
if (event.type === DataStoryEvents.RUN_SUCCESS) {
successToast('Diagram executed successfully!');
}

if (event.type === DataStoryEvents.RUN_ERROR) {
errorToast('Diagram execution failed!');
switch (event.type) {
case DataStoryEvents.RUN_SUCCESS:
successToast('Diagram executed successfully!');
break;
case DataStoryEvents.RUN_ERROR:
console.error(event.payload);
errorToast('Diagram execution failed!');
break;
case DataStoryEvents.SAVE_SUCCESS:
successToast('Diagram saved successfully!');
break;
case DataStoryEvents.SAVE_ERROR:
console.error(event.payload);
errorToast('Diagram save failed!');
}
};

export const SaveComponent = () => {
const { onSave } = useDataStoryControls() as DataStoryControlsType;

export const ToastComponent = () => {
useDataStoryEvent(initToast);

const handleSave = useCallback(async() => {
try {
await onSave?.();
successToast('Diagram saved successfully!');
} catch (error) {
errorToast('Diagram save failed!');
}
}, [onSave]);

return (
<>
<ControlButton
title="Save"
aria-label="Save"
onClick={handleSave}>
<SaveIcon/>
</ControlButton>
<ToastContainer/>
</>
);
Expand Down
1 change: 1 addition & 0 deletions packages/docs/components/demos/ObserversDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default ({ mode, observers }:
<DataStory
client={client}
observers={observers}
hideControls={['save']}
/>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/docs/components/demos/TinkerDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default () => {
<div className="w-full h-1/2">
<DataStory
client={client}
hideControls={['save']}
/>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/docs/components/demos/Tree/AddNodeDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default () => {
<DataStory
client={client}
hideActivityBar={true}
hideControls={['save']}
/>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/docs/components/demos/Tree/MultipleDiagram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default () => {
<div className="w-full h-80 border-gray-400 border-4">
<DataStory
client={client}
hideControls={['save']}
/>
</div>
);
Expand Down
3 changes: 3 additions & 0 deletions packages/docs/components/demos/UnfoldingDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ export default ({ part }: {part: 'MAIN' | 'NESTED_NODE' | 'MAIN_UNFOLDED'}) => {
return (
<div className="w-full h-1/4">
{part === 'MAIN' && <DataStory
hideControls={['save']}
onInitialize={({ run }) => run()}
client={mainClient}
/>}
{part === 'NESTED_NODE' && <DataStory
hideControls={['save']}
client={nestedNodeClient}
/>}
{part === 'MAIN_UNFOLDED' && <DataStory
hideControls={['save']}
client={mainUnfoldedClient}
/>}
</div>
Expand Down
1 change: 1 addition & 0 deletions packages/docs/components/demos/VisualizeDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export default () => {
setPoints([])
run()
}}
hideControls={['save']}
client={client}
observers={{
inputObservers: [{ nodeId: 'Table.1', portId: 'input' }],
Expand Down
18 changes: 16 additions & 2 deletions packages/ui/src/components/DataStory/DataStory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { LoadingMask } from './common/loadingMask';
import { Diagram, Tree } from '@data-story/core';
import { ActivityGroups, findFirstFileNode, LocalStorageKey } from './common/method';
import { NodeApi } from 'react-arborist';
import { DataStoryEvents } from './events/dataStoryEventType';
import { eventManager } from './events/eventManager';

function handleRequestError(requestError?: Error): void {
if (requestError) console.error(`Error fetching : ${requestError?.message}`);
Expand All @@ -21,7 +23,7 @@ function handleRequestError(requestError?: Error): void {
export const DataStoryComponent = (
props: DataStoryProps
) => {
const { client, initSidebarKey } = props
const { client, initSidebarKey, children } = props;
const [selectedNode, setSelectedNode] = useState<ReactFlowNode>();
const [isSidebarClose, setIsSidebarClose] = useState(!!props.hideSidebar);
const [updateSelectedNodeData, setUpdateSelectedNodeData] = useState<ReactFlowNode['data']>();
Expand Down Expand Up @@ -68,7 +70,18 @@ export const DataStoryComponent = (
}
updateTree(newTree);

client.updateTree({ path: LocalStorageKey, tree: newTree });
client.updateTree({ path: LocalStorageKey, tree: newTree })
.then(() => {
eventManager.emit({
type: DataStoryEvents.SAVE_SUCCESS
});
})
.catch((error) => {
eventManager.emit({
type: DataStoryEvents.SAVE_ERROR,
payload: error
});
});
}, [diagramKey, tree]);

const handleClickExplorerNode = useCallback((node: NodeApi<Tree>) => {
Expand Down Expand Up @@ -119,6 +132,7 @@ export const DataStoryComponent = (
return (
<DataStoryCanvasProvider>
<div className="relative h-full w-full">
{ children }
{
(treeLoading && !tree)
? <LoadingMask/>
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/DataStory/DataStoryCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const DataStoryCanvas = React.memo(DataStoryCanvasComponent);

const Flow = ({
initDiagram,
hideControls = false,
hideControls,
slotComponents,
observers,
onInitialize,
Expand Down
37 changes: 28 additions & 9 deletions packages/ui/src/components/DataStory/dataStoryControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { RunIcon } from './icons/runIcon';
import { AddNodeIcon } from './icons/addNodeIcon';
import { Diagram } from '@data-story/core';
import { useStore } from './store/store';
import React, { useMemo } from 'react';
import { DataStoryCanvasProps, StoreSchema } from './types';
import React, { useCallback, useMemo } from 'react';
import { DataStoryCanvasProps, DataStoryProps, StoreSchema } from './types';
import { SaveIcon } from './icons/saveIcon';

export type DataStoryControlsType = {
getDiagram: () => Diagram;
Expand All @@ -29,7 +30,7 @@ export function DataStoryControls({
slotComponents,
onSave,
}: {
hideControls?: boolean;
hideControls?: DataStoryProps['hideControls'];
setShowRun: (showRun: boolean) => void;
setShowAddNode: (showAddNode: boolean) => void;
slotComponents?: React.ReactNode[];
Expand All @@ -52,24 +53,42 @@ export function DataStoryControls({
onSave: onSave
}), [updateDiagram, toDiagram]);

if (hideControls) return null;
const handleSave = useCallback(() => {
onSave?.();
}, [onSave]);

if (hideControls === true) return null;

return <Controls position={'top-left'} showInteractive={false} showZoom={false} showFitView={false}>
<ControlButton
{[<ControlButton
title="Run"
aria-label="Run"
aria-label="run"
key="run"
onClick={() => setShowRun(true)}
>
<RunIcon/>
</ControlButton>
</ControlButton>,
<ControlButton
onClick={() => setShowAddNode(true)}
title="Add Node"
data-cy="add-node-button"
aria-label="Add Node"
key="addNode"
aria-label="addNode"
>
<AddNodeIcon/>
</ControlButton>
</ControlButton>,
<ControlButton
title="Save"
aria-label="save"
key="save"
onClick={handleSave}>
<SaveIcon/>
</ControlButton>].filter((ControlButton) => {
if (Array.isArray(hideControls)) {
return !hideControls.includes(ControlButton.props['aria-label']);
}
return true;
})}

<DataStoryControlsContext.Provider value={context}>
{(slotComponents || []).map((component, index) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum DataStoryEvents {
'RUN_SUCCESS' = 'RUN_SUCCESS',
'RUN_ERROR' = 'RUN_ERROR',
'SAVE_SUCCESS' = 'SAVE_SUCCESS',
'SAVE_ERROR' = 'SAVE_ERROR',
}

export type DataStoryEventType = {
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/src/components/DataStory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ export interface ClientRunParams {
}

export type AcitvityBarType = 'node' | 'diagram' | 'settings' | 'explorer';

type ControlsType = 'run' | 'addNode' | 'save';
export type DataStoryProps = {
children?: React.ReactNode;
client: WorkspaceApiClient,
initDiagram?: Diagram | null;
hideControls?: boolean
hideControls?: boolean | ControlsType[];
slotComponents?: React.ReactNode[];
observers?: DataStoryObservers;
onInitialize?: DataStoryCallback;
Expand Down

0 comments on commit 243176a

Please sign in to comment.