fix(session): prefer current directory for implicit drafts
This commit is contained in:
@@ -4,6 +4,7 @@ import type { Snippet } from '@/types/snippet';
|
|||||||
import { opencodeClient } from '@/lib/opencode/client';
|
import { opencodeClient } from '@/lib/opencode/client';
|
||||||
import { runtimeFetch } from '@/lib/runtime-fetch';
|
import { runtimeFetch } from '@/lib/runtime-fetch';
|
||||||
import { useProjectsStore } from '@/stores/useProjectsStore';
|
import { useProjectsStore } from '@/stores/useProjectsStore';
|
||||||
|
import { useDirectoryStore } from '@/stores/useDirectoryStore';
|
||||||
|
|
||||||
export type SnippetScope = 'global' | 'project';
|
export type SnippetScope = 'global' | 'project';
|
||||||
|
|
||||||
@@ -37,10 +38,12 @@ let loadInFlight: Promise<boolean> | null = null;
|
|||||||
|
|
||||||
const getRequestDirectory = (): string | null => {
|
const getRequestDirectory = (): string | null => {
|
||||||
try {
|
try {
|
||||||
const activeProject = useProjectsStore.getState().getActiveProject?.();
|
const currentDirectory = useDirectoryStore.getState().currentDirectory;
|
||||||
if (activeProject?.path?.trim()) return activeProject.path.trim();
|
if (currentDirectory?.trim()) return currentDirectory.trim();
|
||||||
const clientDir = opencodeClient.getDirectory();
|
const clientDir = opencodeClient.getDirectory();
|
||||||
if (clientDir?.trim()) return clientDir.trim();
|
if (clientDir?.trim()) return clientDir.trim();
|
||||||
|
const activeProject = useProjectsStore.getState().getActiveProject?.();
|
||||||
|
if (activeProject?.path?.trim()) return activeProject.path.trim();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('[SnippetsStore] Error resolving config directory:', error);
|
console.warn('[SnippetsStore] Error resolving config directory:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -246,13 +246,13 @@ describe('openNewSessionDraft project binding', () => {
|
|||||||
useDirectoryStore.getState().setDirectory(projectB.path, { showOverlay: false });
|
useDirectoryStore.getState().setDirectory(projectB.path, { showOverlay: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
test('binds draft to active project when current directory differs', () => {
|
test('keeps implicit draft on current directory when active project differs', () => {
|
||||||
useSessionUIStore.getState().openNewSessionDraft();
|
useSessionUIStore.getState().openNewSessionDraft();
|
||||||
const draft = useSessionUIStore.getState().newSessionDraft;
|
const draft = useSessionUIStore.getState().newSessionDraft;
|
||||||
|
|
||||||
expect(draft.open).toBe(true);
|
expect(draft.open).toBe(true);
|
||||||
expect(draft.selectedProjectId).toBe(projectA.id);
|
expect(draft.selectedProjectId).toBe(projectB.id);
|
||||||
expect(draft.directoryOverride).toBe(projectA.path);
|
expect(draft.directoryOverride).toBe(projectB.path);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('respects explicit directoryOverride over active project', () => {
|
test('respects explicit directoryOverride over active project', () => {
|
||||||
|
|||||||
@@ -626,7 +626,6 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
|
|||||||
if (explicitProject || explicitDirectory !== null) {
|
if (explicitProject || explicitDirectory !== null) {
|
||||||
return explicitProject ?? inferredProjectFromDir ?? fallbackProject
|
return explicitProject ?? inferredProjectFromDir ?? fallbackProject
|
||||||
}
|
}
|
||||||
if (activeProject) return activeProject
|
|
||||||
if (currentDirectory) return currentDirProject ?? fallbackProject
|
if (currentDirectory) return currentDirProject ?? fallbackProject
|
||||||
return persistedProjectByDir ?? persistedProjectById ?? fallbackProject
|
return persistedProjectByDir ?? persistedProjectById ?? fallbackProject
|
||||||
})()
|
})()
|
||||||
@@ -634,8 +633,6 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
|
|||||||
const directory = (() => {
|
const directory = (() => {
|
||||||
if (explicitDirectory !== null) return explicitDirectory
|
if (explicitDirectory !== null) return explicitDirectory
|
||||||
if (explicitProject) return normalizePath(explicitProject.path ?? null)
|
if (explicitProject) return normalizePath(explicitProject.path ?? null)
|
||||||
const selectedProjectPath = normalizePath(selectedProject?.path ?? null)
|
|
||||||
if (selectedProjectPath && selectedProjectPath !== currentDirectory) return selectedProjectPath
|
|
||||||
if (currentDirectory) return currentDirectory
|
if (currentDirectory) return currentDirectory
|
||||||
if (persistedTarget?.directory) return persistedTarget.directory
|
if (persistedTarget?.directory) return persistedTarget.directory
|
||||||
return normalizePath(selectedProject?.path ?? null)
|
return normalizePath(selectedProject?.path ?? null)
|
||||||
|
|||||||
Reference in New Issue
Block a user