Skip to content

refactor: node type CODE, SCOPE, RICH in front-end #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions ui/src/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { lowercase, numbers } from "nanoid-dictionary";
import { useStore } from "zustand";

import { RepoContext } from "../lib/store";
import { dbtype2nodetype, nodetype2dbtype } from "../lib/utils";
import { useEdgesYjsObserver, useYjsObserver } from "../lib/nodes";

import { useApolloClient } from "@apollo/client";
Expand All @@ -46,7 +45,7 @@ import { YMap } from "yjs/dist/src/types/YMap";
import FloatingEdge from "./nodes/FloatingEdge";
import CustomConnectionLine from "./nodes/CustomConnectionLine";

const nodeTypes = { scope: ScopeNode, code: CodeNode, rich: RichNode };
const nodeTypes = { SCOPE: ScopeNode, CODE: CodeNode, RICH: RichNode };
const edgeTypes = {
floating: FloatingEdge,
};
Expand All @@ -62,7 +61,7 @@ function store2nodes(id: string, { getId2children, getPod }) {
if (id !== "ROOT") {
res.push({
id: id,
type: dbtype2nodetype(pod.type),
type: pod.type,
data: {
// label: `ID: ${id}, parent: ${pods[id].parent}, pos: ${pods[id].x}, ${pods[id].y}`,
label: id,
Expand Down Expand Up @@ -535,7 +534,7 @@ function CanvasImpl() {
};

const onNodeContextMenu = (event, node) => {
if (node?.type !== "scope") return;
if (node?.type !== "SCOPE") return;

event.preventDefault();
setShowContextMenu(true);
Expand Down Expand Up @@ -651,8 +650,8 @@ function CanvasImpl() {
<MiniMap
nodeStrokeColor={(n) => {
if (n.style?.borderColor) return n.style.borderColor;
if (n.type === "code") return "#d6dee6";
if (n.type === "scope") return "#f4f6f8";
if (n.type === "CODE") return "#d6dee6";
if (n.type === "SCOPE") return "#f4f6f8";

return "#d6dee6";
}}
Expand All @@ -673,17 +672,17 @@ function CanvasImpl() {
x={points.x}
y={points.y}
addCode={() =>
addNode("code", project({ x: client.x, y: client.y }), parentNode)
addNode("CODE", project({ x: client.x, y: client.y }), parentNode)
}
addScope={() =>
addNode(
"scope",
"SCOPE",
project({ x: client.x, y: client.y }),
parentNode
)
}
addRich={() =>
addNode("rich", project({ x: client.x, y: client.y }), parentNode)
addNode("RICH", project({ x: client.x, y: client.y }), parentNode)
}
onShareClick={() => {
setShareOpen(true);
Expand Down
50 changes: 36 additions & 14 deletions ui/src/lib/fetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ export async function doRemoteLoadRepo(client: ApolloClient<any>, id: string) {
}
}

/**
* For historical reason, the backend DB schema pod.type are "CODE", "DECK",
* "WYSIWYG", while the node types in front-end are "CODE", "SCOPE", "RICH".
*/

function dbtype2nodetype(dbtype: string) {
switch (dbtype) {
case "CODE":
return "CODE";
case "DECK":
return "SCOPE";
case "WYSIWYG":
return "RICH";
default:
throw new Error(`unknown dbtype ${dbtype}`);
}
}

function nodetype2dbtype(nodetype: string) {
switch (nodetype) {
case "CODE":
return "CODE";
case "SCOPE":
return "DECK";
case "RICH":
return "WYSIWYG";
default:
throw new Error(`unknown nodetype ${nodetype}`);
}
}

export function normalize(pods) {
const res: { [key: string]: Pod } = {
ROOT: {
Expand All @@ -108,7 +139,7 @@ export function normalize(pods) {
// Adding this to avoid errors
// XXX should I save these to db?
lang: "python",
type: "DECK",
type: "SCOPE",
content: "",
x: 0,
y: 0,
Expand Down Expand Up @@ -141,29 +172,20 @@ export function normalize(pods) {
type: res[id].type,
}))
: [];
// change children.id format
// UDPATE Or, I just put {id,type} in the children array
//
// pod.children = pod.children.map(({ id }) => id);
//
// sort according to index
// pod.children.sort((a, b) => res[a.id].index - res[b.id].index);
// if (pod.type === "WYSIWYG" || pod.type === "CODE") {
// pod.content = JSON.parse(pod.content);
// }
pod.content = JSON.parse(pod.content);
pod.staged = JSON.parse(pod.staged);
pod.githead = JSON.parse(pod.githead);
pod.type = dbtype2nodetype(pod.type);
if (pod.result) {
pod.result = JSON.parse(pod.result);
}
if (pod.error) {
pod.error = JSON.parse(pod.error);
}
// DEBUG the deck's content seems to be a long string of escaped \
if (pod.type === "DECK" && pod.content) {
if (pod.type === "SCOPE" && pod.content) {
console.log(
`warning: deck ${pod.id} content is not null, setting to null:`,
`warning: SCOPE ${pod.id} content is not null, setting to null:`,
pod.content
);
pod.content = null;
Expand Down Expand Up @@ -195,7 +217,7 @@ function serializePodInput(pod) {
height,
}) => ({
id,
type,
type: nodetype2dbtype(type),
column,
lang,
// stdout,
Expand Down
3 changes: 1 addition & 2 deletions ui/src/lib/nodes.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useCallback, useEffect, useState, useContext } from "react";
import { applyNodeChanges, Edge, Node } from "reactflow";
import { RepoContext } from "./store";
import { nodetype2dbtype } from "./utils";
import { useStore } from "zustand";
import { useApolloClient } from "@apollo/client";
import { Transaction, YEvent } from "yjs";
Expand Down Expand Up @@ -30,7 +29,7 @@ export function useYjsObserver() {
id: node.id,
children: [],
parent: "ROOT",
type: nodetype2dbtype(node.type || ""),
type: node.type as "CODE" | "SCOPE" | "RICH",
lang: "python",
x: node.position.x,
y: node.position.y,
Expand Down
20 changes: 10 additions & 10 deletions ui/src/lib/store/canvasSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Transaction, YEvent } from "yjs";

import { match, P } from "ts-pattern";

import { myNanoId, nodetype2dbtype, dbtype2nodetype } from "../utils";
import { myNanoId } from "../utils";

import {
Connection,
Expand Down Expand Up @@ -69,14 +69,14 @@ function createTemporaryNode(pod, position, parent = "ROOT", level = 0): any {
width: pod.width,
};

if (pod.type === "DECK") {
if (pod.type === "SCOPE") {
style["height"] = pod.height!;
style["backgroundColor"] = level2color[level] || level2color["default"];
}

const newNode = {
id,
type: dbtype2nodetype(pod.type),
type: pod.type,
position,
data: {
label: id,
Expand Down Expand Up @@ -108,13 +108,13 @@ function createTemporaryNode(pod, position, parent = "ROOT", level = 0): any {
/**
* The new reactflow nodes for context-menu's addXXX items.
*/
function createNewNode(type: "scope" | "code" | "rich", position): Node {
function createNewNode(type: "SCOPE" | "CODE" | "RICH", position): Node {
let id = myNanoId();
const newNode = {
id,
type,
position,
...(type === "scope"
...(type === "SCOPE"
? {
width: 600,
height: 600,
Expand Down Expand Up @@ -154,7 +154,7 @@ function getScopeAt(
const scope = nodes.findLast((node) => {
let [x1, y1] = getAbsPos(node, nodesMap);
return (
node.type === "scope" &&
node.type === "SCOPE" &&
x >= x1 &&
!excludes.includes(node.id) &&
x <= x1 + node.width &&
Expand Down Expand Up @@ -244,7 +244,7 @@ export interface CanvasSlice {
setPaneBlur: () => void;

addNode: (
type: "code" | "scope" | "rich",
type: "CODE" | "SCOPE" | "RICH",
position: XYPosition,
parent: string
) => void;
Expand Down Expand Up @@ -358,7 +358,7 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
style: {
...node.style,
backgroundColor:
node.type === "scope" ? level2color[node.data.level] : undefined,
node.type === "SCOPE" ? level2color[node.data.level] : undefined,
},
selected: selectedPods.has(node.id),
// className: get().dragHighlight === node.id ? "active" : "",
Expand Down Expand Up @@ -392,7 +392,7 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
id: node.id,
children: [],
parent: "ROOT",
type: nodetype2dbtype(node.type || ""),
type: node.type as "CODE" | "SCOPE" | "RICH",
lang: "python",
x: node.position.x,
y: node.position.y,
Expand Down Expand Up @@ -910,7 +910,7 @@ function fitChildren(
node2children,
nodesMap
): null | { x: number; y: number; width: number; height: number } {
if (node.type !== "scope") return null;
if (node.type !== "SCOPE") return null;
// This is a scope node. Get all its children and calculate the x,y,width,height to tightly fit its children.
let children = node2children.get(node.id);
// If no children, nothing is changed.
Expand Down
2 changes: 1 addition & 1 deletion ui/src/lib/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enableMapSet();
export type Pod = {
id: string;
name?: string;
type: string;
type: "CODE" | "SCOPE" | "RICH";
content?: string;
dirty?: boolean;
isSyncing?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion ui/src/lib/store/runtimeSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export const createRuntimeSlice: StateCreator<MyState, [], [], RuntimeSlice> = (
get().clearResults(id);
get().setRunning(id);
set({ chain: [...get().chain, id] });
} else if (get().pods[id].type === "DECK") {
} else if (get().pods[id].type === "SCOPE") {
// If this pod is a scope, run all pods inside a scope by geographical order.
// get the pods in the scope
let children = get().node2children.get(id);
Expand Down
33 changes: 0 additions & 33 deletions ui/src/lib/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,4 @@ export function getUpTime(startedAt: string) {
return prettyTime;
}

/**
* For historical reason, the state.pod.type and DB schema pod.type are "CODE",
* "DECK", "WYSIWYG", while the node types in react-flow are "code", "scope",
* "rich". These two functions document this and handle the conversion.
* @param dbtype
* @returns
*/
export function dbtype2nodetype(dbtype: string) {
switch (dbtype) {
case "CODE":
return "code";
case "DECK":
return "scope";
case "WYSIWYG":
return "rich";
default:
throw new Error(`unknown dbtype ${dbtype}`);
}
}

export function nodetype2dbtype(nodetype: string) {
switch (nodetype) {
case "code":
return "CODE";
case "scope":
return "DECK";
case "rich":
return "WYSIWYG";
default:
throw new Error(`unknown nodetype ${nodetype}`);
}
}

export const myNanoId = customAlphabet(lowercase + numbers, 20);