@shutootaki/gwm
Version:
git worktree manager CLI
55 lines • 2.54 kB
JavaScript
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { useState, useEffect } from 'react';
import { Text, Box } from 'ink';
import { SelectList } from './SelectList.js';
import { getWorktreesWithStatus, getStatusIcon, } from '../utils/index.js';
export const WorktreeSelector = ({ onSelect, onCancel, initialQuery = '', placeholder = 'Select a worktree:', }) => {
const [worktrees, setWorktrees] = useState([]);
const [error, setError] = useState(null);
useEffect(() => {
const loadWorktrees = async () => {
try {
const parsed = await getWorktreesWithStatus();
setWorktrees(parsed);
}
catch (err) {
setError(err instanceof Error ? err.message : 'Unknown error');
}
};
loadWorktrees();
}, []);
const handleSelect = (item) => {
const worktree = worktrees.find((w) => w.path === item.value);
if (worktree) {
onSelect(worktree);
}
};
if (error) {
return (_jsx(Box, { children: _jsxs(Text, { color: "red", children: ["Error: ", error] }) }));
}
if (worktrees.length === 0) {
return (_jsx(Box, { children: _jsx(Text, { children: "No worktrees found" }) }));
}
// mainワークツリーを先頭に、アクティブなワークツリーを次に表示
const sortedWorktrees = [...worktrees].sort((a, b) => {
if (a.isMain && !b.isMain)
return -1;
if (!a.isMain && b.isMain)
return 1;
if (a.isActive && !b.isActive)
return -1;
if (!a.isActive && b.isActive)
return 1;
return 0;
});
const items = sortedWorktrees.map((worktree) => {
const icon = getStatusIcon(worktree.status, worktree.isActive);
const prefix = icon !== ' ' ? `[${icon}] ` : '';
return {
label: `${prefix}${worktree.branch.padEnd(30)} ${worktree.path}`,
value: worktree.path,
};
});
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(SelectList, { items: items, onSelect: handleSelect, onCancel: onCancel, placeholder: placeholder, initialQuery: initialQuery }), _jsx(Box, { children: _jsxs(Text, { color: "gray", children: [_jsx(Text, { color: "yellow", children: "[*]" }), " Active ", ' ', _jsx(Text, { color: "cyan", children: "[M]" }), " Main ", ' ', _jsx(Text, { color: "white", children: "[-]" }), " Other"] }) })] }));
};
//# sourceMappingURL=WorktreeSelector.js.map