diff --git a/.taskmaster/tasks/tasks.json b/.taskmaster/tasks/tasks.json index 4c8aab5..abb3228 100644 --- a/.taskmaster/tasks/tasks.json +++ b/.taskmaster/tasks/tasks.json @@ -37,6 +37,30 @@ "dependencies": [], "createdAt": "2026-04-25T10:00:00Z" }, + { + "id": 5, + "title": "feat(kb): drag-to-resize splitter (both ask + search)", + "description": "Replace the fixed Bootstrap col-md-5/7 split with a flex layout containing a 6px drag handle. mousedown on the handle starts the drag; while dragging an invisible full-screen overlay covers any iframe so the PDF.js viewer doesn't swallow mouse events. The chosen ratio (15-85%) persists to localStorage as kb-split-pct so the layout sticks across sessions. Both modes share the same _buildSplitShell helper so the look stays consistent.", + "status": "done", + "priority": "normal", + "details": "Default ratio = 42% left (matches the previous col-md-5 ≈ 41.66%). Applied to renderSearchResults, renderAskAnswer, and showSearchPreview (which now uses 100%-height containers via flex column).", + "testStrategy": "Open ask mode, drag the divider; verify PDF iframe doesn't lose mouse focus mid-drag; reload — divider stays at last position. Repeat in search mode.", + "subtasks": [], + "dependencies": [], + "createdAt": "2026-04-25T11:00:00Z" + }, + { + "id": 6, + "title": "fix: search survives view switch (parity with ask in v0.1.9)", + "description": "Search mode had the same view-bound promise problem as ask did before v0.1.9: navigating to another EspoCRM screen and back blanked the results. Hoisted {query, kind, promise} to a module-level _activeSearch with the same {query, kind, hits, selectedIdx} sessionStorage replay we already built for ask. Selected hit index also persists so the right pane comes back to the same source/page the user had open.", + "status": "done", + "priority": "high", + "details": "Mirrors the v0.1.9 ask fix in _attachSearchHandlers + afterRender. Click handler updates _lastSearch.selectedIdx so a return-trip lands on the user's last-clicked hit, not always the top-ranked one.", + "testStrategy": "Search 'מהי תקנה 37'; click a result other than the first; navigate to Contacts; navigate back — same hit selected, same PDF page on the right.", + "subtasks": [], + "dependencies": [], + "createdAt": "2026-04-25T11:00:00Z" + }, { "id": 4, "title": "fix: ask survives view switch via module-level promise + sessionStorage", diff --git a/files/client/custom/modules/knowledge-base/src/views/kb/index.js b/files/client/custom/modules/knowledge-base/src/views/kb/index.js index 28478ac..1f48c6e 100644 --- a/files/client/custom/modules/knowledge-base/src/views/kb/index.js +++ b/files/client/custom/modules/knowledge-base/src/views/kb/index.js @@ -6,28 +6,36 @@ define('modules/knowledge-base/views/kb/index', ['view'], function (Dep) { // it. sessionStorage backs the last completed answer so a hard reload // also recovers it. None of this leaks to the next browser session — // ask history is intentionally ephemeral. - let _activeAsk = null; // {question, promise} while a request is in flight - let _lastAsk = null; // {question, text, sources, completedAt} after success + let _activeAsk = null; // {question, promise, startedAt} while in flight + let _lastAsk = null; // {question, text, sources, completedAt} after success + let _activeSearch = null; // {query, kind, promise} while in flight + let _lastSearch = null; // {query, kind, hits, selectedIdx, completedAt} - const SS_KEY = 'kb-last-ask'; + const SS_ASK = 'kb-last-ask'; + const SS_SEARCH = 'kb-last-search'; + const LS_SPLIT = 'kb-split-pct'; // long-lived UI preference (not session) - function _loadLastFromSession() { + function _loadJson(storage, key, validator) { try { - const raw = sessionStorage.getItem(SS_KEY); + const raw = storage.getItem(key); if (!raw) return null; const parsed = JSON.parse(raw); - if (parsed && typeof parsed.question === 'string') return parsed; - } catch (e) { /* corrupted, ignore */ } - return null; + return validator(parsed) ? parsed : null; + } catch (e) { return null; } } - function _saveLastToSession(entry) { - try { sessionStorage.setItem(SS_KEY, JSON.stringify(entry)); } + function _saveJson(storage, key, value) { + try { storage.setItem(key, JSON.stringify(value)); } catch (e) { /* quota or disabled, fine */ } } - function _clearLastInSession() { - try { sessionStorage.removeItem(SS_KEY); } catch (e) { /* ignore */ } + function _clear(storage, key) { + try { storage.removeItem(key); } catch (e) { /* ignore */ } + } + if (!_lastAsk) { + _lastAsk = _loadJson(sessionStorage, SS_ASK, x => x && typeof x.question === 'string'); + } + if (!_lastSearch) { + _lastSearch = _loadJson(sessionStorage, SS_SEARCH, x => x && typeof x.query === 'string' && Array.isArray(x.hits)); } - if (!_lastAsk) _lastAsk = _loadLastFromSession(); return Dep.extend({ @@ -98,10 +106,10 @@ define('modules/knowledge-base/views/kb/index', ['view'], function (Dep) { this.renderSourcesList(); } - // Recover ask state across view remounts (user navigated away and - // back). _activeAsk wins — there's still a request in flight, so - // re-show the progress panel and re-bind handlers. Otherwise - // _lastAsk replays the most recent answer in this browser session. + // Recover prior state across view remounts (user navigated away and + // back). Active in-flight wins; otherwise replay the last result + // from sessionStorage so the work the user already did doesn't + // disappear behind a tab switch. if (this.mode === 'ask') { if (_activeAsk) { const $input = this.$el.find('input[data-name="query"]'); @@ -114,6 +122,19 @@ define('modules/knowledge-base/views/kb/index', ['view'], function (Dep) { if ($input.length) $input.val(_lastAsk.question); this.renderAskAnswer(_lastAsk.text, _lastAsk.sources); } + } else if (this.mode === 'search') { + if (_activeSearch) { + const $input = this.$el.find('input[data-name="query"]'); + if ($input.length) $input.val(_activeSearch.query); + this.setLoading(true); + this._attachSearchHandlers(_activeSearch); + } else if (_lastSearch && _lastSearch.hits.length) { + const $input = this.$el.find('input[data-name="query"]'); + if ($input.length) $input.val(_lastSearch.query); + const $kind = this.$el.find('select[data-name="kind"]'); + if ($kind.length && _lastSearch.kind) $kind.val(_lastSearch.kind); + this.renderSearchResults(_lastSearch.hits, _lastSearch.selectedIdx || 0); + } } // Focus the input on every render so users can start typing right away. @@ -135,24 +156,53 @@ define('modules/knowledge-base/views/kb/index', ['view'], function (Dep) { }, runSearch: function (query, kind) { + // A new search invalidates the prior cached one. + _lastSearch = null; + _clear(sessionStorage, SS_SEARCH); + this.setLoading(true); - Espo.Ajax.postRequest('KnowledgeBase/action/search', { + const promise = Espo.Ajax.postRequest('KnowledgeBase/action/search', { query: query, kind: kind, topK: 8, - }).then(res => { - this.setLoading(false); - this.renderSearchResults(res.hits || []); + }); + _activeSearch = {query: query, kind: kind, promise: promise}; + this._attachSearchHandlers(_activeSearch); + }, + + _attachSearchHandlers: function (search) { + const self = this; + search.promise.then(res => { + const hits = res.hits || []; + const entry = { + query: search.query, + kind: search.kind, + hits: hits, + selectedIdx: 0, + completedAt: Date.now(), + }; + _lastSearch = entry; + _saveJson(sessionStorage, SS_SEARCH, entry); + if (_activeSearch === search) _activeSearch = null; + if (self.$el && self.$el.length) { + self.setLoading(false); + if (self.mode === 'search') { + self.renderSearchResults(hits); + } + } }).catch(err => { - this.setLoading(false); - this.showError(err); + if (_activeSearch === search) _activeSearch = null; + if (self.$el && self.$el.length) { + self.setLoading(false); + self.showError(err); + } }); }, runAsk: function (message) { // A new ask invalidates whatever last-result was on screen. _lastAsk = null; - _clearLastInSession(); + _clear(sessionStorage, SS_ASK); this.setLoading(true); // Ask goes through the full agent loop (search + rerank + LLM), @@ -181,7 +231,7 @@ define('modules/knowledge-base/views/kb/index', ['view'], function (Dep) { completedAt: Date.now(), }; _lastAsk = entry; - _saveLastToSession(entry); + _saveJson(sessionStorage, SS_ASK, entry); if (_activeAsk === ask) _activeAsk = null; if (self.$el && self.$el.length) { self.stopAskProgress(); @@ -249,12 +299,13 @@ define('modules/knowledge-base/views/kb/index', ['view'], function (Dep) { } }, - renderSearchResults: function (hits) { + renderSearchResults: function (hits, selectedIdx) { const $results = this.$el.find('.kb-results'); if (!hits.length) { $results.html('