From b026a00af5a2db27d7163b4b440feeb44e517a25 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Fri, 23 Jan 2026 11:08:59 -0500 Subject: [PATCH] Fix: Stabilize Journal Entries query logic and UI search behavior - Consolidated LiveQuery logic into [journal_id]/+page.svelte and removed redundancy from layout. - Added automatic load trigger on journal_id change to ensure data freshness. - Hardened Enabled/Hidden filters to correctly include NULL/undefined values in default views. - Refined search behavior to distinguish between "default view" (null) and "no results found" ([]). - Updated modal_journals_config.svelte with standardized module-level settings. - Robust ID handling in bulk retrieval to handle varying property names (id, random_id). --- src/lib/ae_journals/ae_journals_stores.ts | 41 +++-- src/routes/journals/+layout.svelte | 9 +- .../journals/[journal_id]/+layout.svelte | 114 +------------ src/routes/journals/[journal_id]/+page.svelte | 158 +++++++++++------- .../ae_comp__journal_entry_obj_li.svelte | 12 +- .../ae_comp__journal_entry_obj_qry.svelte | 53 +++--- .../ae_comp__journal_entry_quick_add.svelte | 4 +- .../ae_comp__journal_obj_id_view.svelte | 4 +- .../journals/modal_journals_config.svelte | 53 ++++++ 9 files changed, 229 insertions(+), 219 deletions(-) diff --git a/src/lib/ae_journals/ae_journals_stores.ts b/src/lib/ae_journals/ae_journals_stores.ts index c032188f..cdd9c12d 100644 --- a/src/lib/ae_journals/ae_journals_stores.ts +++ b/src/lib/ae_journals/ae_journals_stores.ts @@ -21,14 +21,6 @@ const journals_local_data_struct: key_val = { time_format: 'time_12_short', time_hours: 12, // 12 or 24 - qry__enabled: 'enabled', // all, disabled, enabled - qry__hidden: 'not_hidden', // all, hidden, not_hidden - qry__limit: 20, - qry__order_by_li: { - // 'created_on': 'desc', - // 'updated_on': 'desc', - }, - qry__offset: 0, qry__journal_id: null, journal_view_history_li: [], // Appended each time the journal is loaded. @@ -58,6 +50,17 @@ const journals_local_data_struct: key_val = { edit: false, edit_kv: {}, + // Query / Search Settings + qry__search_text: '', + qry__enabled: 'enabled', // all, disabled, enabled + qry__hidden: 'not_hidden', // all, hidden, not_hidden + qry__limit: 25, + qry__offset: 0, + qry__order_by_li: { + // 'created_on': 'desc', + // 'updated_on': 'desc', + }, + type_code_li: [ { code: 'diary', name: 'Diary' }, { code: 'log', name: 'Log' }, @@ -72,6 +75,24 @@ const journals_local_data_struct: key_val = { ] }, entry: { + edit: false, + edit_kv: {}, + + // Query / Search Settings + qry__search_text: '', + qry__category_code: '', // For filtering + qry__enabled: 'enabled', // all, disabled, enabled + qry__hidden: 'not_hidden', // all, hidden, not_hidden + qry__limit: 50, + qry__offset: 0, + qry__order_by_li: { + // 'created_on': 'desc', + // 'updated_on': 'desc', + }, + + // This is effectively "last used journal for creating entries" + qry__journal_id: null, + llm__system_prompt: 'Summarize the following journal entry content in a concise manner, focusing on key points and insights.', llm__max_tokens: 512, @@ -81,9 +102,7 @@ const journals_local_data_struct: key_val = { llm__frequency_penalty: 0.0, llm__presence_penalty: 0.0, - auto_save: false, - edit: false, - edit_kv: {} + auto_save: false } }; // console.log(`AE Stores - App Journals Local Storage Data:`, journals_local_data_struct); diff --git a/src/routes/journals/+layout.svelte b/src/routes/journals/+layout.svelte index 9b905942..40a7048a 100644 --- a/src/routes/journals/+layout.svelte +++ b/src/routes/journals/+layout.svelte @@ -26,10 +26,11 @@ } let { data, children }: Props = $props(); - $journals_loc.qry__enabled = 'enabled'; - $journals_loc.qry__hidden = 'not_hidden'; - $journals_loc.qry__limit = 15; - $journals_loc.qry__offset = 0; + // Initialize/Reset Entry Query defaults on layout load + // $journals_loc.entry.qry__enabled = 'enabled'; + // $journals_loc.entry.qry__hidden = 'not_hidden'; + // $journals_loc.entry.qry__limit = 15; + // $journals_loc.entry.qry__offset = 0; // Quickly save the data passed from the parent(s) to the Svelte stores, localStorage, and other. $slct.account_id = data.account_id; diff --git a/src/routes/journals/[journal_id]/+layout.svelte b/src/routes/journals/[journal_id]/+layout.svelte index 0c2bce43..b618c9e1 100644 --- a/src/routes/journals/[journal_id]/+layout.svelte +++ b/src/routes/journals/[journal_id]/+layout.svelte @@ -68,104 +68,6 @@ } } }); - - let lq__journal_entry_obj_li = $derived( - liveQuery(async () => { - let results; - - if ($journals_sess?.entry_li && $journals_sess?.entry_li?.length) { - // $journals_sess.entry_li_trigger = false; - 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]; - let journal_entry_id_random = journal_entry_obj.journal_entry_id_random; - journal_entry_id_random_li.push(journal_entry_id_random); - } - // let journal_entry_id_random_li = tmp_li; - - results = await db_journals.journal_entry.bulkGet(journal_entry_id_random_li); - } else if ($lq__journal_obj?.cfg_json?.entry_group_sort === 'DESC') { - results = await db_journals.journal_entry - // .orderBy('updated_on') - .where('journal_id') - .equals($journals_slct?.journal_id ?? '') // null or undefined does not reset things like '' does - .reverse() - // .sortBy('tmp_sort_2'); - .sortBy('updated_on'); - // .sortBy('title'); - } else if ( - $journals_loc.filter__category_code && - $journals_loc.filter__category_code.length > 0 - ) { - results = await db_journals.journal_entry - .where('journal_id') - .equals($journals_slct?.journal_id ?? '') // null or undefined does not reset things like '' does - .and((entry) => entry.category_code === $journals_loc.filter__category_code) - .reverse() - .sortBy('tmp_sort_1'); - } else { - results = await db_journals.journal_entry - .where('journal_id') - .equals($journals_slct?.journal_id ?? '') // null or undefined does not reset things like '' does - .reverse() - .sortBy('tmp_sort_1'); - // .sortBy('updated_on'); - } - - // 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]; - } - - return results; - }) - ); - - $effect(() => { - if (log_lvl) { - console.log(`LQ - $lq__journal_obj.cfg_json = `, $lq__journal_obj?.cfg_json); - console.log( - `LQ - $journals_loc.filter__category_code = `, - $journals_loc.filter__category_code - ); - if ($journals_sess?.entry_li && $journals_sess?.entry_li?.length) { - 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.filter__category_code && - $journals_loc.filter__category_code.length > 0 - ) { - console.log(`LQ - Using category filter: ${$journals_loc.filter__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}` - ); - } - } - } - }); @@ -221,7 +123,8 @@ hover:preset-filled-tertiary-300-700 transition-all " - title={`View all journals menu: "${$ae_loc?.user?.name}"\nMiddle-click to open in new tab`} + title={`View all journals menu: "${$ae_loc?.user?.name}" +Middle-click to open in new tab`} > @@ -348,8 +251,7 @@ hover:variant-filled-warning transition " - title="Edit Journal meta and configuration (name, type, passcode, categories, etc.: {$lq__journal_obj?.name})" - > + title="Edit Journal meta and configuration (name, type, passcode, categories, etc.: {$lq__journal_obj?.name})"> - +
{@render children?.()} - - +
+ \ No newline at end of file diff --git a/src/routes/journals/[journal_id]/+page.svelte b/src/routes/journals/[journal_id]/+page.svelte index 24d23f04..77bd084f 100644 --- a/src/routes/journals/[journal_id]/+page.svelte +++ b/src/routes/journals/[journal_id]/+page.svelte @@ -11,6 +11,7 @@ // *** Import Svelte specific import { browser } from '$app/environment'; // import { goto } from '$app/navigation'; + import { untrack } from 'svelte'; // *** Import other supporting libraries // import { @@ -111,7 +112,7 @@ $journals_slct.journal_id = ae_acct.slct.journal_id; - $journals_sess.entry_li = []; + $journals_sess.entry_li = null; $journals_slct.journal_entry_id = null; let lq__journal_obj = $derived( @@ -150,52 +151,93 @@ } }); + // Trigger load of entries when the journal_id changes + $effect(() => { + if ($journals_slct.journal_id) { + untrack(() => { + if (log_lvl) console.log(`Triggering load for journal_id: ${$journals_slct.journal_id}`); + $journals_trig.journal_entry_li = true; + }); + } + }); + let lq__journal_entry_obj_li = $derived( liveQuery(async () => { let results; - if ($journals_sess.entry_li_trigger && !$journals_sess?.entry_li) { - $journals_sess.entry_li = null; - $journals_sess.entry_li_trigger = false; - } + // 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.`); + } - if ($journals_sess?.entry_li && $journals_sess?.entry_li?.length) { - let journal_entry_id_random_li = []; // This is to prevent the array from constantly updating and triggering the liveQuery. + results = []; + } else { + if (log_lvl) { + console.log(`LQ - Using $journals_sess.entry_li to get journal entries.`); + } - for (let i = 0; i < $journals_sess?.entry_li.length; i++) { - let journal_entry_obj = $journals_sess?.entry_li[i]; - let journal_entry_id_random = journal_entry_obj.journal_entry_id_random; - journal_entry_id_random_li.push(journal_entry_id_random); + 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); } - // let journal_entry_id_random_li = tmp_li; - - results = await db_journals.journal_entry.bulkGet(journal_entry_id_random_li); - } else if ($lq__journal_obj?.cfg_json?.entry_group_sort === 'DESC') { - results = await db_journals.journal_entry - // .orderBy('updated_on') - .where('journal_id') - .equals($journals_slct?.journal_id ?? '') // null or undefined does not reset things like '' does - .reverse() - // .sortBy('tmp_sort_2'); - .sortBy('updated_on'); - // .sortBy('title'); - } else if ( - $journals_loc.filter__category_code && - $journals_loc.filter__category_code.length > 0 - ) { - results = await db_journals.journal_entry - .where('journal_id') - .equals($journals_slct?.journal_id ?? '') // null or undefined does not reset things like '' does - .and((entry) => entry.category_code === $journals_loc.filter__category_code) - .reverse() - .sortBy('tmp_sort_1'); } else { - results = await db_journals.journal_entry + 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 - .reverse() - .sortBy('tmp_sort_1'); - // .sortBy('updated_on'); + .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); + } } // Check if results are different than the current session version stored under $journals_slct @@ -214,20 +256,20 @@ if (log_lvl) { console.log(`LQ - $lq__journal_obj.cfg_json = `, $lq__journal_obj?.cfg_json); console.log( - `LQ - $journals_loc.filter__category_code = `, - $journals_loc.filter__category_code + `LQ - $journals_loc.entry.qry__category_code = `, + $journals_loc.entry.qry__category_code ); - if ($journals_sess?.entry_li && $journals_sess?.entry_li?.length) { + 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.filter__category_code && - $journals_loc.filter__category_code.length > 0 + $journals_loc.entry.qry__category_code && + $journals_loc.entry.qry__category_code.length > 0 ) { - console.log(`LQ - Using category filter: ${$journals_loc.filter__category_code}`); + 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}` @@ -268,13 +310,13 @@ console.log(`Triggered: $journals_trig.journal_entry_li`); } - if ($journals_loc.qry__enabled !== 'all' || $journals_loc.qry__hidden !== 'all') { + 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.qry__enabled = ${$journals_loc.qry__enabled}`); - console.log(`$journals_loc.qry__hidden = ${$journals_loc.qry__hidden}`); + 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) { @@ -287,10 +329,10 @@ api_cfg: $ae_api, for_obj_type: 'journal', for_obj_id: $journals_slct.journal_id, - enabled: $journals_loc.qry__enabled, - hidden: $journals_loc.qry__hidden, - limit: $journals_loc.qry__limit, - order_by_li: $journals_loc.qry__order_by_li, + 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 } @@ -336,18 +378,18 @@ " > --> - show_export_modal = true} - onShowImport={() => show_import_modal = true} + show_export_modal = true} + onShowImport={() => show_import_modal = true} /> {#if $lq__journal_entry_obj_li && $lq__journal_entry_obj_li?.length} {:else}
-

No journal entry available to show.

+

No Journal Entry available to show. Please check the query filters or create a new Entry.

{/if} @@ -379,4 +421,4 @@

You must be logged in as the owner to view this Journal.

-{/if} +{/if} \ No newline at end of file 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 7b40739c..51680eda 100644 --- a/src/routes/journals/ae_comp__journal_entry_obj_li.svelte +++ b/src/routes/journals/ae_comp__journal_entry_obj_li.svelte @@ -349,19 +349,17 @@ type="button" onclick={() => { if ( - $journals_loc.filter__category_code == + $journals_loc.entry.qry__category_code == journals_journal_entry_obj.category_code ) { - $journals_loc.filter__category_code = null; + $journals_loc.entry.qry__category_code = null; } else { - $journals_loc.filter__category_code = - journals_journal_entry_obj.category_code; - $journals_loc.qry__category_code = + $journals_loc.entry.qry__category_code = journals_journal_entry_obj.category_code; } $journals_trig.journal_entry_li = true; }} - class:bg-green-100={$journals_loc.filter__category_code == + class:bg-green-100={$journals_loc.entry.qry__category_code == journals_journal_entry_obj.category_code} class="btn btn-sm variant-outline-secondary hover:preset-filled-secondary-500 transition py-1 px-2" title={`Filter by category: ${journals_journal_entry_obj.category_code}`} @@ -559,6 +557,6 @@ /> {/if} {:else} -

No journal entry available to show.

+

No Æ Journal Entry available to show.

{/if} \ No newline at end of file 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 ec19863d..51c1cb6c 100644 --- a/src/routes/journals/ae_comp__journal_entry_obj_qry.svelte +++ b/src/routes/journals/ae_comp__journal_entry_obj_qry.svelte @@ -77,7 +77,7 @@ if (log_lvl) { console.log( - `Triggered: $journals_trig.journal_entry_qry: ${$journals_loc.qry__search_text}` + `Triggered: $journals_trig.journal_entry_qry: ${$journals_loc.entry.qry__search_text}` ); } @@ -85,36 +85,31 @@ $journals_prom.load__journal_entry_obj_li = await journals_func.qry__journal_entry({ api_cfg: $ae_api, journal_id: $lq__journal_obj?.journal_id ?? '', - qry_str: $journals_loc.qry__search_text, + qry_str: $journals_loc.entry.qry__search_text, // qry_created_on: null, // qry_alert: null, // qry_priority: null, // qry_type: and_type, - enabled: 'enabled', // $journals_loc.qry__enabled, - hidden: 'not_hidden', // $journals_loc.qry__hidden, - // order_by_li: $journals_loc.qry__order_by_li, - // limit: $journals_loc.qry__limit, + enabled: $journals_loc.entry.qry__enabled ?? 'enabled', + hidden: $journals_loc.entry.qry__hidden ?? 'not_hidden', + // order_by_li: $journals_loc.entry.qry__order_by_li, + // limit: $journals_loc.entry.qry__limit, // try_cache: try_cache, log_lvl: log_lvl }); - if ($journals_prom.load__journal_entry_obj_li.length) { + + if (!$journals_loc.entry.qry__search_text) { + // If search text was cleared or empty, reset to default view (null) + $journals_sess.entry_li = null; + } else if ($journals_prom.load__journal_entry_obj_li && $journals_prom.load__journal_entry_obj_li.length > 0) { $journals_sess.entry_li = $journals_prom.load__journal_entry_obj_li; - - $journals_sess = { - ...$journals_sess - }; // ensure session is updated - - // $journals_trig.journal_entry_li = true; // trigger the entry list to refresh - // $journals_trig.journal_entry_li = $journals_prom.load__journal_entry_obj_li; + $journals_sess = { ...$journals_sess }; // ensure session is updated } else { console.log('Clear the search results: no entries found for that query.'); - // $journals_sess.entry_li = ['']; - $journals_sess.entry_li = null; - // $journals_trig.journal_entry_li = true; - // alert('No journal entries found for that search query.'); - // $journals_sess = $journals_sess; + // Explicitly set to empty array to indicate "0 results found" (vs null which is "default view") + $journals_sess.entry_li = []; } if (log_lvl) { @@ -131,10 +126,10 @@ disabled={false} type="text" placeholder="Search Journal Entries" - bind:value={$journals_loc.qry__search_text} + bind:value={$journals_loc.entry.qry__search_text} onkeyup={(event) => { if (event.key === 'Enter') { - // $journals_loc.qry__search_text = (event.target as HTMLInputElement).value; + // $journals_loc.entry.qry__search_text = (event.target as HTMLInputElement).value; $journals_trig.journal_entry_qry = true; } }} @@ -151,11 +146,11 @@