2026-03-04 01:41:01 +02:00
|
|
|
import { useDirectoryStore } from '@/stores/useDirectoryStore';
|
|
|
|
|
import { useProjectsStore } from '@/stores/useProjectsStore';
|
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-03-31 18:47:00 +03:00
|
|
|
import { useSessions } from '@/sync/sync-context';
|
2026-03-04 01:41:01 +02:00
|
|
|
import type { Session } from '@opencode-ai/sdk/v2';
|
|
|
|
|
|
|
|
|
|
export const useChatSearchDirectory = (): string | undefined => {
|
2026-03-31 18:47:00 +03:00
|
|
|
const currentSessionId = useSessionUIStore((state) => state.currentSessionId);
|
|
|
|
|
const sessions = useSessions();
|
2026-04-17 01:13:59 +08:00
|
|
|
const worktreeAttachment = useSessionWorktreeStore((state) =>
|
|
|
|
|
currentSessionId ? state.getAttachment(currentSessionId) : undefined
|
|
|
|
|
);
|
2026-03-31 18:47:00 +03:00
|
|
|
const worktreeMap = useSessionUIStore((state) => state.worktreeMetadata);
|
|
|
|
|
const newSessionDraft = useSessionUIStore((state) => state.newSessionDraft);
|
2026-03-04 01:41:01 +02:00
|
|
|
|
|
|
|
|
const activeProjectId = useProjectsStore((state) => state.activeProjectId);
|
|
|
|
|
const projects = useProjectsStore((state) => state.projects);
|
|
|
|
|
|
|
|
|
|
const fallbackDirectory = useDirectoryStore((state) => state.currentDirectory);
|
|
|
|
|
|
|
|
|
|
if (currentSessionId) {
|
2026-04-17 01:13:59 +08:00
|
|
|
const attachmentDirectory = getAttachedSessionDirectory(worktreeAttachment);
|
|
|
|
|
if (attachmentDirectory) {
|
|
|
|
|
return attachmentDirectory;
|
|
|
|
|
}
|
2026-03-04 01:41:01 +02:00
|
|
|
const worktreeMetadata = worktreeMap.get(currentSessionId);
|
|
|
|
|
if (worktreeMetadata?.path) {
|
|
|
|
|
return worktreeMetadata.path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SessionWithDirectory = Session & { directory?: string };
|
|
|
|
|
const currentSession = sessions.find((session) => session.id === currentSessionId) as SessionWithDirectory | undefined;
|
|
|
|
|
if (currentSession?.directory) {
|
|
|
|
|
return currentSession.directory;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 22:31:29 +02:00
|
|
|
if (newSessionDraft?.open && (newSessionDraft.bootstrapPendingDirectory || newSessionDraft.directoryOverride)) {
|
|
|
|
|
return (newSessionDraft.bootstrapPendingDirectory || newSessionDraft.directoryOverride) ?? undefined;
|
2026-03-04 01:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (activeProjectId) {
|
|
|
|
|
const activeProject = projects.find((project) => project.id === activeProjectId);
|
|
|
|
|
if (activeProject?.path) {
|
|
|
|
|
return activeProject.path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallbackDirectory ?? undefined;
|
|
|
|
|
};
|