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)
})
}
/**
* 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,
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

View File

@ -1674,27 +1674,35 @@ export class Me {
* @param {SimpleChat} chat
*/
setup_load(div, chat) {
if (!(chat.ods_key() in localStorage)) {
return;
}
div.innerHTML += `<p class="role-system">Restore</p>
<p>Load previously saved chat session, if available</p>`;
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 += `<p class="role-system">Restore</p>
<p>Load previously saved chat session, if available</p>`;
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);
})
}
/**