File size: 3,163 Bytes
970785f
dd11db6
6938328
 
dd11db6
 
970785f
dd11db6
 
 
 
970785f
dd11db6
 
 
 
 
 
 
 
 
 
 
970785f
dd11db6
 
 
 
 
 
 
 
 
 
6938328
b30bb95
 
ef52f60
b30bb95
 
 
 
4baa2bc
b30bb95
 
 
 
 
ef52f60
 
 
 
 
4baa2bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b30bb95
dd11db6
 
 
b30bb95
 
 
 
dd11db6
 
 
 
b30bb95
ef52f60
b30bb95
dd11db6
b30bb95
 
 
 
 
6938328
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { datetimer } from "./datetimer.js";
class ChatStorageItem {
    constructor(chat_history_storage) {
        this.chat_history_storage = chat_history_storage;
    }
    create_index() {
        let datetime_string = datetimer.now_with_ms_as_pathname();
        let chat_index = `chat_${datetime_string}`;
        return chat_index;
    }
    create_title() {
        let chat_title = datetimer.now();
        return chat_title;
    }
    get_messagers_container_html() {
        let messagers_container = $("#messagers-container");
        if (messagers_container.children().length > 0) {
            return messagers_container[0].outHTML;
        } else {
            return null;
        }
    }
    get_current_datetime_string() {
        return datetimer.now_with_ms();
    }
    construct() {
        this.html = this.get_messagers_container_html();
        this.index = this.create_index();
        this.title = this.create_title();
        this.saved_datetime = this.get_current_datetime_string();
        this.message_count = $("#messagers-container").children().length;
    }
}

class ChatHistoryStorage {
    constructor() {
        this.init_database();
        this.render_chat_history_sidebar_items();
    }
    init_database() {
        this.db = new Dexie("chat_history");
        this.db.version(1).stores({
            chat_history: "index, title, html, saved_datetime",
        });
        this.db.chat_history.count((count) => {
            console.log(`${count} records loaded from chat_history.`);
        });
    }
    clear_database() {
        this.db.chat_history.clear();
        this.render_chat_history_sidebar_items();
        console.log("chat_history cleared.");
    }
    render_chat_history_sidebar_items() {
        let chat_history_sidebar_items = $("#chat-history-sidebar-items");
        let chat_history = this.db.chat_history;
        chat_history_sidebar_items.empty();
        chat_history.each((chat_history_item) => {
            let chat_history_item_html = `
                <li class="nav-item">
                    <a class="nav-link" href="#${chat_history_item.index}">
                    ${chat_history_item.title}
                    </a>
                </li>
            `;
            chat_history_sidebar_items.append(chat_history_item_html);
        });
    }
    save_current_chat_session() {
        let chat_storage_item = new ChatStorageItem(this);
        chat_storage_item.construct();
        if (chat_storage_item.html === null) {
            console.log("Empty messagers_container, no chat session to save.");
            return;
        } else {
            this.db.chat_history.put({
                index: chat_storage_item.index,
                title: chat_storage_item.title,
                html: chat_storage_item.html,
                saved_datetime: chat_storage_item.saved_datetime,
            });
            this.render_chat_history_sidebar_items();
            console.log(
                `${chat_storage_item.message_count} messages saved at ${chat_storage_item.saved_datetime}.`
            );
        }
    }
}

export let chat_history_storage = new ChatHistoryStorage();