Skip to content

Commit

Permalink
refact code and adjust ui
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceunx committed May 21, 2024
1 parent 3f39c80 commit 389c887
Show file tree
Hide file tree
Showing 17 changed files with 628 additions and 701 deletions.
14 changes: 3 additions & 11 deletions src/components/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ const defaultEdgeOptions = {
},
};

export default function Chart({
handleSelect,
content,
}: {
handleSelect: (node: Node | null) => void;
content?: string;
}) {
export default function Chart({ content }: { content?: string }) {
let { showToast } = useToast();
const [nodes, setNodes, onNodesChange] = useNodesState([]);
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
Expand All @@ -54,14 +48,13 @@ export default function Chart({

const onNodeClick = useCallback(
(_: MouseEvent, node: Node) => {
handleSelect(node);
if (node.type == "reactionNode") {
setDelKey("Delete");
} else {
setDelKey("");
}
},
[handleSelect, setDelKey],
[setDelKey],
);

useEffect(() => {
Expand Down Expand Up @@ -98,7 +91,6 @@ export default function Chart({

const onNodesDelete = useCallback(
(deleted: any) => {
handleSelect(null);
let incomes = deleted;
let removeNodes: Node[] = [];
let removeNodeIds: string[] = [];
Expand All @@ -122,7 +114,7 @@ export default function Chart({
setNodes(remainingNodes);
setDelKey("");
},
[nodes, edges, setNodes, setEdges, handleSelect],
[nodes, edges, setNodes, setEdges],
);

return (
Expand Down
42 changes: 24 additions & 18 deletions src/components/ChemNode.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import { memo } from 'react'
import { Flex } from '@radix-ui/themes'
import { Handle, Position, NodeProps } from 'reactflow'
import Image from 'next/image'
import { ChemNodeData } from '@/types'
import { memo } from "react";
import { Flex } from "@radix-ui/themes";
import { Handle, Position, NodeProps, NodeToolbar } from "reactflow";
import Image from "next/image";
import { ChemNodeData } from "@/types";
import ChemicalDetail from "./_ChemicalDetail";
import SynthesisDetail from "./_SynthesisDetail";

function ChemNode({ data }: NodeProps<ChemNodeData>) {
function ChemNode({ data, id }: NodeProps<ChemNodeData>) {
const handleStyle = {
width: '2px',
height: '10px',
borderRadius: '1px',
backgroundColor: '#778899',
}
width: "2px",
height: "10px",
borderRadius: "1px",
backgroundColor: "#778899",
};

return (
<Flex
direction='column'
className='relative bg-green-100 w-20 h-20 p-1 rounded-md hover:bg-green-300'
direction="column"
className="relative bg-green-100 w-20 h-20 p-1 rounded-md hover:bg-green-300"
>
<Handle style={handleStyle} type='target' position={Position.Left} />
<Image src={data.imgUrl} fill alt='chem' />
<NodeToolbar>
<SynthesisDetail smiles={data.smiles} id={id} />
<ChemicalDetail smiles={data.smiles} />
</NodeToolbar>
<Handle style={handleStyle} type="target" position={Position.Left} />
<Image src={data.imgUrl} fill alt="chem" />
{!data.isTarget && (
<Handle style={handleStyle} type='source' position={Position.Right} />
<Handle style={handleStyle} type="source" position={Position.Right} />
)}
</Flex>
)
);
}
export default memo(ChemNode)
export default memo(ChemNode);
8 changes: 4 additions & 4 deletions src/components/Condition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const Condition: React.FC<any> = ({ condition, idx }) => {
// eslint-disable-next-line
}, []);
return (
<Table.Row className="w-full">
<Table.Row className="w-full h-fit justify-center align-middle">
<Table.RowHeaderCell>{condition.score.toFixed(2)}</Table.RowHeaderCell>
<Table.Cell>
{reagent && (
Expand All @@ -45,7 +45,7 @@ const Condition: React.FC<any> = ({ condition, idx }) => {
alt="reagent"
width={70}
height={70}
className="bg-green-200 rounded-full p-2"
className="bg-green-200 rounded-md p-2"
/>
)}
</Table.Cell>
Expand All @@ -56,7 +56,7 @@ const Condition: React.FC<any> = ({ condition, idx }) => {
alt="solvent"
width={70}
height={70}
className="bg-white rounded-full p-2"
className="bg-white rounded-md p-2"
/>
)}
</Table.Cell>
Expand All @@ -67,7 +67,7 @@ const Condition: React.FC<any> = ({ condition, idx }) => {
alt="catalyst"
width={70}
height={70}
className="bg-yellow-200 rounded-full p-2"
className="bg-yellow-200 rounded-md p-2"
/>
)}
</Table.Cell>
Expand Down
Empty file removed src/components/ConditionDetail.tsx
Empty file.
84 changes: 0 additions & 84 deletions src/components/Conditions.tsx

This file was deleted.

87 changes: 4 additions & 83 deletions src/components/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,109 +1,30 @@
import { useState } from "react";
import { Flex, Separator, Text } from "@radix-ui/themes";
import { Flex, Separator } from "@radix-ui/themes";
import Search from "./Search";
import Chart from "./Chart";
import { Node } from "reactflow";
import Reactions from "./Reactions";
import NodeDetail from "./NodeDetail";
import RouteDetail from "./RouteDetail";
import Conditions from "./Conditions";
import { ToastProvider } from "./CustomToast";
import { useCurrentLocale } from "next-i18n-router/client";
import i18nConfig from "../../i18nConfig";

type Props = {
content?: string;
};

export default function Dashboard(props: Props) {
const locale = useCurrentLocale(i18nConfig);
const [currentNode, setCurrentNode] = useState<Node | null>(null);
const [routes, setRoutes] = useState([]);
const [conditions, setConditions] = useState([]);
const [selectCondition, setSelectCondition] = useState<any>({});

const handleSelect = (node: Node | null) => {
setRoutes([]);
setConditions([]);
setSelectCondition({});
setCurrentNode(node);
};

return (
<Flex direction="column" width="100%" className="h-full">
{!props.content && (
<>
<Search
setRoutes={setRoutes}
setConditions={setConditions}
setCurrentNode={setCurrentNode}
/>
<Search />
<Separator
orientation="horizontal"
size="4"
className="bg-gray-500"
/>
</>
)}
<Flex width="100%" className="h-1/2">
<Flex width="100%" className="h-full">
<ToastProvider>
<Chart handleSelect={handleSelect} content={props.content} />
<Chart content={props.content} />
</ToastProvider>
</Flex>
<Separator orientation="horizontal" size="4" className="bg-gray-500" />
<Flex className="h-1/2" width="100%" direction="row">
<Flex className="w-3/4 h-full" direction="column">
{Boolean(routes.length) && (
<Reactions
routes={routes}
currentNode={currentNode}
locale={locale!}
/>
)}
{Boolean(conditions.length) && (
<Conditions
conditions={conditions}
currentNode={currentNode}
setSelectCondition={setSelectCondition}
locale={locale!}
/>
)}
</Flex>
<Flex
className="w-1/4 ml-2 h-full"
align="center"
direction="column"
gap="2"
style={{ backgroundColor: "var(--gray-a4)" }}
>
<Text align="center" className="text-gray-300">
{currentNode
? locale === "en"
? "Current Target"
: "当前目标"
: locale === "en"
? "No Target"
: "未选中目标"}{" "}
</Text>
<Flex className="w-64" direction="column">
{currentNode && currentNode.type === "chemNode" && (
<NodeDetail
setRoutes={setRoutes}
currentNode={currentNode}
locale={locale!}
/>
)}
{currentNode && currentNode.type === "reactionNode" && (
<RouteDetail
setConditions={setConditions}
currentNode={currentNode}
selectCondition={selectCondition}
locale={locale!}
/>
)}
</Flex>
</Flex>
</Flex>
</Flex>
);
}
54 changes: 0 additions & 54 deletions src/components/NodeDetail.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/Reaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Reaction: React.FC<any> = ({ route, target, idx }) => {
}, []);

return (
<Table.Row className='w-full h-36'>
<Table.Row className="w-full h-fit justify-center align-middle">
<Table.RowHeaderCell>{route.plausibility.toFixed(2)}</Table.RowHeaderCell>
<Table.Cell>
{svg && (
Expand Down
Loading

0 comments on commit 389c887

Please sign in to comment.