Refine journal search filtering
This commit is contained in:
@@ -235,7 +235,7 @@ Middle-click to open in new tab`}>
|
||||
</a>
|
||||
{:else}
|
||||
<!-- Edit Journal button. Creates a modal to edit the journal. -->
|
||||
<Journal_entry_obj_qry {log_lvl} {lq__journal_obj} />
|
||||
<Journal_entry_obj_qry {log_lvl} lq__journal_obj={$lq__journal_obj} />
|
||||
{/if}
|
||||
|
||||
<!-- Add default journal entry -->
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
let log_lvl: number = $state(0);
|
||||
|
||||
interface Props {
|
||||
data: any;
|
||||
data: {
|
||||
account_id: string;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
let { data }: Props = $props();
|
||||
@@ -16,23 +19,13 @@ import { untrack } from 'svelte';
|
||||
import { liveQuery } from 'dexie';
|
||||
|
||||
// *** Import Aether specific variables and functions
|
||||
import { ae_util } from '$lib/ae_utils/ae_utils';
|
||||
import {
|
||||
ae_snip,
|
||||
ae_loc,
|
||||
ae_sess,
|
||||
ae_api,
|
||||
ae_trig,
|
||||
slct,
|
||||
slct_trigger
|
||||
} from '$lib/stores/ae_stores';
|
||||
import { ae_loc, ae_api } from '$lib/stores/ae_stores';
|
||||
import { db_journals } from '$lib/ae_journals/db_journals';
|
||||
import { journal_entry_filter_list } from '$lib/ae_journals/ae_journals_search_helpers';
|
||||
import {
|
||||
journals_loc,
|
||||
journals_sess,
|
||||
journals_slct,
|
||||
journals_prom,
|
||||
journals_trig
|
||||
journals_slct
|
||||
} from '$lib/ae_journals/ae_journals_stores';
|
||||
import { journals_func } from '$lib/ae_journals/ae_journals_functions';
|
||||
|
||||
@@ -42,12 +35,34 @@ import AeCompModalJournalExport from '../ae_comp__modal_journal_export.svelte';
|
||||
import AeCompModalJournalImport from '../ae_comp__modal_journal_import.svelte';
|
||||
|
||||
// Variables
|
||||
let ae_acct = $derived(data[data.account_id]);
|
||||
interface JournalPageAccount {
|
||||
api?: unknown;
|
||||
loc?: {
|
||||
person_id?: string | null;
|
||||
};
|
||||
slct: {
|
||||
journal_id: string | null;
|
||||
journal_entry_id?: string | null;
|
||||
};
|
||||
}
|
||||
|
||||
let ae_acct = $derived(data[data.account_id] as JournalPageAccount);
|
||||
let show_export_modal = $state(false);
|
||||
let show_import_modal = $state(false);
|
||||
|
||||
interface JournalSearchParams {
|
||||
v: number;
|
||||
str: string;
|
||||
cat: string;
|
||||
limit: number;
|
||||
enabled: 'enabled' | 'all' | 'not_enabled';
|
||||
hidden: 'hidden' | 'all' | 'not_hidden';
|
||||
journal_id: string | null;
|
||||
remote_first: boolean;
|
||||
}
|
||||
|
||||
let search_id_li: Array<string> = $state([]);
|
||||
let search_debounce_timer: any = null;
|
||||
let search_debounce_timer: ReturnType<typeof setTimeout> | null = null;
|
||||
let last_search_id = 0;
|
||||
let last_executed_key = ''; // Search Guard Key
|
||||
|
||||
@@ -72,45 +87,6 @@ let lq__journal_obj = $derived(
|
||||
})
|
||||
);
|
||||
|
||||
// Stable LiveQuery Pattern (Aether UI V3)
|
||||
// Re-wrapped in $derived to ensure the observable instance remains stable
|
||||
// unless the underlying dependencies (ids, search context) change.
|
||||
// Important: keep the `liveQuery` closure free of transient reactive
|
||||
// references — capture stable values (ids, search keys) so the observable
|
||||
// isn't recreated unnecessarily on every render. Use `search_id_li` or
|
||||
// other plain arrays/values as explicit dependencies.
|
||||
let lq__journal_entry_obj_li = $derived(
|
||||
liveQuery(async () => {
|
||||
const ids = search_id_li;
|
||||
const journal_id = $lq__journal_obj?.journal_id;
|
||||
const search_text = $journals_loc.entry.qry__search_text;
|
||||
const cat_code = $journals_loc.entry.qry__category_code;
|
||||
|
||||
// SCENARIO 1: Specific IDs provided (Search Results)
|
||||
if (Array.isArray(ids) && ids.length > 0) {
|
||||
if (log_lvl)
|
||||
console.log(`Journal Page 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)
|
||||
if (journal_id && !search_text && !cat_code) {
|
||||
if (log_lvl)
|
||||
console.log(
|
||||
`Journal Page 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 [];
|
||||
})
|
||||
);
|
||||
|
||||
// Standardized Reactive Search Pattern (Aether UI V3)
|
||||
// 1. Isolate dependencies into a stable derived object
|
||||
let search_params = $derived({
|
||||
@@ -121,10 +97,53 @@ let search_params = $derived({
|
||||
enabled: $journals_loc.entry.qry__enabled,
|
||||
hidden: $journals_loc.entry.qry__hidden,
|
||||
journal_id: $journals_slct.journal_id,
|
||||
person_id: $ae_loc.person_id,
|
||||
remote_first: $journals_loc.entry.qry__remote_first
|
||||
});
|
||||
|
||||
// Stable LiveQuery Pattern (Aether UI V3)
|
||||
// Re-wrapped in $derived to ensure the observable instance remains stable
|
||||
// unless the underlying dependencies (ids, search context) change.
|
||||
// Important: keep the `liveQuery` closure free of transient reactive
|
||||
// references — capture stable values (ids, search keys) so the observable
|
||||
// isn't recreated unnecessarily on every render. Use `search_id_li` or
|
||||
// other plain arrays/values as explicit dependencies.
|
||||
let lq__journal_entry_obj_li = $derived(
|
||||
(() => {
|
||||
const ids = search_id_li;
|
||||
const params = search_params;
|
||||
const journal_id = $lq__journal_obj?.journal_id;
|
||||
|
||||
return liveQuery(async () => {
|
||||
if (params.remote_first && (!Array.isArray(ids) || ids.length === 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// SCENARIO 1: Specific IDs provided (Search Results)
|
||||
if (Array.isArray(ids) && ids.length > 0) {
|
||||
if (log_lvl)
|
||||
console.log(`Journal Page LQ: bulkGet ${ids.length} IDs`);
|
||||
const results = await db_journals.journal_entry.bulkGet(ids);
|
||||
return results.filter((item) => item !== undefined);
|
||||
}
|
||||
|
||||
if (!journal_id) return null;
|
||||
|
||||
// SCENARIO 2: Fallback to broad journal search (Default view)
|
||||
if (log_lvl)
|
||||
console.log(
|
||||
`Journal Page LQ: Broad search for journal: ${journal_id}`
|
||||
);
|
||||
|
||||
const results = await db_journals.journal_entry
|
||||
.where('journal_id')
|
||||
.equals(journal_id)
|
||||
.toArray();
|
||||
|
||||
return journal_entry_filter_list(results, params) ?? [];
|
||||
});
|
||||
})()
|
||||
);
|
||||
|
||||
// 2. Controlled effect for triggering searches
|
||||
$effect(() => {
|
||||
// Establishes reactive dependency on search_params
|
||||
@@ -143,7 +162,7 @@ $effect(() => {
|
||||
};
|
||||
});
|
||||
|
||||
async function handle_search_refresh(params: any) {
|
||||
async function handle_search_refresh(params: JournalSearchParams) {
|
||||
// 1. Guard: Check if criteria actually changed
|
||||
const qry_key = JSON.stringify(params);
|
||||
if (qry_key === last_executed_key) return;
|
||||
@@ -163,46 +182,40 @@ async function handle_search_refresh(params: any) {
|
||||
$journals_sess.entry.qry__status = 'loading';
|
||||
});
|
||||
|
||||
if (remote_first) {
|
||||
untrack(() => {
|
||||
search_id_li = [];
|
||||
});
|
||||
}
|
||||
|
||||
const qry_str = params.str;
|
||||
const cat_code = params.cat;
|
||||
const order_by_li = {
|
||||
group: 'DESC',
|
||||
priority: 'DESC',
|
||||
sort: 'DESC',
|
||||
updated_on: 'DESC',
|
||||
created_on: 'DESC'
|
||||
};
|
||||
|
||||
let local_ids: string[] = [];
|
||||
|
||||
// 3. FAST PATH: Local IDB Search (SWR)
|
||||
// We skip this ONLY if remote_first is checked AND we have search text
|
||||
if (!remote_first) {
|
||||
// Broad views still need the local IDB set so "All" remains complete.
|
||||
// Remote-first is only used to skip the local fast path for text searches.
|
||||
if (!remote_first || !qry_str) {
|
||||
try {
|
||||
if (journal_id) {
|
||||
let local_results = await db_journals.journal_entry
|
||||
const local_results = await db_journals.journal_entry
|
||||
.where('journal_id')
|
||||
.equals(journal_id)
|
||||
.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;
|
||||
});
|
||||
const filtered_results =
|
||||
journal_entry_filter_list(local_results, params) ?? [];
|
||||
|
||||
local_ids = local_results
|
||||
.map((e) => e.id || e.journal_entry_id)
|
||||
local_ids = filtered_results
|
||||
.map((entry) => entry.id || entry.journal_entry_id)
|
||||
.filter(Boolean);
|
||||
|
||||
if (current_search_id === last_search_id) {
|
||||
@@ -233,21 +246,25 @@ async function handle_search_refresh(params: any) {
|
||||
enabled: params.enabled,
|
||||
hidden: params.hidden,
|
||||
limit: params.limit,
|
||||
order_by_li,
|
||||
log_lvl: 0
|
||||
});
|
||||
|
||||
if (current_search_id === last_search_id) {
|
||||
const api_results = results || [];
|
||||
const api_results = (results || []) as Array<{
|
||||
id?: string;
|
||||
journal_entry_id?: string | null;
|
||||
}>;
|
||||
const api_ids = api_results
|
||||
.map((e: any) => e.id || e.journal_entry_id)
|
||||
.filter(Boolean);
|
||||
.map((entry) => entry.id || entry.journal_entry_id)
|
||||
.filter((entry): entry is string => Boolean(entry));
|
||||
const display_ids = !qry_str && local_ids.length > 0 ? local_ids : api_ids;
|
||||
|
||||
// Protect UI cache if API returns empty during revalidation
|
||||
if (
|
||||
api_ids.length === 0 &&
|
||||
!qry_str &&
|
||||
local_ids.length > 0 &&
|
||||
!remote_first &&
|
||||
!qry_str
|
||||
api_ids.length === 0
|
||||
) {
|
||||
untrack(() => {
|
||||
$journals_sess.entry.qry__status = 'done';
|
||||
@@ -257,7 +274,7 @@ async function handle_search_refresh(params: any) {
|
||||
|
||||
untrack(() => {
|
||||
$journals_sess.entry_li = api_results;
|
||||
search_id_li = api_ids;
|
||||
search_id_li = display_ids;
|
||||
$journals_sess.entry.qry__status = 'done';
|
||||
});
|
||||
if (log_lvl)
|
||||
|
||||
Reference in New Issue
Block a user