Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 68 additions & 25 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,31 @@ export default function App() {
projects: createProjects as any,
defaultProject,
onCancel: () => setUiMode('list'),
onSubmit: (project: string, feature: string) => {
createFeature(project, feature);
const list = collectWorktrees();
const wtInfos = sortWorktrees(attachRuntimeData(list));
setState((s) => ({...s, worktrees: wtInfos}));
setUiMode('list');
onSubmit: async (project: string, feature: string) => {
try {
const result = createFeature(project, feature);
if (result) {
// Refresh UI first to show the new worktree
const list = collectWorktrees();
const wtInfos = sortWorktrees(attachRuntimeData(list));
setState((s) => ({...s, worktrees: wtInfos}));

// Close dialog
setUiMode('list');

// Small delay to ensure UI is updated and tmux session is ready
await new Promise(resolve => setTimeout(resolve, 100));

// Auto-attach to the newly created session
attachOrCreateSession(result.project, result.feature, result.path);
} else {
// Creation failed, close dialog
setUiMode('list');
}
} catch (error) {
console.error('Failed to create feature:', error);
setUiMode('list');
}
}
})
)
Expand Down Expand Up @@ -502,26 +521,50 @@ export default function App() {
onSubmit: async (remoteBranch: string, localName: string) => {
const proj = branchProject || state.worktrees[state.selectedIndex]?.project;
if (!proj) { setUiMode('list'); return; }
const ok = gm.createWorktreeFromRemote(proj, remoteBranch, localName);
if (ok) {
const worktreePath = [BASE_PATH, `${proj}${DIR_BRANCHES_SUFFIX}`, localName].join('/');
setupWorktreeEnvironment(proj, worktreePath);
createTmuxSession(proj, localName, worktreePath);

try {
const ok = gm.createWorktreeFromRemote(proj, remoteBranch, localName);
if (ok) {
const worktreePath = [BASE_PATH, `${proj}${DIR_BRANCHES_SUFFIX}`, localName].join('/');
setupWorktreeEnvironment(proj, worktreePath);
createTmuxSession(proj, localName, worktreePath);

// Refresh UI first to show the new worktree
const list = collectWorktrees();
const wtInfos = sortWorktrees(attachRuntimeData(list));
setState((s) => ({...s, worktrees: wtInfos}));

// Close dialog
setUiMode('list');
setBranchProject(null);
setBranchList([]);

// Small delay to ensure UI is updated and tmux session is ready
await new Promise(resolve => setTimeout(resolve, 100));

// Auto-attach to the newly created session
attachOrCreateSession(proj, localName, worktreePath);

// Fetch PR status asynchronously without blocking UI
Promise.resolve().then(async () => {
try {
const prMap = await gm.batchGetPRStatusForWorktreesAsync(wtInfos.map(w => ({project: w.project, path: w.path})), true);
const withPr = sortWorktrees(wtInfos.map(w => new WorktreeInfo({...w, pr: prMap[w.path] || w.pr})));
setState((s) => ({...s, worktrees: withPr}));
} catch {}
});
} else {
// Creation failed
setUiMode('list');
setBranchProject(null);
setBranchList([]);
}
} catch (error) {
console.error('Failed to create worktree from branch:', error);
setUiMode('list');
setBranchProject(null);
setBranchList([]);
}
const list = collectWorktrees();
const wtInfos = sortWorktrees(attachRuntimeData(list));
setState((s) => ({...s, worktrees: wtInfos}));
setUiMode('list');
setBranchProject(null);
setBranchList([]);
// Fetch PR status asynchronously without blocking UI
Promise.resolve().then(async () => {
try {
const prMap = await gm.batchGetPRStatusForWorktreesAsync(wtInfos.map(w => ({project: w.project, path: w.path})), true);
const withPr = sortWorktrees(wtInfos.map(w => new WorktreeInfo({...w, pr: prMap[w.path] || w.pr})));
setState((s) => ({...s, worktrees: withPr}));
} catch {}
});
},
onRefresh: () => {
if (!branchProject) return;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/BranchPickerDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type BranchInfo = {

type Props = {
branches: BranchInfo[];
onSubmit: (remoteBranch: string, localName: string) => void;
onSubmit: (remoteBranch: string, localName: string) => Promise<void> | void;
onCancel: () => void;
onRefresh?: () => void;
};
Expand Down
22 changes: 19 additions & 3 deletions src/ui/CreateFeatureDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {kebabCase, validateFeatureName, truncateText} from '../utils.js';
type Props = {
projects: ProjectInfo[];
defaultProject?: string;
onSubmit: (project: string, feature: string) => void;
onSubmit: (project: string, feature: string) => Promise<void> | void;
onCancel: () => void;
};

export default function CreateFeatureDialog({projects, defaultProject, onSubmit, onCancel}: Props) {
const [mode, setMode] = useState<'select'|'input'>('select');
const [mode, setMode] = useState<'select'|'input'|'creating'>('select');
const [filter, setFilter] = useState('');
const [selected, setSelected] = useState(() => Math.max(0, projects.findIndex(p => p.name === defaultProject)));
const [feature, setFeature] = useState('');
Expand All @@ -25,6 +25,7 @@ export default function CreateFeatureDialog({projects, defaultProject, onSubmit,

useInput((input, key) => {
if (!isRawModeSupported) return;
if (mode === 'creating') return; // Disable input during creation
if (key.escape) {
if (mode === 'input') setMode('select');
else onCancel();
Expand All @@ -48,14 +49,29 @@ export default function CreateFeatureDialog({projects, defaultProject, onSubmit,
if (key.return) {
const proj = filtered[selected]?.name || projects[0]?.name;
const feat = kebabCase(feature);
if (proj && validateFeatureName(feat)) onSubmit(proj, feat);
if (proj && validateFeatureName(feat)) {
setMode('creating');
Promise.resolve(onSubmit(proj, feat)).catch(() => {
// If creation fails, go back to input mode
setMode('input');
});
}
return;
}
if (key.backspace) setFeature(prev => prev.slice(0, -1));
else if (input && !key.ctrl && !key.meta) setFeature(prev => prev + input);
}
});

if (mode === 'creating') {
return h(
Box, {flexDirection: 'column', alignItems: 'center'},
h(Text, {color: 'cyan'}, 'Creating feature branch...'),
h(Text, {color: 'yellow'}, `${filtered[selected]?.name || ''}/${feature}`),
h(Text, {color: 'gray'}, 'Setting up worktree and tmux session...')
);
}

if (mode === 'select') {
return h(
Box, {flexDirection: 'column'},
Expand Down