SimpleChatTCRV:iDB:GetKeys: helps decide whether restore btn shown

This commit is contained in:
hanishkvc 2025-11-12 02:57:40 +05:30
parent 570131943f
commit ca8e6ab1a6
3 changed files with 58 additions and 20 deletions

View File

@ -103,3 +103,31 @@ export function db_get(dbName, storeName, key, callerTag, cb) {
cb(false, errReason) 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)
})
}

View File

@ -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, * UI Cleanup - msgs spaced out, toolcall edit hr not always, scroll ui only when required,
hide settings/info till user requests, heading gradient 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 #### ToDo

View File

@ -1674,27 +1674,35 @@ export class Me {
* @param {SimpleChat} chat * @param {SimpleChat} chat
*/ */
setup_load(div, chat) { setup_load(div, chat) {
if (!(chat.ods_key() in localStorage)) { let tag = `Me:Load:${chat.chatId}`;
return; mIdb.db_getkeys(DB_NAME, DB_STORE, tag, (status, related)=>{
} if (!status || (related == null)) {
div.innerHTML += `<p class="role-system">Restore</p> return
<p>Load previously saved chat session, if available</p>`; }
let btn = ui.el_create_button(chat.ods_key(), (ev)=>{ if (related.constructor.name == DOMException.name) {
let tag = `Me:Load:${chat.chatId}`; return
console.log(`DBUG:${tag}`, chat); }
chat.load((loadStatus, dbStatus, related)=>{ if (/** @type {IDBValidKey[]} */(related).indexOf(chat.ods_key()) == -1) {
if (!loadStatus || !dbStatus) { return;
console.log(`WARN:${tag}:DidntLoad:${loadStatus}:${dbStatus}:${related}`); }
return; div.innerHTML += `<p class="role-system">Restore</p>
} <p>Load previously saved chat session, if available</p>`;
console.log(`INFO:${tag}:Loaded:${loadStatus}:${dbStatus}`); let btn = ui.el_create_button(chat.ods_key(), (ev)=>{
queueMicrotask(()=>{ console.log(`DBUG:${tag}`, chat);
this.multiChat.chat_show(chat.chatId, true, true); chat.load((loadStatus, dbStatus, related)=>{
this.multiChat.elInSystem.value = chat.get_system_latest().ns.getContent(); 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); })
} }
/** /**