File size: 2,899 Bytes
0632cd1
 
 
 
 
 
 
56779ed
0632cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56779ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const sendButton = document.getElementById('sendButton')
const textInput = document.getElementById('textInput')

function createNewMessage(text, type) {
    const message = document.createElement('div')
    message.className = 'message'
    let content = document.createElement('div')
    content.style.whiteSpace = "pre-line"
    if (type === 'bot') {
        content.className = 'bot_message mt-4 py-3 px-4 rounded-4 w-75 ms-3'
    } else {
        content.className = 'user_message mt-4 py-3 px-4 rounded-4 w-75 me-3'
    }
    content.innerText = text
    message.appendChild(content)
    chatBody.appendChild(message)
}

function sendMessageToServer() {
    const userQuery = textInput.value;
    if (userQuery.length === 0) {
        return
    }
    createNewMessage(userQuery, 'user')
    socket.send(JSON.stringify({'query': userQuery, 'country': "Undefined"}));
    textInput.value = ''
    isFirstWord = false
    chatBody.scrollTop = chatBody.scrollHeight;
}

function generateUUID() {
    const arr = new Uint8Array(16);
    window.crypto.getRandomValues(arr);

    arr[6] = (arr[6] & 0x0f) | 0x40;
    arr[8] = (arr[8] & 0x3f) | 0x80;

    return ([...arr].map((b, i) =>
        (i === 4 || i === 6 || i === 8 || i === 10 ? "-" : "") + b.toString(16).padStart(2, "0")
    ).join(""));
}

textInput.addEventListener("keydown", function (event) {
    if (event.key === "Enter") {
        sendMessageToServer();
    }
});

sendButton.addEventListener("click", sendMessageToServer);


function enrichAIResponse(botMessageElement) {
    const links = botMessageElement.querySelectorAll('.extraDataLink');

    links.forEach(link => {
        const tooltip = link.nextElementSibling;
        link.addEventListener('mouseenter', function () {
            tooltip.style.display = 'block';
            const tooltipWidth = parseInt(link.offsetWidth) * 2
            tooltip.style.width = tooltipWidth + 'px'

            const tooltipImg = tooltip.querySelector('.tooltip-img')
            if (tooltipImg) {
                tooltipImg.width = tooltipWidth - 16
            }
            const linkRect = this.getBoundingClientRect();
            if (linkRect.top < tooltip.offsetHeight + 4) {
                tooltip.style.top = (window.scrollY + linkRect.bottom + 4) + 'px';
            } else {
                tooltip.style.top = (window.scrollY + linkRect.top - tooltip.offsetHeight - 4) + 'px';
            }
            tooltip.style.left = (linkRect.left + (linkRect.width / 2) - (tooltipWidth / 2)) + 'px';
        });

        link.addEventListener('mouseleave', function () {
            setTimeout(() => {
                if (!tooltip.matches(':hover')) {
                    tooltip.style.display = 'none';
                }
            }, 300)
        });

        tooltip.addEventListener('mouseleave', function () {
            this.style.display = 'none';
        });
    });
}