Bug fixes and clean up for the recent history of Journal Entries.
This commit is contained in:
@@ -129,57 +129,106 @@ let lq__journal_entry_obj = $derived(liveQuery(async () => {
|
||||
return results;
|
||||
}));
|
||||
|
||||
|
||||
// $effect(() => {
|
||||
// if (browser && $lq__journal_entry_obj?.journal_entry_id) {
|
||||
|
||||
// // $journals_loc.entry_view_history_li = [...new Set($journals_loc.entry_view_history_li)]
|
||||
|
||||
// let tmp_history_li = [
|
||||
// ...new Set($journals_loc?.entry_view_history_li ?? [])
|
||||
// ];
|
||||
|
||||
// // Limit to last 15 entries
|
||||
// if (tmp_history_li.length > 15) {
|
||||
// tmp_history_li = tmp_history_li.slice(tmp_history_li.length - 15);
|
||||
// }
|
||||
|
||||
// // let chk_history_li = tmp_history_li?.filter(item => item.id === $lq__journal_entry_obj?.journal_entry_id);
|
||||
|
||||
// // if (chk_history_li?.length) {
|
||||
// // // Already in history, do not add again
|
||||
// // console.log(`Entry ID = ${$lq__journal_entry_obj?.journal_entry_id} already in history, not adding again.`, tmp_history_li);
|
||||
|
||||
// // // if (tmp_history_li !== $journals_loc.entry_view_history_li) {
|
||||
// // if (JSON.stringify(tmp_history_li) !== JSON.stringify($journals_loc.entry_view_history_li)) {
|
||||
// // $journals_loc.entry_view_history_li = tmp_history_li;
|
||||
|
||||
// // console.log(`$journals_loc.entry_view_history_li = `, $journals_loc.entry_view_history_li);
|
||||
// // }
|
||||
|
||||
// // return;
|
||||
// // }
|
||||
|
||||
// tmp_history_li.push({
|
||||
// id: $lq__journal_entry_obj?.journal_entry_id ?? 'NONE',
|
||||
// name: $lq__journal_entry_obj?.name ?? ae_util.iso_datetime_formatter($lq__journal_entry_obj?.created_on, 'datetime_iso_12_no_seconds'),
|
||||
// url: `/journals/${$lq__journal_entry_obj?.journal_id ?? 'NONE'}/entry/${$lq__journal_entry_obj?.journal_entry_id ?? 'NONE'}`,
|
||||
// });
|
||||
|
||||
// // Remove duplicates and keep most recent
|
||||
// tmp_history_li = [...new Set(tmp_history_li.map(item => JSON.stringify(item)))].map(item => JSON.parse(item));
|
||||
|
||||
// // Limit to last 15 entries
|
||||
// if (tmp_history_li.length > 15) {
|
||||
// tmp_history_li = tmp_history_li.slice(tmp_history_li.length - 15);
|
||||
// }
|
||||
|
||||
// if (JSON.stringify(tmp_history_li) !== JSON.stringify($journals_loc?.entry_view_history_li)) {
|
||||
// $journals_loc.entry_view_history_li = tmp_history_li;
|
||||
|
||||
// console.log(`$journals_loc.entry_view_history_li = `, $journals_loc?.entry_view_history_li);
|
||||
// }
|
||||
|
||||
// console.log(`$journals_loc.entry_view_history_li = `, $journals_loc?.entry_view_history_li);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
$effect(() => {
|
||||
if (browser && $lq__journal_entry_obj?.journal_entry_id) {
|
||||
log_lvl = 2;
|
||||
// Start with the current KV or convert the LI to a KV if needed
|
||||
let history_kv = { ...( $journals_loc?.entry_view_history_kv ?? {} ) };
|
||||
|
||||
// $journals_loc.entry_view_history_li = [...new Set($journals_loc.entry_view_history_li)]
|
||||
// Add or update the current entry
|
||||
const entry_id = $lq__journal_entry_obj?.journal_entry_id ?? 'NONE';
|
||||
history_kv[entry_id] = {
|
||||
id: entry_id,
|
||||
name: $lq__journal_entry_obj?.name ?? ae_util.iso_datetime_formatter($lq__journal_entry_obj?.created_on, 'datetime_iso_12_no_seconds'),
|
||||
url: `/journals/${$lq__journal_entry_obj?.journal_id ?? 'NONE'}/entry/${entry_id}`,
|
||||
};
|
||||
|
||||
let tmp_history_li = [
|
||||
...new Set($journals_loc.entry_view_history_li)
|
||||
];
|
||||
console.log(`history_kv (before limiting) = `, history_kv);
|
||||
|
||||
// Limit to last 15 entries
|
||||
if (tmp_history_li.length > 15) {
|
||||
tmp_history_li = tmp_history_li.slice(tmp_history_li.length - 15);
|
||||
}
|
||||
// // Convert KV to array, sort by most recent (last updated), and limit to 15
|
||||
// let history_li = Object.values(history_kv);
|
||||
|
||||
// let chk_history_li = tmp_history_li?.filter(item => item.id === $lq__journal_entry_obj?.journal_entry_id);
|
||||
// console.log(`history_li (before limiting) = `, history_li);
|
||||
|
||||
// if (chk_history_li?.length) {
|
||||
// // Already in history, do not add again
|
||||
// console.log(`Entry ID = ${$lq__journal_entry_obj?.journal_entry_id} already in history, not adding again.`, tmp_history_li);
|
||||
|
||||
// // if (tmp_history_li !== $journals_loc.entry_view_history_li) {
|
||||
// if (JSON.stringify(tmp_history_li) !== JSON.stringify($journals_loc.entry_view_history_li)) {
|
||||
// $journals_loc.entry_view_history_li = tmp_history_li;
|
||||
|
||||
// console.log(`$journals_loc.entry_view_history_li = `, $journals_loc.entry_view_history_li);
|
||||
// // If you want to keep the most recent 15, you can use the order of insertion.
|
||||
// // To do this, remove the oldest if over 15.
|
||||
// if (history_li.length > 15) {
|
||||
// // Remove the oldest entries (by insertion order)
|
||||
// // Get the keys in insertion order
|
||||
// const keys = Object.keys(history_kv);
|
||||
// const keys_to_remove = keys.slice(0, history_li.length - 15);
|
||||
// for (const key of keys_to_remove) {
|
||||
// delete history_kv[key];
|
||||
// }
|
||||
|
||||
// return;
|
||||
// }
|
||||
|
||||
tmp_history_li.push({
|
||||
id: $lq__journal_entry_obj?.journal_entry_id ?? 'NONE',
|
||||
name: $lq__journal_entry_obj?.name ?? ae_util.iso_datetime_formatter($lq__journal_entry_obj?.created_on, 'datetime_iso_12_no_seconds'),
|
||||
url: `/journals/${$lq__journal_entry_obj?.journal_id ?? 'NONE'}/entry/${$lq__journal_entry_obj?.journal_entry_id ?? 'NONE'}`,
|
||||
});
|
||||
|
||||
// Remove duplicates and keep most recent
|
||||
tmp_history_li = [...new Set(tmp_history_li.map(item => JSON.stringify(item)))].map(item => JSON.parse(item));
|
||||
|
||||
// Limit to last 15 entries
|
||||
if (tmp_history_li.length > 15) {
|
||||
tmp_history_li = tmp_history_li.slice(tmp_history_li.length - 15);
|
||||
// Only update if changed
|
||||
if (JSON.stringify(history_kv) !== JSON.stringify($journals_loc?.entry_view_history_kv)) {
|
||||
$journals_loc.entry_view_history_kv = history_kv;
|
||||
console.log(`$journals_loc.entry_view_history_kv = `, $journals_loc.entry_view_history_kv);
|
||||
} else {
|
||||
if (log_lvl > 1) {
|
||||
console.log(`$journals_loc.entry_view_history_kv has not changed.`);
|
||||
}
|
||||
}
|
||||
|
||||
if (JSON.stringify(tmp_history_li) !== JSON.stringify($journals_loc.entry_view_history_li)) {
|
||||
$journals_loc.entry_view_history_li = tmp_history_li;
|
||||
|
||||
console.log(`$journals_loc.entry_view_history_li = `, $journals_loc.entry_view_history_li);
|
||||
}
|
||||
|
||||
console.log(`$journals_loc.entry_view_history_li = `, $journals_loc.entry_view_history_li);
|
||||
// log_lvl = 1;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user