File size: 4,866 Bytes
f22a7f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// static/js/settings.js

let currentAccentColor = '#ffc107';

function switchSettingsPanel(panelId, element) {
    // Hide all panels
    document.querySelectorAll('.settings-section').forEach(p => p.classList.remove('active'));
    // Show target
    document.getElementById(panelId).classList.add('active');
    
    // Update tabs
    document.querySelectorAll('.settings-tab').forEach(t => t.classList.remove('active'));
    element.classList.add('active');
}

function setAccentColor(hexCode, element) {
    currentAccentColor = hexCode;
    
    // Update swatches visually
    document.querySelectorAll('.color-swatch').forEach(s => s.classList.remove('active'));
    if(element) {
        element.classList.add('active');
    }
    
    // Update the custom color picker input to match if needed
    document.getElementById('custom-accent-color').value = hexCode;
    
    previewSettings();
}

function previewSettings() {
    // 1. Apply Accent Color
    document.documentElement.style.setProperty('--accent-main', currentAccentColor);
    // Generate a softer glow color based on the hex (simplified logic)
    document.documentElement.style.setProperty('--accent-glow', currentAccentColor + '40'); 
    
    // 2. Apply Theme (Light/Dark)
    const theme = document.getElementById('setting-theme-mode').value;
    if (theme === 'light') {
        document.documentElement.style.setProperty('--bg-base', '#f5f5f5');
        document.documentElement.style.setProperty('--bg-surface', '#ffffff');
        document.documentElement.style.setProperty('--text-primary', '#111111');
        document.documentElement.style.setProperty('--text-secondary', '#555555');
        document.documentElement.style.setProperty('--glass-border', '1px solid rgba(0,0,0,0.1)');
        document.documentElement.style.setProperty('--panel-bg', 'rgba(255,255,255,0.8)');
    } else {
        // Revert to Dark
        document.documentElement.style.setProperty('--bg-base', '#050505');
        document.documentElement.style.setProperty('--bg-surface', '#0a0a0a');
        document.documentElement.style.setProperty('--text-primary', '#ffffff');
        document.documentElement.style.setProperty('--text-secondary', '#a0a0a0');
        document.documentElement.style.setProperty('--glass-border', '1px solid rgba(255, 255, 255, 0.05)');
        document.documentElement.style.setProperty('--panel-bg', 'rgba(20, 20, 20, 0.6)');
    }

    // 3. Apply Editor Settings
    const editorFont = document.getElementById('setting-editor-font').value;
    const editorSize = document.getElementById('setting-editor-font-size').value;
    document.documentElement.style.setProperty('--font-mono', editorFont);
    
    if (typeof editor !== 'undefined' && editor) {
        editor.setOptions({
            fontFamily: editorFont,
            fontSize: editorSize
        });
    }

    // 4. Apply Terminal Settings
    const termSize = document.getElementById('setting-terminal-font-size').value;
    const termOutput = document.getElementById('terminal-output');
    if (termOutput) {
        termOutput.style.fontSize = termSize;
    }
}

async function saveSettings() {
    const btn = document.getElementById('save-settings-btn');
    btn.innerText = "Saving...";
    
    const settingsData = {
        theme: document.getElementById('setting-theme-mode').value,
        accent: currentAccentColor,
        editorFont: document.getElementById('setting-editor-font').value,
        editorSize: document.getElementById('setting-editor-font-size').value,
        termSize: document.getElementById('setting-terminal-font-size').value
    };

    try {
        const res = await fetch('/api/settings', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({token: currentToken, settings: settingsData})
        });
        
        const data = await res.json();
        if(data.success) {
            btn.innerText = "✓ Saved";
            setTimeout(() => btn.innerText = "Save Settings", 2000);
        } else {
            alert("Failed to save settings.");
            btn.innerText = "Save Settings";
        }
    } catch (e) {
        alert("Network error.");
        btn.innerText = "Save Settings";
    }
}

// Called on login/boot
async function loadSettings() {
    if(!currentUser) return;
    
    // Set username in profile
    const usernameInput = document.getElementById('setting-username');
    const avatar = document.getElementById('profile-avatar');
    if(usernameInput) usernameInput.value = currentUser;
    if(avatar) avatar.innerText = currentUser.charAt(0).toUpperCase();

    // In a full app, we would fetch the user's specific settings from the DB here via a GET route.
    // For now, we apply the default UI state so everything looks perfect immediately.
    previewSettings();
}