From ca8e6ab1a61bcf962ddf98fda3812b50d037b557 Mon Sep 17 00:00:00 2001 From: hanishkvc Date: Wed, 12 Nov 2025 02:57:40 +0530 Subject: [PATCH] SimpleChatTCRV:iDB:GetKeys: helps decide whether restore btn shown --- tools/server/public_simplechat/idb.mjs | 28 ++++++++++++ tools/server/public_simplechat/readme.md | 4 +- tools/server/public_simplechat/simplechat.js | 46 ++++++++++++-------- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/tools/server/public_simplechat/idb.mjs b/tools/server/public_simplechat/idb.mjs index 3b065996d3..1a6b559a83 100644 --- a/tools/server/public_simplechat/idb.mjs +++ b/tools/server/public_simplechat/idb.mjs @@ -103,3 +103,31 @@ export function db_get(dbName, storeName, key, callerTag, cb) { cb(false, errReason) }) } + + +/** + * Return all keys from a store in a db, + * through the provided callback. + * + * @param {string} dbName + * @param {string} storeName + * @param {string | undefined} callerTag + * @param {(status: boolean, related: IDBValidKey[] | DOMException | null) => void} cb + */ +export function db_getkeys(dbName, storeName, callerTag, cb) { + let tag = `iDB:GetKeys:${callerTag}`; + db_open(dbName, storeName, tag).then((/** @type {IDBDatabase} */db)=>{ + let reqGet = db_trans_store(db, storeName, 'readonly').getAllKeys(); + reqGet.onsuccess = (evGet) => { + console.info(`DBUG:${tag}:transact success`) + cb(true, reqGet.result) + } + reqGet.onerror = (evGet) => { + console.info(`ERRR:${tag}:OnError:transact failed:${reqGet.error}`) + cb(false, reqGet.error) + } + }).catch((errReason)=>{ + console.info(`ERRR:${tag}:Caught:transact failed:${errReason}`) + cb(false, errReason) + }) +} diff --git a/tools/server/public_simplechat/readme.md b/tools/server/public_simplechat/readme.md index 55cad4cd3b..b57fe0c3aa 100644 --- a/tools/server/public_simplechat/readme.md +++ b/tools/server/public_simplechat/readme.md @@ -737,7 +737,9 @@ sliding window based drop off or even before they kick in, this can help in many * UI Cleanup - msgs spaced out, toolcall edit hr not always, scroll ui only when required, hide settings/info till user requests, heading gradient -* iDB module - add open, transact, put and get. Use for chat session save and load +* iDB module + * add open, transact, put and get. Use for chat session save and load + * getKeys used to show Restore/Load button wrt chat sessions. #### ToDo diff --git a/tools/server/public_simplechat/simplechat.js b/tools/server/public_simplechat/simplechat.js index 78e15490a3..448182a5a7 100644 --- a/tools/server/public_simplechat/simplechat.js +++ b/tools/server/public_simplechat/simplechat.js @@ -1674,27 +1674,35 @@ export class Me { * @param {SimpleChat} chat */ setup_load(div, chat) { - if (!(chat.ods_key() in localStorage)) { - return; - } - div.innerHTML += `

Restore

-

Load previously saved chat session, if available

`; - let btn = ui.el_create_button(chat.ods_key(), (ev)=>{ - let tag = `Me:Load:${chat.chatId}`; - console.log(`DBUG:${tag}`, chat); - chat.load((loadStatus, dbStatus, related)=>{ - if (!loadStatus || !dbStatus) { - console.log(`WARN:${tag}:DidntLoad:${loadStatus}:${dbStatus}:${related}`); - return; - } - console.log(`INFO:${tag}:Loaded:${loadStatus}:${dbStatus}`); - queueMicrotask(()=>{ - this.multiChat.chat_show(chat.chatId, true, true); - this.multiChat.elInSystem.value = chat.get_system_latest().ns.getContent(); + let tag = `Me:Load:${chat.chatId}`; + mIdb.db_getkeys(DB_NAME, DB_STORE, tag, (status, related)=>{ + if (!status || (related == null)) { + return + } + if (related.constructor.name == DOMException.name) { + return + } + if (/** @type {IDBValidKey[]} */(related).indexOf(chat.ods_key()) == -1) { + return; + } + div.innerHTML += `

Restore

+

Load previously saved chat session, if available

`; + let btn = ui.el_create_button(chat.ods_key(), (ev)=>{ + console.log(`DBUG:${tag}`, chat); + chat.load((loadStatus, dbStatus, related)=>{ + if (!loadStatus || !dbStatus) { + console.log(`WARN:${tag}:DidntLoad:${loadStatus}:${dbStatus}:${related}`); + return; + } + console.log(`INFO:${tag}:Loaded:${loadStatus}:${dbStatus}`); + queueMicrotask(()=>{ + this.multiChat.chat_show(chat.chatId, true, true); + this.multiChat.elInSystem.value = chat.get_system_latest().ns.getContent(); + }); }); }); - }); - div.appendChild(btn); + div.appendChild(btn); + }) } /**