aboutsummaryrefslogtreecommitdiff
path: root/src/left_panel.js
blob: 2aed1e63e1dfdc718b6cfc244886e762680c9376 (plain)
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
import { writable } from 'svelte/store';

let leftPanelCardId = writable('');
let leftPanelCardDesc = writable('');
let isMobileInfoVisible = writable(false);

let curVersion = 0;
let descCache = new Map();

function showMobileInfo() {
    isMobileInfoVisible.set(true);
}

function closeMobileInfo() {
    isMobileInfoVisible.set(false);
}

function setLeftPanelCard(id) {
    leftPanelCardId.set(id);
    curVersion += 1;
    leftPanelCardDesc.set('加载中...');
    let ver = curVersion;
    setDesc(ver, id);
}

function setDesc(version, id) {
    if (descCache.has(id)) {
        leftPanelCardDesc.set(descCache.get(id));
        return;
    }
    let descUrl = 'https://raye.mistivia.com/cardtext/' + id + '.txt';
    fetch(descUrl)
        .then((response) => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.text(); 
        })
        .then((desc) => {
            descCache.set(id, desc);
            if (version === curVersion) {
                leftPanelCardDesc.set(desc);
            }
        })
        .catch((error) => {
            console.error('Error fetching the file:', error);
        });
}

export {
    leftPanelCardId,
    leftPanelCardDesc,
    setLeftPanelCard,
    showMobileInfo,
    closeMobileInfo,
    isMobileInfoVisible,
};