File size: 5,349 Bytes
baa4c21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as DropdownMenu from '@radix-ui/react-dropdown-menu';
import { AnimatePresence, motion, type Variants } from 'framer-motion';
import { memo, useEffect, useRef, useState } from 'react';
import type { FileMap } from '~/lib/stores/files';
import { classNames } from '~/utils/classNames';
import { WORK_DIR } from '~/utils/constants';
import { cubicEasingFn } from '~/utils/easings';
import { renderLogger } from '~/utils/logger';
import FileTree from './FileTree';

const WORK_DIR_REGEX = new RegExp(`^${WORK_DIR.split('/').slice(0, -1).join('/').replaceAll('/', '\\/')}/`);

interface FileBreadcrumbProps {
  files?: FileMap;
  pathSegments?: string[];
  onFileSelect?: (filePath: string) => void;
}

const contextMenuVariants = {
  open: {
    y: 0,
    opacity: 1,
    transition: {
      duration: 0.15,
      ease: cubicEasingFn,
    },
  },
  close: {
    y: 6,
    opacity: 0,
    transition: {
      duration: 0.15,
      ease: cubicEasingFn,
    },
  },
} satisfies Variants;

export const FileBreadcrumb = memo<FileBreadcrumbProps>(({ files, pathSegments = [], onFileSelect }) => {
  renderLogger.trace('FileBreadcrumb');

  const [activeIndex, setActiveIndex] = useState<number | null>(null);

  const contextMenuRef = useRef<HTMLDivElement | null>(null);
  const segmentRefs = useRef<(HTMLSpanElement | null)[]>([]);

  const handleSegmentClick = (index: number) => {
    setActiveIndex((prevIndex) => (prevIndex === index ? null : index));
  };

  useEffect(() => {
    const handleOutsideClick = (event: MouseEvent) => {
      if (
        activeIndex !== null &&
        !contextMenuRef.current?.contains(event.target as Node) &&
        !segmentRefs.current.some((ref) => ref?.contains(event.target as Node))
      ) {
        setActiveIndex(null);
      }
    };

    document.addEventListener('mousedown', handleOutsideClick);

    return () => {
      document.removeEventListener('mousedown', handleOutsideClick);
    };
  }, [activeIndex]);

  if (files === undefined || pathSegments.length === 0) {
    return null;
  }

  return (
    <div className="flex">

      {pathSegments.map((segment, index) => {

        const isLast = index === pathSegments.length - 1;



        const path = pathSegments.slice(0, index).join('/');



        if (!WORK_DIR_REGEX.test(path)) {

          return null;

        }



        const isActive = activeIndex === index;



        return (

          <div key={index} className="relative flex items-center">

            <DropdownMenu.Root open={isActive} modal={false}>

              <DropdownMenu.Trigger asChild>

                <span

                  ref={(ref) => (segmentRefs.current[index] = ref)}

                  className={classNames('flex items-center gap-1.5 cursor-pointer shrink-0', {

                    'text-bolt-elements-textTertiary hover:text-bolt-elements-textPrimary': !isActive,

                    'text-bolt-elements-textPrimary underline': isActive,

                    'pr-4': isLast,

                  })}

                  onClick={() => handleSegmentClick(index)}

                >

                  {isLast && <div className="i-ph:file-duotone" />}

                  {segment}

                </span>

              </DropdownMenu.Trigger>

              {index > 0 && !isLast && <span className="i-ph:caret-right inline-block mx-1" />}

              <AnimatePresence>

                {isActive && (

                  <DropdownMenu.Portal>

                    <DropdownMenu.Content

                      className="z-file-tree-breadcrumb"

                      asChild

                      align="start"

                      side="bottom"

                      avoidCollisions={false}

                    >

                      <motion.div

                        ref={contextMenuRef}

                        initial="close"

                        animate="open"

                        exit="close"

                        variants={contextMenuVariants}

                      >

                        <div className="rounded-lg overflow-hidden">

                          <div className="max-h-[50vh] min-w-[300px] overflow-scroll bg-bolt-elements-background-depth-1 border border-bolt-elements-borderColor shadow-sm rounded-lg">

                            <FileTree

                              files={files}

                              hideRoot

                              rootFolder={path}

                              collapsed

                              allowFolderSelection

                              selectedFile={`${path}/${segment}`}

                              onFileSelect={(filePath) => {

                                setActiveIndex(null);

                                onFileSelect?.(filePath);

                              }}

                            />

                          </div>

                        </div>

                        <DropdownMenu.Arrow className="fill-bolt-elements-borderColor" />

                      </motion.div>

                    </DropdownMenu.Content>

                  </DropdownMenu.Portal>

                )}

              </AnimatePresence>

            </DropdownMenu.Root>

          </div>

        );

      })}

    </div>
  );
});