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).
This commit is contained in:
Scott Idem
2026-01-23 11:08:59 -05:00
parent 6925229d56
commit b026a00af5
9 changed files with 229 additions and 219 deletions

View File

@@ -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}`
);
}
}
}
});
</script>
<!-- Svelte layout for a Journal ID page and children -->
@@ -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`}
>
<!-- <BookHeart /> -->
<!-- <Library /> -->
@@ -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})">
<Pencil />
<span class="hidden md:inline">
Edit Journal
@@ -371,8 +273,8 @@
let data_kv = {
category_code: null
};
if ($journals_loc.qry__category_code) {
data_kv.category_code = $journals_loc.qry__category_code;
if ($journals_loc.entry.qry__category_code) {
data_kv.category_code = $journals_loc.entry.qry__category_code;
}
if (log_lvl) {
console.log('Creating new journal entry with data_kv:', data_kv);
@@ -425,7 +327,7 @@
</span>
</div>
<!-- <div class="overflow-auto"> -->
<div class="overflow-auto">
{@render children?.()}
<!-- </div> -->
</section>
</div>
</section>

View File

@@ -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 @@
"
> -->
<Journal_view
{lq__journal_obj}
{lq__journal_entry_obj_li}
onShowExport={() => show_export_modal = true}
onShowImport={() => show_import_modal = true}
<Journal_view
{lq__journal_obj}
{lq__journal_entry_obj_li}
onShowExport={() => show_export_modal = true}
onShowImport={() => show_import_modal = true}
/>
{#if $lq__journal_entry_obj_li && $lq__journal_entry_obj_li?.length}
<Journal_entry_obj_li {lq__journal_obj} {lq__journal_entry_obj_li} />
{:else}
<section class="main_content grow px-1 md:px-2 pb-28 flex flex-col gap-1 items-center">
<p>No journal entry available to show.</p>
<p>No Journal Entry available to show. Please check the query filters or create a new Entry.</p>
</section>
{/if}
@@ -379,4 +421,4 @@
<section class="main_content grow px-1 md:px-2 pb-28 flex flex-col gap-1 items-center">
<p class="text-center">You must be logged in as the owner to view this Journal.</p>
</section>
{/if}
{/if}