2026-03-31 18:47:00 +03:00
|
|
|
import { useSessionUIStore } from '@/sync/session-ui-store';
|
2026-04-17 01:13:59 +08:00
|
|
|
import { useSessionWorktreeStore } from '@/sync/session-worktree-store';
|
|
|
|
|
import { getAttachedSessionDirectory } from '@/sync/session-worktree-contract';
|
2026-04-04 02:19:55 +03:00
|
|
|
import { useSessionDirectory } from '@/sync/sync-context';
|
2026-01-27 11:57:56 +02:00
|
|
|
import { useDirectoryStore } from '@/stores/useDirectoryStore';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hook that resolves the effective working directory for tabs (Git, Diff, Files, Terminal).
|
2026-03-31 18:47:00 +03:00
|
|
|
*
|
2026-01-27 11:57:56 +02:00
|
|
|
* Priority order:
|
|
|
|
|
* 1. Worktree metadata path (for worktree sessions)
|
|
|
|
|
* 2. Session directory (for active sessions)
|
|
|
|
|
* 3. Draft session directoryOverride (when creating a new session)
|
|
|
|
|
* 4. Fallback directory from DirectoryStore
|
2026-03-31 18:47:00 +03:00
|
|
|
*
|
2026-01-27 11:57:56 +02:00
|
|
|
* This ensures that tabs show content from the correct project directory
|
|
|
|
|
* even when a draft session is being created.
|
|
|
|
|
*/
|
|
|
|
|
export const useEffectiveDirectory = (): string | undefined => {
|
2026-03-31 18:47:00 +03:00
|
|
|
const currentSessionId = useSessionUIStore((s) => s.currentSessionId);
|
|
|
|
|
const newSessionDraft = useSessionUIStore((s) => s.newSessionDraft);
|
2026-04-04 02:19:55 +03:00
|
|
|
const currentSessionDirectory = useSessionDirectory(currentSessionId);
|
2026-04-17 01:13:59 +08:00
|
|
|
const worktreeAttachment = useSessionWorktreeStore((s) => currentSessionId ? s.getAttachment(currentSessionId) : undefined);
|
2026-03-31 18:47:00 +03:00
|
|
|
const worktreeMap = useSessionUIStore((s) => s.worktreeMetadata);
|
|
|
|
|
const fallbackDirectory = useDirectoryStore((s) => s.currentDirectory);
|
2026-01-27 11:57:56 +02:00
|
|
|
|
|
|
|
|
// If we have an active session, use its directory
|
|
|
|
|
if (currentSessionId) {
|
2026-04-17 01:13:59 +08:00
|
|
|
const attachmentDirectory = getAttachedSessionDirectory(worktreeAttachment);
|
|
|
|
|
if (attachmentDirectory) {
|
|
|
|
|
return attachmentDirectory;
|
|
|
|
|
}
|
2026-01-27 11:57:56 +02:00
|
|
|
const worktreeMetadata = worktreeMap.get(currentSessionId);
|
|
|
|
|
if (worktreeMetadata?.path) {
|
|
|
|
|
return worktreeMetadata.path;
|
|
|
|
|
}
|
2026-04-04 02:19:55 +03:00
|
|
|
if (currentSessionDirectory) {
|
|
|
|
|
return currentSessionDirectory;
|
2026-01-27 11:57:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If a draft session is open, use its directoryOverride
|
2026-03-22 22:31:29 +02:00
|
|
|
if (newSessionDraft?.open && (newSessionDraft.bootstrapPendingDirectory || newSessionDraft.directoryOverride)) {
|
|
|
|
|
return (newSessionDraft.bootstrapPendingDirectory || newSessionDraft.directoryOverride) ?? undefined;
|
2026-01-27 11:57:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to the global directory
|
|
|
|
|
return fallbackDirectory ?? undefined;
|
|
|
|
|
};
|