-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-viewer.html
More file actions
147 lines (129 loc) · 6.19 KB
/
code-viewer.html
File metadata and controls
147 lines (129 loc) · 6.19 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shadow AI — Code Viewer</title>
<link rel="stylesheet" href="code-viewer-styles.css">
</head>
<body>
<div class="app-shell">
<!-- Title Bar -->
<header class="titlebar" style="-webkit-app-region: drag;">
<div class="titlebar-left">
<svg class="titlebar-icon" width="16" height="16" viewBox="0 0 32 32" fill="none" stroke="currentColor"
stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">
<polyline points="10 8 4 16 10 24" />
<polyline points="22 8 28 16 22 24" />
<line x1="18" y1="6" x2="14" y2="26" stroke-dasharray="2 2" />
</svg>
<span class="titlebar-title">Code Snippets</span>
<span class="snippet-count" id="snippet-count">0</span>
</div>
<div class="titlebar-right" style="-webkit-app-region: no-drag;">
<button class="titlebar-btn" id="btn-close" title="Close">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
</button>
</div>
</header>
<!-- Snippet List -->
<div class="snippet-list" id="snippet-list">
<div class="empty-state" id="empty-state">
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" stroke-width="1.2"
opacity="0.3">
<polyline points="10 8 4 16 10 24" />
<polyline points="22 8 28 16 22 24" />
</svg>
<p>No code snippets yet</p>
<span>Ask a coding question to generate snippets</span>
</div>
</div>
</div>
<script>
// ── DOM References ──
const snippetList = document.getElementById('snippet-list');
const snippetCount = document.getElementById('snippet-count');
const emptyState = document.getElementById('empty-state');
// ── Close button ──
document.getElementById('btn-close').addEventListener('click', () => {
window.electronAPI.closeWindow();
});
// ── Receive snippets from main process ──
window.electronAPI.onCodeSnippets((snippets) => {
renderSnippets(snippets);
});
function renderSnippets(snippets) {
// Update count
snippetCount.textContent = snippets.length;
if (snippets.length === 0) {
emptyState.style.display = 'flex';
return;
}
emptyState.style.display = 'none';
// Group by problem name
const grouped = {};
snippets.forEach(s => {
if (!grouped[s.problemName]) grouped[s.problemName] = [];
grouped[s.problemName].push(s);
});
// Clear and render
snippetList.innerHTML = '';
for (const [problemName, items] of Object.entries(grouped)) {
const group = document.createElement('div');
group.className = 'snippet-group';
group.innerHTML = `<div class="group-header">${escapeHtml(problemName)}</div>`;
let naiveSnippet = null;
let optimalSnippet = null;
items.forEach(snippet => {
// Track naive and optimal for compare button
if (snippet.tag === 'naive' && !naiveSnippet) naiveSnippet = snippet;
if (snippet.tag === 'optimal' && !optimalSnippet) optimalSnippet = snippet;
const card = document.createElement('div');
card.className = 'snippet-card';
card.innerHTML = `
<div class="snippet-card-top">
<span class="tag tag-${snippet.tag}">${snippet.tag.toUpperCase()}</span>
<span class="snippet-lang">${snippet.language}</span>
</div>
<pre class="snippet-preview"><code>${escapeHtml(snippet.code.substring(0, 120))}${snippet.code.length > 120 ? '...' : ''}</code></pre>
<div class="snippet-meta">${snippet.timestamp}</div>
`;
card.addEventListener('click', () => {
window.electronAPI.openCodeWindow(snippet);
});
group.appendChild(card);
});
// Add Compare button if both naive and optimal exist
if (naiveSnippet && optimalSnippet) {
const compareBtn = document.createElement('button');
compareBtn.className = 'compare-btn';
compareBtn.innerHTML = `
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="2" y="3" width="8" height="18" rx="1" />
<rect x="14" y="3" width="8" height="18" rx="1" />
</svg>
Compare Naive vs Optimal
`;
compareBtn.addEventListener('click', (e) => {
e.stopPropagation();
window.electronAPI.openCodeCompare({
problemName: problemName,
naive: naiveSnippet,
optimal: optimalSnippet
});
});
group.appendChild(compareBtn);
}
snippetList.appendChild(group);
}
}
function escapeHtml(text) {
const map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
return text.replace(/[&<>"']/g, m => map[m]);
}
</script>
</body>
</html>