Skip to content

Commit

Permalink
Feat: Sutomisable cytoscape gaph
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNik committed Feb 12, 2025
1 parent cacb8db commit 53da35f
Show file tree
Hide file tree
Showing 11 changed files with 1,105 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
commonjs: true,
es6: true,
},
ignorePatterns: ["!**/.server", "!**/.client"],
ignorePatterns: ["!**/.server", "!**/.client", "app/components/ui/*"],

// Base config
extends: ["eslint:recommended"],
Expand Down
563 changes: 563 additions & 0 deletions app/components/cytoscape-configurator.tsx

Large diffs are not rendered by default.

68 changes: 59 additions & 9 deletions app/components/cytoscape-graph.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,71 @@
import React, { useEffect, useRef, useState } from "react";
import cytoscape from "cytoscape";
import { cn } from "~/lib/utils";
import { CytoscapeJsonStructure, nodeData } from "~/lib/typings/cytoscape";
import { CytoscapeJsonStructure, nodeData, singleNodeData, UserConfig } from "~/lib/typings/cytoscape";
import { Dialog, DialogContent, DialogTrigger } from "~/components/ui/dialog";
import { getCytoscapeConfig } from "~/config/cytoscape";
import { Separator } from "./ui/separator";
import { getCytoscapeConfig, defaultConfig } from "~/config/cytoscape";
import { Separator } from "~/components/ui/separator";
import CytoscapeConfigurator from "~/components/cytoscape-configurator";

// These imports dont have any typings!
// @ts-expect-error
import avsdf from "cytoscape-avsdf"
// @ts-expect-error
import cola from "cytoscape-cola"
// @ts-expect-error
import dagre from "cytoscape-dagre"
// @ts-expect-error
import elk from "cytoscape-elk"
// @ts-expect-error
import klay from "cytoscape-klay"


const LOCAL_STORAGE_KEY = "cytoscape_configs";

interface GraphProps {
data: CytoscapeJsonStructure;
onNodeClick: (nodeData: nodeData) => void;
onNodeClick: (nodeData: singleNodeData) => void;
}

const CytoscapeGraph: React.FC<GraphProps> = ({ data, onNodeClick }) => {
const cyRef = useRef<HTMLDivElement>(null);
const [selectedNode, setSelectedNode] = useState<nodeData | null>(null);
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [userConfig, setUserConfig] = useState<UserConfig>(defaultConfig);

useEffect(() => {
const savedConfigs = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY) || "[]");
const selectedConfigName = localStorage.getItem("selected_cytoscape_config");

let initialConfig = defaultConfig;

if (selectedConfigName) {
const foundConfig = savedConfigs.find((c: UserConfig) => c.name === selectedConfigName);
if (foundConfig) {
initialConfig = foundConfig;
}
}

setUserConfig(initialConfig);
}, []);

useEffect(() => {
if (userConfig.name && userConfig.name !== "Default") {
localStorage.setItem("selected_cytoscape_config", userConfig.name);
}
}, [userConfig]);

useEffect(() => {
if (!cyRef.current) return;
if (!cyRef.current || !data) return;

cytoscape.use(avsdf);
cytoscape.use(cola);
cytoscape.use(dagre);
cytoscape.use(klay);
cytoscape.use(elk);

const config = getCytoscapeConfig(cyRef.current, data);
const cy = cytoscape(config);
const fullConfig = getCytoscapeConfig(cyRef.current, data, userConfig);
const cy = cytoscape(fullConfig);

cy.on("tap", "node", (event) => {
const nodeData = event.target.data();
Expand All @@ -32,16 +77,21 @@ const CytoscapeGraph: React.FC<GraphProps> = ({ data, onNodeClick }) => {
return () => {
cy.destroy();
};
}, [data, onNodeClick]);
}, [data, userConfig, onNodeClick]);

return (
<>
<div
className={cn(
"w-full h-[1000px] border border-gray-300 rounded-md shadow-md bg-muted"
"w-full min-h-[100vh] border border-gray-300 rounded-md shadow-md bg-muted mx-auto"
)}
ref={cyRef}
/>
<CytoscapeConfigurator
currentConfig={userConfig}
onConfigChange={setUserConfig}
defaultConfig={defaultConfig}
/>
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
<DialogTrigger asChild>
<button className="hidden" aria-hidden="true">
Expand Down
2 changes: 1 addition & 1 deletion app/components/settings/configured-hosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function HostList({ hosts }: { hosts: string[] }) {

export default function ConfiguredHosts() {
const data = useLoaderData<{ configuredHosts: Promise<string[]> }>();
const [error, setError] = useState<string | null>(null);
const [error,] = useState<string | null>(null);

return (
<Suspense
Expand Down
139 changes: 139 additions & 0 deletions app/components/ui/alert-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import * as React from "react"
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"

import { cn } from "~/lib/utils"
import { buttonVariants } from "~/components/ui/button"

const AlertDialog = AlertDialogPrimitive.Root

const AlertDialogTrigger = AlertDialogPrimitive.Trigger

const AlertDialogPortal = AlertDialogPrimitive.Portal

const AlertDialogOverlay = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
ref={ref}
/>
))
AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName

const AlertDialogContent = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
>(({ className, ...props }, ref) => (
<AlertDialogPortal>
<AlertDialogOverlay />
<AlertDialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
/>
</AlertDialogPortal>
))
AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName

const AlertDialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-2 text-center sm:text-left",
className
)}
{...props}
/>
)
AlertDialogHeader.displayName = "AlertDialogHeader"

const AlertDialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
AlertDialogFooter.displayName = "AlertDialogFooter"

const AlertDialogTitle = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Title
ref={ref}
className={cn("text-lg font-semibold", className)}
{...props}
/>
))
AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName

const AlertDialogDescription = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
AlertDialogDescription.displayName =
AlertDialogPrimitive.Description.displayName

const AlertDialogAction = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Action>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Action
ref={ref}
className={cn(buttonVariants(), className)}
{...props}
/>
))
AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName

const AlertDialogCancel = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Cancel>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Cancel>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Cancel
ref={ref}
className={cn(
buttonVariants({ variant: "outline" }),
"mt-2 sm:mt-0",
className
)}
{...props}
/>
))
AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName

export {
AlertDialog,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
}
1 change: 0 additions & 1 deletion app/config/columns.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use client";
import type { ColumnDef } from "@tanstack/react-table";

export type ContainerData = {
Expand Down
Loading

0 comments on commit 53da35f

Please sign in to comment.