forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseGitWorktrees.ts
More file actions
31 lines (27 loc) · 958 Bytes
/
useGitWorktrees.ts
File metadata and controls
31 lines (27 loc) · 958 Bytes
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
"use client";
import { useState, useCallback } from "react";
import type { GitWorktree } from "@/types";
export function useGitWorktrees(cwd: string) {
const [worktrees, setWorktrees] = useState<GitWorktree[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const fetch_ = useCallback(async () => {
if (!cwd) return;
setLoading(true);
setError(null);
try {
const res = await fetch(`/api/git/worktrees?cwd=${encodeURIComponent(cwd)}`);
if (!res.ok) {
const data = await res.json();
throw new Error(data.error || 'Failed to fetch worktrees');
}
const data = await res.json();
setWorktrees(data.worktrees);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch worktrees');
} finally {
setLoading(false);
}
}, [cwd]);
return { worktrees, loading, error, fetch: fetch_ };
}