Skip to content

Commit

Permalink
WIP commit, now we are loading flowView node configs from plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Oct 8, 2023
1 parent ec613dd commit a591c30
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 52 deletions.
5 changes: 1 addition & 4 deletions src/main/ipc/invokes/getFileTypes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { IpcMainInvokeEvent } from 'electron';
import { PluginConfig } from 'shared/types/plugin';
import { FileTypeMap } from 'shared/types';
import project from 'main/project';

// TODO move it to shared types with a better name, Replace similar types with shared one
type FileTypeMap = { [name: string]: PluginConfig };

export default async function getFileTypes(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
event: IpcMainInvokeEvent,
Expand Down
16 changes: 12 additions & 4 deletions src/renderer/components/Tab/TabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import Box from '@mui/material/Box';
import TextField from '@mui/material/TextField';

import { ChannelsMain } from 'shared/types';
import { ChannelsMain, FileTypeMap } from 'shared/types';

import { useAppSelector, useAppDispatch } from 'renderer/state/store/index';
import {
Expand All @@ -23,11 +23,11 @@ export interface TabViewProps {
export default function TabView({ tabId }: TabViewProps) {
const dispatch = useAppDispatch();
// TODO use shard type for the state
const [fileTypes, setFileTypes] = React.useState<any | null>(null);
const [fileTypes, setFileTypes] = React.useState<FileTypeMap | null>(null);

React.useEffect(() => {
async function getFileTypes() {
const result: string[] = await window.electron.ipcRenderer.invoke(
const result: FileTypeMap = await window.electron.ipcRenderer.invoke(
ChannelsMain.getFileTypes
);
setFileTypes(result);
Expand All @@ -49,10 +49,18 @@ export default function TabView({ tabId }: TabViewProps) {
if (fileTypes) {
const fileTypePlugin = fileTypes[tabData.extension];
if (fileTypePlugin) {
return <FlowView tabId={tabId} setTabIsDirty={setTabIsDirty} />;
return (
<FlowView
tabId={tabId}
setTabIsDirty={setTabIsDirty}
config={fileTypePlugin.flowView}
/>
);
} else {
return <DefaultFileView tabData={tabData} />;
}
} else {
return undefined;
}
};

Expand Down
59 changes: 16 additions & 43 deletions src/renderer/components/Tab/TabViews/FlowView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { styled } from '@mui/material/styles';
import Box from '@mui/material/Box';

import { ChannelsMain } from 'shared/types';
import { FlowViewConfig } from 'shared/types/plugin';

import { useAppSelector } from 'renderer/state/store/index';
import { FileTabData } from 'renderer/state/types';
Expand All @@ -39,48 +40,10 @@ import ConversationNode from 'renderer/components/Nodes/ConversationNode';

import 'reactflow/dist/style.css';

const initialNodes = [
{
id: '1',
type: 'Plot',
data: { label: 'Node 1' },
position: { x: 0, y: 0 },
},
{
id: '2',
type: 'Plot',
data: { label: 'Node 2' },
position: { x: 400, y: 0 },
},
];

const edgeSharedSettings = {
markerEnd: { type: MarkerType.ArrowClosed, width: 6 },
};

const initialEdges = [
{
id: 'e1',
source: '1',
target: '2',
...edgeSharedSettings,
},
];

const nodeConfigs = [
{
type: 'Plot',
connections: [1, 1],
},
{
type: 'Note',
},
{
type: 'Conversation',
connections: [1, 1],
},
];

const nodeTypes = {
Plot: PlotNode,
Note: NoteNode,
Expand Down Expand Up @@ -131,9 +94,11 @@ type SetIsDirtyCallbackType = (isDirty: boolean) => void;
export interface FlowProps {
tabId: string;
setTabIsDirty: SetIsDirtyCallbackType;
config: FlowViewConfig;
}

function Flow({ tabId, setTabIsDirty }: FlowProps) {
function Flow({ tabId, setTabIsDirty, config }: FlowProps) {
const nodeConfigs = config.nodes;
const tabState = useAppSelector((state) =>
state.tabsState.tabs.find((tab) => tab.id === tabId)
);
Expand All @@ -150,8 +115,9 @@ function Flow({ tabId, setTabIsDirty }: FlowProps) {
setIsDirty: setTabIsDirty,
maxHistorySize: 100,
});
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);

const [nodes, setNodes, onNodesChange] = useNodesState([]);
const [edges, setEdges, onEdgesChange] = useEdgesState([]);

const [splash, setSplash] = React.useState<boolean>(true);
const [contextMenu, setContextMenu] = React.useState<{
Expand Down Expand Up @@ -278,6 +244,7 @@ function Flow({ tabId, setTabIsDirty }: FlowProps) {
}
},
[
nodeConfigs,
nodes,
setNodes,
setEdges,
Expand Down Expand Up @@ -392,6 +359,7 @@ function Flow({ tabId, setTabIsDirty }: FlowProps) {
if (!rclick) centerOnNode(newNode.id);
},
[
nodeConfigs,
contextMenu,
nodes,
setNodes,
Expand Down Expand Up @@ -473,12 +441,17 @@ function Flow({ tabId, setTabIsDirty }: FlowProps) {
export interface FlowViewProps {
tabId: string;
setTabIsDirty: SetIsDirtyCallbackType;
config: FlowViewConfig;
}

export default function FlowView({ tabId, setTabIsDirty }: FlowViewProps) {
export default function FlowView({
tabId,
setTabIsDirty,
config,
}: FlowViewProps) {
return (
<ReactFlowProvider>
<Flow tabId={tabId} setTabIsDirty={setTabIsDirty} />
<Flow tabId={tabId} setTabIsDirty={setTabIsDirty} config={config} />
</ReactFlowProvider>
);
}
3 changes: 3 additions & 0 deletions src/shared/types/fileTypeMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PluginConfig } from './plugin';

export type FileTypeMap = { [name: string]: PluginConfig };
2 changes: 2 additions & 0 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CreateNewProjectResult } from './createNewProjectResult';
import { BrowseFileSystemResult } from './browseFileSystemResult';
import { OpenProjectResult } from './openProjectResult';
import { ProjectTreeNode, ProjectTree } from './projectTree';
import { FileTypeMap } from './fileTypeMap';
import Logger from './logger';
import LogLevel from './logLevel';
import LogMessage, { formatLog } from './logMessage';
Expand All @@ -35,6 +36,7 @@ export type {
OpenProjectResult,
ProjectTreeNode,
ProjectTree,
FileTypeMap,
Logger,
LogMessage,
};
2 changes: 1 addition & 1 deletion src/shared/types/plugin/pluginConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type ConnectionInfo = {

export type NodeInfo = {
type: string;
connection: ConnectionInfo;
connections: ConnectionInfo;
};

export type FlowViewConfig = {
Expand Down

0 comments on commit a591c30

Please sign in to comment.