delete tabs working

This commit is contained in:
anovazzi1 2023-02-22 18:18:45 -03:00
commit 41e5052da1
4 changed files with 45 additions and 21 deletions

View file

@ -6,7 +6,7 @@ type TabsContextType={
tabIndex:number; tabIndex:number;
setTabIndex:(index:number)=>void; setTabIndex:(index:number)=>void;
flows:Array<flow> flows:Array<flow>
removeFlow:(index:number)=>void; removeFlow:(id:string)=>void;
addFlow:(newFlow:flow)=>void; addFlow:(newFlow:flow)=>void;
updateFlow:(newFLow:flow)=>void; updateFlow:(newFLow:flow)=>void;
} }
@ -15,7 +15,7 @@ const TabsContextInitialValue = {
tabIndex : 0, tabIndex : 0,
setTabIndex:(index:number)=>{}, setTabIndex:(index:number)=>{},
flows:[], flows:[],
removeFlow:(index:number)=>{}, removeFlow:(id:string)=>{},
addFlow:(newFlow:flow)=>{}, addFlow:(newFlow:flow)=>{},
updateFlow:(newFLow:flow)=>{} updateFlow:(newFLow:flow)=>{}
@ -27,11 +27,24 @@ export const TabsContext = createContext<TabsContextType>(TabsContextInitialValu
export function TabsProvider({children}){ export function TabsProvider({children}){
const [tabIndex,setTabIndex] = useState(0) const [tabIndex,setTabIndex] = useState(0)
const [flows,setFlows] = useState<Array<flow>>([]) const [flows,setFlows] = useState<Array<flow>>([])
function removeFlow(index:number){ function removeFlow(id:string){
let newFlows = flows setFlows(prevState=>{
const newFlows = [...prevState];
const index = newFlows.findIndex(flow=>flow.id===id)
if(index!==-1){
newFlows.splice(index,1) newFlows.splice(index,1)
window.sessionStorage.setItem('tabs',JSON.stringify(newFlows)) }
setFlows(newFlows) if(index===tabIndex){
if(tabIndex===0){
//
}
else{
setTabIndex(tabIndex-1)
}
}
window.sessionStorage.setItem('tabs', JSON.stringify(newFlows));
return newFlows;
})
} }
function addFlow(newFlow: flow) { function addFlow(newFlow: flow) {
setFlows(prevState => { setFlows(prevState => {

View file

@ -1,3 +1,4 @@
import { PlusIcon } from "@heroicons/react/24/outline";
import { useContext, useEffect, useState } from "react"; import { useContext, useEffect, useState } from "react";
import { Tab, Tabs, TabList, TabPanel } from "react-tabs"; import { Tab, Tabs, TabList, TabPanel } from "react-tabs";
import { ReactFlowProvider } from "reactflow"; import { ReactFlowProvider } from "reactflow";
@ -19,12 +20,14 @@ export function TabsManager() {
<div className="h-full w-full flex flex-col"> <div className="h-full w-full flex flex-col">
<div className="w-full flex pr-2 flex-row text-center items-center"> <div className="w-full flex pr-2 flex-row text-center items-center">
{flows.map((flow, index) => { {flows.map((flow, index) => {
console.log(tabIndex)
return ( return (
<TabComponent selected={index === tabIndex} key={index}> <TabComponent selected={index === tabIndex} key={index} id={flow.id}>
<div onClick={() => setTabIndex(index)}>{flow.name}</div> <div onClick={() => setTabIndex(index)}>{flow.name}</div>
</TabComponent> </TabComponent>
); );
})} })}
<div onClick={()=>addFlow({ name: "untitled", data: null, id: _.uniqueId() })} className="cursor-pointer"><PlusIcon color="black" width={24}></PlusIcon></div>
</div> </div>
<div className="w-full h-full"> <div className="w-full h-full">
<ReactFlowProvider> <ReactFlowProvider>

View file

@ -1,16 +1,24 @@
import { XMarkIcon } from "@heroicons/react/24/solid"; import { XMarkIcon } from "@heroicons/react/24/solid";
import { useContext } from "react";
import { TabsContext } from "../../../../contexts/tabsContext";
import { classNames } from "../../../../utils"; import { classNames } from "../../../../utils";
type tabProps = { export default function TabComponent({ children, selected, id }) {
name:string; const { removeFlow, flows } = useContext(TabsContext);
} return (
<div
className={classNames(
export default function TabComponent({children,selected}){ selected ? " shadow-lg" : "bg-gray-300",
return( "flex border-t border-l border-r border-black rounded-t-md shadow-sm cursor-pointer"
<div className={classNames(selected?" shadow-lg":"bg-gray-300","flex border-t border-l border-r border-black rounded-t-md shadow-sm cursor-pointer")}> )}
>
{children} {children}
<XMarkIcon className="w-5 hover:text-red-500"></XMarkIcon> {flows.length > 1 && (
<XMarkIcon
className="w-5 hover:text-red-500"
onClick={() => removeFlow(id)}
></XMarkIcon>
)}
</div> </div>
) );
} }

View file

@ -50,7 +50,7 @@ export default function FlowPage({flow}) {
setViewport(flow.data.viewport) setViewport(flow.data.viewport)
} }
useEffect(()=>{ useEffect(()=>{
if(reactFlowInstance){ if(reactFlowInstance && flow){
flow.data =reactFlowInstance.toObject() flow.data =reactFlowInstance.toObject()
updateFlow(flow) updateFlow(flow)
} }