Guest User

Untitled

a guest
May 3rd, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 2.34 KB | Source Code | 0 0
  1.  
  2. type ChildrenProps = {
  3.   node: Node;
  4. };
  5.  
  6. export function Children({ node }: ChildrenProps) {
  7.   return node.children.map((c) => (
  8.     <Item key={c.path + "/" + c.name} node={c} />
  9.   ));
  10. }
  11.  
  12. type ItemProps = ChildrenProps & {
  13.   node: Child;
  14. };
  15.  
  16. function Item({ node }: ItemProps) {
  17.   const [open, setOpen] = useState(false);
  18.   const path = node.path;
  19.   const uri = monaco.Uri.parse(
  20.     `file:///${path.startsWith("/") ? path.slice(1) : path}`
  21.   ).toString();
  22.  
  23.    const {
  24.     setSelectedDir,
  25.     setSelectedFile,
  26.     selectedFile,
  27.     selectedDir
  28.   } = useItemStore()
  29.  
  30.   const isSelected = selectedFile ? selectedFile === uri : selectedDir === path;
  31.   return (
  32.     <>
  33.       <div
  34.         key={node.path}
  35.         onClick={() => {
  36.           if (node.isDir) {
  37.             console.log("toggling open ", path, open);
  38.             setOpen((o) => !o);
  39.             setSelectedDir(path);
  40.           } else {
  41.             const childUri = monaco.Uri.parse(
  42.               `file:///${path.startsWith("/") ? path.slice(1) : path}`
  43.             ).toString();
  44.             setSelectedFile(childUri);
  45.           }
  46.         }}
  47.         className={cn(
  48.           `flex items-center transition-all ease-out`,
  49.           `hover:cursor-pointer hover:bg-gray-900`
  50.         )}
  51.         style={{ paddingLeft: `${node.depth * 16}px` }}
  52.       >
  53.         <span className="flex w-[32px] h-[32px] items-center justify-center">
  54.           {getIcon(node.name.split(".").pop() || "", node.name, {
  55.             isDir: node.isDir,
  56.             open,
  57.           })}
  58.         </span>
  59.  
  60.         <span style={{ marginLeft: 1, marginBottom: 3 }}>{node.name}</span>
  61.       </div>
  62.  
  63.       {open && node.isDir && <Nested node={node} />}
  64.     </>
  65.   );
  66. }
  67.  
  68. function Nested({ node: dir }: ItemProps) {
  69.   const { data, isLoading } = useWSQuery(["GENERATE_TREE", dir.path]);
  70.  
  71.   const useFullData = useMemo(() => {
  72.     if (data) {
  73.       const cloned = { ...data };
  74.  
  75.       cloned.children.forEach((c) => {
  76.         c.depth = (dir.depth !== undefined ? dir.depth : 0) + 1;
  77.       });
  78.  
  79.       return cloned;
  80.     }
  81.   }, [data, dir]);
  82.  
  83.   if (isLoading) {
  84.     // Maybe query is still loading..
  85.     return null;
  86.   }
  87.  
  88.   if (!data || !useFullData) {
  89.     return <p>error fetching contents for {dir.path}</p>;
  90.   }
  91.  
  92.   return <Children key={dir.path + Math.random()} node={useFullData} />;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment