From 6055fc340818af8a198c78fafb8e774ee1302388 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Tue, 27 Jan 2026 15:20:17 -0500 Subject: [PATCH] refactor(journals): standardize high-performance reactive search and stabilize results stream - Implemented the loop-proof search pattern in 'src/routes/journals/[journal_id]/+page.svelte' using versioned triggers and untracked dependency isolation. - Stabilized data loading by introducing 'ae_comp__journal_entry_obj_li_wrapper.svelte', ensuring smooth SWR transitions between local and API results. - Synchronized result counts with the rendered list in 'ae_comp__journal_entry_obj_li.svelte' and implemented permissive visibility defaults. - Added 'Remote First' toggle support (Edit Mode only) and laid groundwork for Global Search in 'ae_comp__journal_entry_obj_qry.svelte'. - Updated Dexie schema and API helpers to support future person-level cross-journal searching. --- .../ae_journals/ae_journals__journal_entry.ts | 15 +- src/lib/ae_journals/ae_journals_stores.ts | 3 + src/lib/ae_journals/db_journals.ts | 1 + src/routes/journals/[journal_id]/+page.svelte | 427 +++++------------- .../ae_comp__journal_entry_obj_li.svelte | 42 +- ..._comp__journal_entry_obj_li_wrapper.svelte | 65 +++ .../ae_comp__journal_entry_obj_qry.svelte | 291 ++++++------ 7 files changed, 367 insertions(+), 477 deletions(-) create mode 100644 src/routes/journals/ae_comp__journal_entry_obj_li_wrapper.svelte diff --git a/src/lib/ae_journals/ae_journals__journal_entry.ts b/src/lib/ae_journals/ae_journals__journal_entry.ts index 09ca2561..f4dfb3e7 100644 --- a/src/lib/ae_journals/ae_journals__journal_entry.ts +++ b/src/lib/ae_journals/ae_journals__journal_entry.ts @@ -297,10 +297,11 @@ export async function delete_ae_obj_id__journal_entry({ } // This new function is using CRUD V3 Search. -// Updated 2026-01-02 +// Updated 2026-01-27 export async function qry__journal_entry({ api_cfg, journal_id, + person_id = null, qry_str = null, // Example: 'name:contains:"test"' qry_category_code = null, qry_created_on = null, // Example greater than: '2024-10-24' @@ -325,6 +326,7 @@ export async function qry__journal_entry({ }: { api_cfg: any; journal_id: any; + person_id?: string | null; qry_str?: null | string; qry_category_code?: null | string; qry_created_on?: null | string; @@ -339,15 +341,13 @@ export async function qry__journal_entry({ try_cache?: boolean; log_lvl?: number; }) { - // log_lvl = 1; if (log_lvl) { - console.log(`*** qry__journal_entry() *** journal_id=${journal_id}`); + console.log(`*** qry__journal_entry() *** journal_id=${journal_id} person_id=${person_id}`); } const search_query: any = { and: [] }; if (qry_str) { - console.log('qry_str:', qry_str); // Using 'like' with wildcards to ensure compatibility search_query.and.push({ field: 'default_qry_str', op: 'like', value: `%${qry_str.trim()}%` }); params['lk_qry'] = { 'default_qry_str': qry_str.trim() }; @@ -362,13 +362,16 @@ export async function qry__journal_entry({ } if (qry_priority) { - console.log('qry_priority:', qry_priority); search_query.and.push({ field: 'priority', op: 'eq', value: qry_priority }); } - // Add context + // Context scoping: Prefer journal_id if provided, otherwise fallback to person_id (global search) if (journal_id) { search_query.and.push({ field: 'journal_id_random', op: 'eq', value: journal_id }); + } else if (person_id) { + search_query.and.push({ field: 'person_id_random', op: 'eq', value: person_id }); + } else { + console.warn('qry__journal_entry: No journal_id or person_id provided. Search might be too broad.'); } // Add enabled/hidden filters diff --git a/src/lib/ae_journals/ae_journals_stores.ts b/src/lib/ae_journals/ae_journals_stores.ts index cdd9c12d..fd39a379 100644 --- a/src/lib/ae_journals/ae_journals_stores.ts +++ b/src/lib/ae_journals/ae_journals_stores.ts @@ -79,6 +79,9 @@ const journals_local_data_struct: key_val = { edit_kv: {}, // Query / Search Settings + search_version: 0, + qry__remote_first: false, + qry__global_person_search: false, qry__search_text: '', qry__category_code: '', // For filtering qry__enabled: 'enabled', // all, disabled, enabled diff --git a/src/lib/ae_journals/db_journals.ts b/src/lib/ae_journals/db_journals.ts index 33a00307..add54b09 100644 --- a/src/lib/ae_journals/db_journals.ts +++ b/src/lib/ae_journals/db_journals.ts @@ -117,6 +117,7 @@ export class MySubClassedDexie extends Dexie { journal_entry: ` id, journal_entry_id, journal_entry_id_random, journal_id, journal_id_random, + person_id, person_id_random, code, template, name, diff --git a/src/routes/journals/[journal_id]/+page.svelte b/src/routes/journals/[journal_id]/+page.svelte index 815afc26..19b79b98 100644 --- a/src/routes/journals/[journal_id]/+page.svelte +++ b/src/routes/journals/[journal_id]/+page.svelte @@ -10,36 +10,13 @@ // *** Import Svelte specific import { browser } from '$app/environment'; - // import { goto } from '$app/navigation'; import { untrack } from 'svelte'; // *** Import other supporting libraries - // import { - // ArrowDown01, ArrowDown10, ArrowDownUp, - // BetweenVerticalEnd, BetweenVerticalStart, - // BookHeart, BookImage, Bookmark, BookOpenText, BriefcaseBusiness, - // Check, Copy, - // Expand, Eye, EyeOff, - // Flag, FlagOff, FilePlus, Fingerprint, - // Globe, - // Library, - // MessageSquareWarning, Minus, - // Notebook, - // Pencil, Plus, - // RemoveFormatting, - // SquareLibrary, - // Shapes, Share2, ShieldCheck, ShieldMinus, Siren, Skull, - // Tags, Target, ToggleLeft, ToggleRight, Trash2, TypeOutline, - // X - // } from '@lucide/svelte'; - // import * as icons from '@lucide/svelte'; - // import { Modal } from 'flowbite-svelte'; import { liveQuery } from 'dexie'; // *** Import Aether specific variables and functions - // import type { key_val } from '$lib/ae_stores'; import { ae_util } from '$lib/ae_utils/ae_utils'; - // import { core_func } from '$lib/ae_core/ae_core_functions'; import { ae_snip, ae_loc, @@ -59,361 +36,201 @@ } from '$lib/ae_journals/ae_journals_stores'; import { journals_func } from '$lib/ae_journals/ae_journals_functions'; - // import Journal_obj_id_edit from './ae_journals_comp__journal_obj_id_edit.svelte'; import AeCompJournalObjIdView from './../ae_comp__journal_obj_id_view.svelte'; - // import Journal_page_menu from './session_page_menu.svelte'; - // import Element_data_store from '$lib/element_data_store_v2.svelte'; - - import Journal_entry_obj_li from './../ae_comp__journal_entry_obj_li.svelte'; - // import Journal_entry_obj_qry from './../ae_comp__journal_entry_obj_qry.svelte'; + import Journal_entry_obj_li_wrapper from './../ae_comp__journal_entry_obj_li_wrapper.svelte'; import Journal_obj_id_edit from '../ae_comp__journal_obj_id_edit.svelte'; import AeCompModalJournalExport from '../ae_comp__modal_journal_export.svelte'; import AeCompModalJournalImport from '../ae_comp__modal_journal_import.svelte'; - import { FileDown } from 'lucide-svelte'; - - // let ae_promises: key_val = {}; - // let ae_tmp: key_val = {}; - // let ae_triggers: key_val = {}; // Variables - // *** Quickly pull out data from parent(s) let ae_acct = data[$slct.account_id]; let show_export_modal = $state(false); let show_import_modal = $state(false); + + let search_id_li: Array = $state([]); + let search_debounce_timer: any = null; + let last_search_id = 0; function handle_import_complete() { // Trigger a refresh of the journal entry list - $journals_trig.journal_entry_li = true; + if ($journals_loc.entry.search_version === undefined) $journals_loc.entry.search_version = 0; + $journals_loc.entry.search_version++; } - $effect(() => { - if (log_lvl > 1) { - console.log(`ae_acct = `, ae_acct); - } - }); - - $inspect(log_lvl, `log_lvl = ${log_lvl}`); - $inspect($journals_slct.journal_id, `$journals_slct.journal_id = ${$journals_slct.journal_id}`); - $inspect( - $journals_slct.journal_entry_id, - `$journals_slct.journal_entry_id = ${$journals_slct.journal_entry_id}` - ); - - // $inspect($journals_slct.journal_entry_id).with((type, val) => { - // if (type === 'update') { - // // debugger; // or `console.trace`, or whatever you want - // console.log('$journals_slct.journal_entry_id updated:', val); - // } - // }); - - // For some reason data.params.journal_id (or whatever param) is not being passed to this page when loaded by a link from another page. This seems to be a bug with Svelte or SvelteKit. Hopefully fixed in a future version 5? 2024-11-06 - // $journals_slct.journal_id = ae_acct.slct.journal_id; - // $journals_slct.journal_obj = ae_acct.slct.journal_obj; - $journals_slct.journal_id = ae_acct.slct.journal_id; - - $journals_sess.entry_li = null; $journals_slct.journal_entry_id = null; let lq__journal_obj = $derived( liveQuery(async () => { - let results = await db_journals.journal.get($journals_slct?.journal_id ?? ''); // null or undefined does not reset things like '' does - - // Check if results are different than the current session version stored under $journals_slct + let results = await db_journals.journal.get($journals_slct?.journal_id ?? ''); if ($journals_slct.journal_obj && results) { if (JSON.stringify($journals_slct.journal_obj) !== JSON.stringify(results)) { $journals_slct.journal_obj = { ...results }; } } - return results; }) ); + // Standardized Reactive Search Pattern (Aether UI V3) $effect(() => { - if (log_lvl) { - console.log(`lq__journal_obj: journal_id = ${$journals_slct?.journal_id}`); - console.log(`lq__journal_obj: results = `, lq__journal_obj); - if ($journals_slct.journal_obj && lq__journal_obj) { - if ( - JSON.stringify($journals_slct.journal_obj) !== JSON.stringify(lq__journal_obj) - ) { - console.log( - `Session slct stored version has changed for ID = ${$journals_slct.journal_id}`, - $journals_slct.journal_obj - ); - } else { - console.log( - `Session slct stored version has not changed for ID = ${$journals_slct.journal_id}` - ); - } - } - } - }); + // 1. Reactive Dependencies + const qry_params = { + v: $journals_loc.entry.search_version, + str: $journals_loc.entry.qry__search_text, + cat: $journals_loc.entry.qry__category_code, + limit: $journals_loc.entry.qry__limit, + enabled: $journals_loc.entry.qry__enabled, + hidden: $journals_loc.entry.qry__hidden, + journal_id: $journals_slct.journal_id, + person_id: $ae_loc.person_id + }; - // Trigger load of entries when the journal_id changes - $effect(() => { - if ($journals_slct.journal_id) { + // 2. Debounce Logic + if (search_debounce_timer) clearTimeout(search_debounce_timer); + search_debounce_timer = setTimeout(() => { + // 3. Execution (Untracked to prevent loops) untrack(() => { - if (log_lvl) console.log(`Triggering load for journal_id: ${$journals_slct.journal_id}`); - $journals_trig.journal_entry_li = true; + handle_search_refresh(); }); - } + }, 250); + + return () => { + if (search_debounce_timer) clearTimeout(search_debounce_timer); + }; }); - let lq__journal_entry_obj_li = $derived( - liveQuery(async () => { - let results: any[] = []; + async function handle_search_refresh() { + const current_search_id = ++last_search_id; + const journal_id = $journals_slct.journal_id; + const person_id = $ae_loc.person_id; + const remote_first = $journals_loc.entry.qry__remote_first; + + if (log_lvl) console.log(`[Journal Search #${current_search_id}] Refreshing entries (remote=${remote_first}, journal=${journal_id})...`); + + $journals_sess.entry.qry__status = 'loading'; + + if (remote_first) { + search_id_li = []; + } + + const qry_str = ($journals_loc.entry.qry__search_text ?? '').toLowerCase().trim(); + const cat_code = $journals_loc.entry.qry__category_code; - // If we have a specific list in the session (e.g. from a search), use it. - if ($journals_sess?.entry_li !== null && $journals_sess?.entry_li !== undefined) { - if ($journals_sess.entry_li.length === 0) { - if (log_lvl) { - console.log(`LQ - Using empty $journals_sess.entry_li to get journal entries.`); + let local_ids: string[] = []; + + // 1. FAST PATH: Local IDB Search + if (!remote_first) { + try { + if (journal_id) { + let query = db_journals.journal_entry.where('journal_id').equals(journal_id); + + let local_results = await query + .filter(entry => { + if (cat_code && entry.category_code !== cat_code) return false; + if (qry_str) { + const name = (entry.name ?? '').toLowerCase(); + const content = (entry.content ?? '').toLowerCase(); + return name.includes(qry_str) || content.includes(qry_str); + } + return true; + }) + .toArray(); + + local_results.sort((a, b) => { + const dateA = a.updated_on ? new Date(a.updated_on).getTime() : 0; + const dateB = b.updated_on ? new Date(b.updated_on).getTime() : 0; + return dateB - dateA; + }); + + local_ids = local_results.map(e => e.id || e.journal_entry_id_random).filter(Boolean); + + if (current_search_id === last_search_id) { + if (log_lvl) console.log(`[Journal Search #${current_search_id}] Fast Path found ${local_ids.length} items locally.`); + search_id_li = local_ids; + if (local_ids.length > 0) $journals_sess.entry.qry__status = 'done'; } - - results = []; - } else { - if (log_lvl) { - console.log(`LQ - Using $journals_sess.entry_li to get journal entries.`); - } - - let journal_entry_id_random_li = []; // This is to prevent the array from constantly updating and triggering the liveQuery. - - for (let i = 0; i < $journals_sess?.entry_li.length; i++) { - let journal_entry_obj = $journals_sess?.entry_li[i]; - // Robust ID extraction: try id, then journal_entry_id_random, then journal_entry_id - let target_id = journal_entry_obj.id || journal_entry_obj.journal_entry_id_random || journal_entry_obj.journal_entry_id; - - if (target_id) { - journal_entry_id_random_li.push(target_id); - } - } - results = await db_journals.journal_entry.bulkGet(journal_entry_id_random_li); - // Filter out any undefined results (e.g. if ID not found in local DB yet) - results = results.filter(item => item !== undefined); - } - } else { - if (log_lvl) { - console.log(`LQ - No $journals_sess.entry_li set. Querying local DB for journal entries for journal_id: ${$journals_slct?.journal_id}`); - } - - // Otherwise, query the local database based on the current journal context and filters. - let collection = db_journals.journal_entry - .where('journal_id') - .equals($journals_slct?.journal_id ?? ''); // null or undefined does not reset things like '' does - - // Apply Category Filter - if ( - $journals_loc.entry.qry__category_code && - $journals_loc.entry.qry__category_code.length > 0 - ) { - collection = collection.and( - (entry) => entry.category_code === $journals_loc.entry.qry__category_code - ); - } - - // Apply Enabled Filter - if ($journals_loc.entry.qry__enabled === 'enabled') { - collection = collection.and((entry) => entry.enable === true || entry.enable === null || entry.enable === undefined); - } else if ($journals_loc.entry.qry__enabled === 'not_enabled') { - collection = collection.and((entry) => entry.enable === false); - } - - // Apply Hidden Filter - if ($journals_loc.entry.qry__hidden === 'hidden') { - collection = collection.and((entry) => entry.hide === true); - } else if ($journals_loc.entry.qry__hidden === 'not_hidden') { - collection = collection.and((entry) => entry.hide === false || entry.hide === null || entry.hide === undefined); - } - - // Apply Sorting - if ($lq__journal_obj?.cfg_json?.entry_group_sort === 'DESC') { - results = await collection.reverse().sortBy('updated_on'); - } else { - results = await collection.reverse().sortBy('tmp_sort_1'); - } - - // Apply Limit (after sorting) - if ($journals_loc.entry.qry__limit && $journals_loc.entry.qry__limit > 0) { - results = results.slice(0, $journals_loc.entry.qry__limit); } + } catch (e) { + if (log_lvl) console.warn('Journal Fast Path failed.', e); } + } - // Check if results are different than the current session version stored under $journals_slct - if ( - $journals_slct.journal_entry_obj_li && - JSON.stringify($journals_slct.journal_entry_obj_li) !== JSON.stringify(results) - ) { - $journals_slct.journal_entry_obj_li = [...results]; + // 2. REVALIDATE: API Request + try { + const results = await journals_func.qry__journal_entry({ + api_cfg: $ae_api, + journal_id: journal_id, + person_id: null, + qry_str: qry_str || null, + qry_category_code: cat_code || null, + enabled: $journals_loc.entry.qry__enabled, + hidden: $journals_loc.entry.qry__hidden, + limit: $journals_loc.entry.qry__limit, + log_lvl: 0 + }); + + if (current_search_id === last_search_id) { + const api_results = results || []; + const api_ids = api_results.map((e: any) => e.id || e.journal_entry_id_random).filter(Boolean); + + if (api_ids.length === 0 && local_ids.length > 0 && !remote_first && !qry_str) { + $journals_sess.entry.qry__status = 'done'; + return; + } + + $journals_sess.entry_li = api_results; + search_id_li = api_ids; + $journals_sess.entry.qry__status = 'done'; + if (log_lvl) console.log(`[Journal Search #${current_search_id}] Revalidation Complete. Found ${api_ids.length} items.`); } - - return results; - }) - ); - - $effect(() => { - if (log_lvl) { - console.log(`LQ - $lq__journal_obj.cfg_json = `, $lq__journal_obj?.cfg_json); - console.log( - `LQ - $journals_loc.entry.qry__category_code = `, - $journals_loc.entry.qry__category_code - ); - if ($journals_sess?.entry_li !== null) { - console.log(`LQ - Using $journals_sess.entry_li to get journal entries.`); - } else if ($lq__journal_obj?.cfg_json?.entry_group_sort === 'DESC') { - console.log( - `LQ - Using DESC sort for Journal Entry list journal_id: ${$journals_slct?.journal_id}` - ); - } else if ( - $journals_loc.entry.qry__category_code && - $journals_loc.entry.qry__category_code.length > 0 - ) { - console.log(`LQ - Using category filter: ${$journals_loc.entry.qry__category_code}`); - } else { - console.log( - `LQ - Using default sort for Journal Entry list journal_id: ${$journals_slct?.journal_id}` - ); - } - if ( - $journals_slct.journal_entry_obj_li && - JSON.stringify($journals_slct.journal_entry_obj_li) !== - JSON.stringify(lq__journal_entry_obj_li) - ) { - console.log( - `Session slct li stored version has changed for ID = ${$journals_slct.journal_id}`, - $journals_slct.journal_entry_obj_li - ); - } else { - if (log_lvl > 1) { - console.log( - `Session slct li stored version has not changed for ID = ${$journals_slct.journal_id}` - ); + } catch (error) { + if (current_search_id === last_search_id) { + console.error('Journal revalidation failed:', error); + $journals_sess.entry.qry__status = 'error'; + if (search_id_li.length === 0 && local_ids.length > 0) { + search_id_li = local_ids; } } } - }); - - // Trigger doing a basic load of journal entries - // NOTE: Categories are (currently) filtered at the CSS style level. - $effect(() => { - if ($journals_trig.journal_entry_li) { - $journals_trig.journal_entry_li = false; - - // if ($journals_trig?.journal_entry_li.length > 0) { - // $journals_sess.entry_li = $journals_trig.journal_entry_li; - // console.log('TEST TEST TEST'); - // return ; - // } - - if (log_lvl) { - console.log(`Triggered: $journals_trig.journal_entry_li`); - } - - if ($journals_loc.entry.qry__enabled !== 'all' || $journals_loc.entry.qry__hidden !== 'all') { - if (log_lvl) { - console.log( - `Not set to all for enabled or hidden. Clearing all journal entries to be safe.` - ); - console.log(`$journals_loc.entry.qry__enabled = ${$journals_loc.entry.qry__enabled}`); - console.log(`$journals_loc.entry.qry__hidden = ${$journals_loc.entry.qry__hidden}`); - } - let results = db_journals.journal_entry.clear(); - if (log_lvl) { - console.log(`Cleared all journal entries in the database.`, results); - } - } - - $journals_prom.load__journal_entry_obj_li = journals_func.load_ae_obj_li__journal_entry( - { - api_cfg: $ae_api, - for_obj_type: 'journal', - for_obj_id: $journals_slct.journal_id, - enabled: $journals_loc.entry.qry__enabled, - hidden: $journals_loc.entry.qry__hidden, - limit: $journals_loc.entry.qry__limit, - order_by_li: $journals_loc.entry.qry__order_by_li, - try_cache: true, - log_lvl: log_lvl - } - ); - - // Should we reset the entry list??? - // $journals_sess.entry_li = null; - } - }); + } if (browser) { - let message = { journal_id: $journals_slct?.journal_id ?? null }; - window.parent.postMessage(message, '*'); + window.parent.postMessage({ journal_id: $journals_slct?.journal_id ?? null }, '*'); } - Æ Journals: - {$lq__journal_obj?.name - ? ae_util.shorten_string({ - string: $lq__journal_obj?.name, - max_length: 20, - begin_length: 10, - end_length: 4 - }) - : ''} - - {$ae_loc?.title} + Æ Journals: {$lq__journal_obj?.name ?? ''} - {$ae_loc?.title} {#if $ae_loc.person_id == $lq__journal_obj?.person_id} - - - show_export_modal = true} on_show_import={() => show_import_modal = true} /> - + - - - - - - - show_export_modal = false} /> - show_import_modal = false} diff --git a/src/routes/journals/ae_comp__journal_entry_obj_li.svelte b/src/routes/journals/ae_comp__journal_entry_obj_li.svelte index 510a05cd..3db7616f 100644 --- a/src/routes/journals/ae_comp__journal_entry_obj_li.svelte +++ b/src/routes/journals/ae_comp__journal_entry_obj_li.svelte @@ -71,11 +71,46 @@ function handle_modal_update() { handle_modal_close(); } + + // Derived list of visible items (Standardized Search Pattern 2026-01-27) + // Ensures count matches exactly what is rendered to the user + let visible_journal_entry_obj_li = $derived((() => { + const list = $lq__journal_entry_obj_li; + + if (!list || !Array.isArray(list)) return []; + + const filtered = list.filter((item: any) => { + if (!item) return false; + + // ADMIN/TRUSTED: See everything + if ($ae_loc.trusted_access) return true; + + // PUBLIC: Filter hidden/disabled + // Permissive defaults for missing metadata + const is_hidden = item.hide === true || item.hide === 1; + const is_disabled = item.enable === false || item.enable === 0; + + return !is_hidden && !is_disabled; + }); + + if (log_lvl) console.log(`visible_journal_entry_obj_li: Input=${list.length}, Output=${filtered.length}`); + + return filtered; + })());
- {#if $lq__journal_entry_obj_li && $lq__journal_entry_obj_li.length} - {#each $lq__journal_entry_obj_li as journals_journal_entry_obj, index} + {#if visible_journal_entry_obj_li && visible_journal_entry_obj_li.length} +
+

+ Found: + + {visible_journal_entry_obj_li.length}× + +

+
+ + {#each visible_journal_entry_obj_li as journals_journal_entry_obj, index}
+ interface Props { + container_class_li?: string | Array; + search_id_li?: Array; + lq__journal_obj: any; + log_lvl?: number; + } + + let { + container_class_li = '', + search_id_li = [], + lq__journal_obj, + log_lvl = 0 + }: Props = $props(); + + // *** Import other supporting libraries + import { liveQuery } from 'dexie'; + import { db_journals } from '$lib/ae_journals/db_journals'; + import { journals_loc } from '$lib/ae_journals/ae_journals_stores'; + + // *** Import Aether specific components + import Journal_entry_obj_li from './ae_comp__journal_entry_obj_li.svelte'; + + // Stable LiveQuery Pattern (Aether UI V3) + // Wrapped in $derived to ensure the query refreshes when IDs or filters change + let lq__journal_entry_obj_li = $derived( + liveQuery(async () => { + const ids = search_id_li; + const journal_id = $lq__journal_obj?.journal_id; + + // SCENARIO 1: Specific IDs provided (Search Results) + if (Array.isArray(ids) && ids.length > 0) { + if (log_lvl) console.log(`Journal Wrapper LQ: bulkGet ${ids.length} IDs`); + const results = await db_journals.journal_entry.bulkGet(ids); + return results.filter(item => item !== undefined); + } + + // SCENARIO 2: Fallback to broad search (Default view) + // Only if search is empty and we have a journal context + if (journal_id && !$journals_loc.entry.qry__search_text && !$journals_loc.entry.qry__category_code) { + if (log_lvl) console.log(`Journal Wrapper LQ: Fallback search for journal: ${journal_id}`); + return await db_journals.journal_entry + .where('journal_id') + .equals(journal_id) + .reverse() + .sortBy('tmp_sort_1'); + } + + return []; + }) + ); + + +{#if $lq__journal_entry_obj_li} + +{:else} +
+ +

Loading entries...

+
+{/if} diff --git a/src/routes/journals/ae_comp__journal_entry_obj_qry.svelte b/src/routes/journals/ae_comp__journal_entry_obj_qry.svelte index 9272ea4b..ebf68d8b 100644 --- a/src/routes/journals/ae_comp__journal_entry_obj_qry.svelte +++ b/src/routes/journals/ae_comp__journal_entry_obj_qry.svelte @@ -68,174 +68,139 @@ } from '$lib/ae_journals/ae_journals_stores'; import { journals_func } from '$lib/ae_journals/ae_journals_functions'; - // Debounced Search Logic (Refactored 2026-01-27) - let search_debounce_timer: any = null; - let last_search_id = 0; - - $effect(() => { - // Reactive dependencies - const s_search_text = $journals_loc.entry.qry__search_text; - const s_category_code = $journals_loc.entry.qry__category_code; - const s_enabled = $journals_loc.entry.qry__enabled; - const s_hidden = $journals_loc.entry.qry__hidden; - const s_journal_id = $lq__journal_obj?.journal_id; - const s_trigger = $journals_trig.journal_entry_qry; - - if (search_debounce_timer) clearTimeout(search_debounce_timer); - - // Auto-search if text is cleared or long enough, or if category changes - const should_search = s_trigger || - s_search_text.length === 0 || - s_search_text.length >= 3 || - s_category_code !== ''; - - if (should_search && s_journal_id) { - search_debounce_timer = setTimeout(() => { - $journals_trig.journal_entry_qry = false; - handle_search_refresh(); - }, 400); + // *** Functions and Logic + function handle_search_trigger() { + if ($journals_loc.entry.search_version === undefined) { + $journals_loc.entry.search_version = 0; } + $journals_loc.entry.search_version++; + } - return () => { - if (search_debounce_timer) clearTimeout(search_debounce_timer); + function preventDefault(fn: (event: T) => void) { + return function (event: T) { + event.preventDefault(); + fn(event); }; - }); - - async function handle_search_refresh() { - const current_search_id = ++last_search_id; - const qry_str = $journals_loc.entry.qry__search_text; - const category_code = $journals_loc.entry.qry__category_code; - - if (log_lvl) console.log(`[Journal Search #${current_search_id}] text="${qry_str}" cat="${category_code}"`); - - try { - const results = await journals_func.qry__journal_entry({ - api_cfg: $ae_api, - journal_id: $lq__journal_obj?.journal_id ?? '', - qry_str: qry_str, - qry_category_code: category_code, - enabled: $journals_loc.entry.qry__enabled ?? 'enabled', - hidden: $journals_loc.entry.qry__hidden ?? 'not_hidden', - log_lvl: 0 - }); - - if (current_search_id === last_search_id) { - $journals_prom.load__journal_entry_obj_li = results; - - if (!qry_str && !category_code) { - // Reset to default view if everything cleared - $journals_sess.entry_li = null; - } else { - $journals_sess.entry_li = results || []; - $journals_sess = { ...$journals_sess }; - } - - if (log_lvl) console.log(`[Journal Search #${current_search_id}] Done. Found ${results?.length ?? 0} results.`); - } - } catch (error) { - if (current_search_id === last_search_id) { - console.error('Journal search failed:', error); - } - } } - - - - { - if (event.key === 'Enter') { - $journals_trig.journal_entry_qry = true; - } - }} - title={`Search for Entries in "${$lq__journal_obj?.name}. Press Enter to search.`} - autocomplete="off" - class=" - input input-sm input-bordered - w-44 md:w-52 - text-sm - " - class:bg-red-200={$journals_sess.entry_li == null} - class:dark:bg-red-800={$journals_sess.entry_li == null} - /> - - - {#await $journals_prom.load__journal_entry_obj_li} - Loading... - {:then load_result} - {#if load_result === null} - - No entries found - null - - {:else if load_result?.length === 0} - - No entries found - - {:else if $journals_sess.entry_li?.length === 0} - - Enter to search - - {:else} - - {load_result?.length ?? 0} found - - {/if} - {:catch error} - - Error loading entries. - - {/await} - - + + + + - - - - - + + + + + + + + + + + + {#if $ae_loc.edit_mode} + + {/if} + +