File size: 8,249 Bytes
61aec16
 
 
7068559
61aec16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d212ba6
61aec16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d212ba6
35a414b
61aec16
 
35a414b
61aec16
d212ba6
 
35a414b
7068559
35a414b
 
7068559
 
 
 
 
 
 
d212ba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61aec16
 
 
 
 
 
 
 
d212ba6
 
61aec16
 
 
d212ba6
 
 
61aec16
35a414b
61aec16
 
 
 
 
 
 
35a414b
61aec16
 
 
 
 
 
 
 
d212ba6
35a414b
61aec16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d212ba6
35a414b
d212ba6
61aec16
 
 
d212ba6
 
61aec16
35a414b
61aec16
 
 
 
 
d212ba6
61aec16
 
35a414b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61aec16
 
 
 
 
 
 
 
 
 
 
35a414b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61aec16
 
 
 
 
 
 
d212ba6
35a414b
61aec16
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
 * T077: Directory tree component with collapsible folders
 */
import React, { useState, useMemo, useEffect } from 'react';
import { ChevronRight, ChevronDown, Folder, File } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { ScrollArea } from '@/components/ui/scroll-area';
import type { NoteSummary } from '@/types/note';
import { cn } from '@/lib/utils';

interface TreeNode {
  name: string;
  path: string;
  type: 'file' | 'folder';
  children?: TreeNode[];
  note?: NoteSummary;
}

interface DirectoryTreeProps {
  notes: NoteSummary[];
  selectedPath?: string;
  onSelectNote: (path: string) => void;
  onMoveNote?: (oldPath: string, newFolderPath: string) => void;
}

/**
 * Build a tree structure from flat list of note paths
 */
function buildTree(notes: NoteSummary[]): TreeNode[] {
  const root: TreeNode = { name: '', path: '', type: 'folder', children: [] };

  for (const note of notes) {
    const parts = note.note_path.split('/');
    let current = root;

    // Navigate/create folders
    for (let i = 0; i < parts.length - 1; i++) {
      const folderName = parts[i];
      const folderPath = parts.slice(0, i + 1).join('/');
      
      let folder = current.children?.find(
        (child) => child.name === folderName && child.type === 'folder'
      );

      if (!folder) {
        folder = {
          name: folderName,
          path: folderPath,
          type: 'folder',
          children: [],
        };
        current.children = current.children || [];
        current.children.push(folder);
      }

      current = folder;
    }

    // Add file
    const fileName = parts[parts.length - 1];
    current.children = current.children || [];
    current.children.push({
      name: fileName,
      path: note.note_path,
      type: 'file',
      note,
    });
  }

  // Sort children: folders first, then files, alphabetically
  const sortChildren = (node: TreeNode) => {
    if (node.children) {
      node.children.sort((a, b) => {
        if (a.type !== b.type) {
          return a.type === 'folder' ? -1 : 1;
        }
        return a.name.localeCompare(b.name);
      });
      node.children.forEach(sortChildren);
    }
  };

  sortChildren(root);

  return root.children || [];
}

interface TreeNodeItemProps {
  node: TreeNode;
  depth: number;
  selectedPath?: string;
  onSelectNote: (path: string) => void;
  onMoveNote?: (oldPath: string, newFolderPath: string) => void;
  forceExpandState?: boolean;
}

function TreeNodeItem({ node, depth, selectedPath, onSelectNote, onMoveNote, forceExpandState }: TreeNodeItemProps) {
  const [isOpen, setIsOpen] = useState(depth < 2); // Auto-expand first 2 levels
  const [isDragOver, setIsDragOver] = useState(false);

  // T014: Use forceExpandState if provided, otherwise use local isOpen state
  // Sync local state with force state when it changes
  const effectiveIsOpen = forceExpandState ?? isOpen;

  // Update local state when force state is applied
  useEffect(() => {
    if (forceExpandState !== undefined) {
      setIsOpen(forceExpandState);
    }
  }, [forceExpandState]);

  const handleDragOver = (e: React.DragEvent) => {
    e.preventDefault();
    e.stopPropagation();
    if (node.type === 'folder') {
      setIsDragOver(true);
    }
  };

  const handleDragLeave = (e: React.DragEvent) => {
    e.preventDefault();
    e.stopPropagation();
    setIsDragOver(false);
  };

  const handleDrop = (e: React.DragEvent) => {
    e.preventDefault();
    e.stopPropagation();
    setIsDragOver(false);

    if (node.type === 'folder') {
      const draggedPath = e.dataTransfer.getData('application/note-path');
      if (draggedPath && onMoveNote) {
        onMoveNote(draggedPath, node.path);
      }
    }
  };

  const handleDragStart = (e: React.DragEvent) => {
    if (node.type === 'file') {
      e.dataTransfer.effectAllowed = 'move';
      e.dataTransfer.setData('application/note-path', node.path);
    }
  };

  if (node.type === 'folder') {
    return (
      <div>
        <Button
          variant="ghost"
          className={cn(
            "w-full justify-start font-normal px-2 h-8",
            "hover:bg-accent transition-colors duration-200",
            isDragOver && "bg-accent ring-2 ring-primary"
          )}
          style={{ paddingLeft: `${depth * 12 + 8}px` }}
          onClick={() => setIsOpen(!isOpen)}
          onDragOver={handleDragOver}
          onDragLeave={handleDragLeave}
          onDrop={handleDrop}
        >
          {effectiveIsOpen ? (
            <ChevronDown className="h-4 w-4 mr-1 shrink-0" />
          ) : (
            <ChevronRight className="h-4 w-4 mr-1 shrink-0" />
          )}
          <Folder className="h-4 w-4 mr-2 shrink-0 text-muted-foreground" />
          <span className="truncate">{node.name}</span>
        </Button>
        {effectiveIsOpen && node.children && (
          <div>
            {node.children.map((child) => (
              <TreeNodeItem
                key={child.path}
                node={child}
                depth={depth + 1}
                selectedPath={selectedPath}
                onSelectNote={onSelectNote}
                onMoveNote={onMoveNote}
                forceExpandState={forceExpandState}
              />
            ))}
          </div>
        )}
      </div>
    );
  }

  // File node
  const isSelected = node.path === selectedPath;
  // Remove .md extension for display
  const displayName = node.name.replace(/\.md$/, '');

  return (
    <Button
      variant="ghost"
      className={cn(
        "w-full justify-start font-normal px-2 h-8",
        "hover:bg-accent transition-colors duration-200",
        isSelected && "bg-accent transition-colors duration-300 animate-highlight-pulse",
        "cursor-move"
      )}
      style={{ paddingLeft: `${depth * 12 + 8}px` }}
      onClick={() => onSelectNote(node.path)}
      draggable
      onDragStart={handleDragStart}
    >
      <File className="h-4 w-4 mr-2 shrink-0 text-muted-foreground transition-colors duration-200" />
      <span className="truncate">{displayName}</span>
    </Button>
  );
}

export function DirectoryTree({ notes, selectedPath, onSelectNote, onMoveNote }: DirectoryTreeProps) {
  const tree = useMemo(() => buildTree(notes), [notes]);

  // T012: Add expandAll state to DirectoryTree component
  // T013: Add collapseAll state to DirectoryTree component
  // T015: Implement expand/collapse state propagation logic
  const [expandAllState, setExpandAllState] = useState<boolean | undefined>(undefined);

  const handleExpandAll = () => {
    setExpandAllState(true);
    // Reset after transition completes (300ms)
    setTimeout(() => {
      setExpandAllState(undefined);
    }, 300);
  };

  const handleCollapseAll = () => {
    setExpandAllState(false);
    // Reset after transition completes (300ms)
    setTimeout(() => {
      setExpandAllState(undefined);
    }, 300);
  };

  if (notes.length === 0) {
    return (
      <div className="p-4 text-sm text-muted-foreground text-center">
        No notes found. Create your first note to get started.
      </div>
    );
  }

  return (
    <ScrollArea className="h-full">
      <div className="py-2">
        {/* T016: Add "Expand All" button above directory tree */}
        {/* T017: Add "Collapse All" button above directory tree */}
        <div className="flex gap-2 px-2 pb-2">
          <Button
            variant="outline"
            size="sm"
            onClick={handleExpandAll}
            className="flex-1 text-xs"
            aria-label="Expand all folders"
          >
            Expand All
          </Button>
          <Button
            variant="outline"
            size="sm"
            onClick={handleCollapseAll}
            className="flex-1 text-xs"
            aria-label="Collapse all folders"
          >
            Collapse All
          </Button>
        </div>
        {tree.map((node) => (
          <TreeNodeItem
            key={node.path}
            node={node}
            depth={0}
            selectedPath={selectedPath}
            onSelectNote={onSelectNote}
            onMoveNote={onMoveNote}
            forceExpandState={expandAllState}
          />
        ))}
      </div>
    </ScrollArea>
  );
}