File size: 24,535 Bytes
6044e02
 
 
 
 
 
 
 
 
0e8f914
 
 
 
6044e02
0e8f914
 
 
 
6044e02
0e8f914
6044e02
 
 
 
 
 
 
 
 
 
 
 
0e8f914
 
6044e02
 
 
 
 
0e8f914
 
6044e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e8f914
 
 
 
 
 
6044e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e8f914
 
6044e02
0e8f914
6044e02
 
 
 
 
 
0e8f914
6044e02
 
 
 
 
0e8f914
6044e02
 
0e8f914
6044e02
 
 
0e8f914
 
 
6044e02
0e8f914
 
6044e02
 
 
 
0e8f914
 
6044e02
 
 
 
 
 
 
 
 
 
0e8f914
6044e02
0e8f914
6044e02
 
 
 
 
 
 
0e8f914
6044e02
 
 
0e8f914
6044e02
0e8f914
6044e02
 
 
 
0e8f914
 
 
6044e02
 
 
 
 
 
0e8f914
6044e02
 
 
 
 
 
 
 
 
0e8f914
6044e02
0e8f914
 
6044e02
 
 
 
0e8f914
6044e02
 
 
 
 
0e8f914
6044e02
 
0e8f914
 
6044e02
 
 
 
 
 
0e8f914
 
 
 
 
 
6044e02
 
 
 
 
 
 
 
 
 
0e8f914
 
 
 
6044e02
 
 
0e8f914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6044e02
 
 
 
0e8f914
6044e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e8f914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6044e02
 
 
 
 
 
 
0e8f914
6044e02
 
 
 
0e8f914
6044e02
 
 
 
 
 
 
 
 
0e8f914
6044e02
 
 
 
 
 
0e8f914
6044e02
 
 
 
 
 
 
 
 
0e8f914
6044e02
 
0e8f914
 
 
 
6044e02
 
0e8f914
 
 
 
6044e02
3922044
0e8f914
 
 
 
3922044
 
0e8f914
 
 
 
ae7166a
 
0e8f914
 
 
 
ae7166a
 
3922044
 
 
 
 
 
0e8f914
3922044
 
 
 
 
 
 
 
ae7166a
0e8f914
 
ae7166a
 
 
 
 
 
 
 
 
3922044
0e8f914
ae7166a
0e8f914
3922044
 
0e8f914
 
3922044
 
0e8f914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3922044
 
 
 
 
 
 
 
 
ae7166a
0e8f914
 
 
 
 
ae7166a
 
 
 
0e8f914
 
 
ae7166a
 
0e8f914
ae7166a
 
 
 
 
3922044
 
 
 
c63db81
 
 
 
 
 
 
ae7166a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e8f914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3922044
 
ae7166a
0e8f914
 
 
 
 
 
 
 
 
 
 
 
3922044
0e8f914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95720c9
0e8f914
 
 
 
 
 
 
 
 
 
 
 
 
 
3922044
 
0e8f914
3922044
 
0e8f914
3922044
0e8f914
ae7166a
 
 
3922044
 
 
 
 
 
 
 
 
0e8f914
 
 
ae7166a
 
 
 
 
 
 
 
0e8f914
 
 
 
 
ae7166a
 
 
0e8f914
 
 
 
 
 
 
 
 
 
3922044
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae7166a
3922044
 
 
 
 
 
 
 
 
 
 
 
 
 
0e8f914
3922044
0e8f914
 
 
 
 
 
 
 
 
 
 
3922044
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HF Space Manager - 控制面板</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #00ff9d;
            --primary-dark: #00cc7d;
            --background-color: #0a0b0f;
            --card-background: #12141c;
            --text-primary: #ffffff;
            --text-secondary: #7f8ea3;
            --border-color: #1e2029;
            --success-color: #00ff9d;
            --warning-color: #ff9d00;
            --danger-color: #ff2d55;
            --sleeping-color: #00ffff;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        }

        body {
            background-color: var(--background-color);
            background-image: 
                radial-gradient(circle at 10% 20%, rgba(0, 255, 157, 0.03) 0%, transparent 20%),
                radial-gradient(circle at 90% 80%, rgba(255, 0, 255, 0.03) 0%, transparent 20%);
            min-height: 100vh;
            color: var(--text-primary);
        }

        .header {
            background: rgba(18, 20, 28, 0.95);
            backdrop-filter: blur(10px);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1000;
            border-bottom: 1px solid var(--border-color);
        }

        .header-content {
            max-width: 1400px;
            margin: 0 auto;
            padding: 1rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .nav-section {
            display: flex;
            align-items: center;
            gap: 2rem;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: 600;
            color: var(--text-primary);
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .logo i {
            color: var(--primary-color);
            text-shadow: 0 0 10px var(--primary-color);
        }

        .search-bar {
            position: relative;
            width: 300px;
        }

        .search-bar input {
            width: 100%;
            padding: 0.5rem 1rem 0.5rem 2.5rem;
            background: rgba(255, 255, 255, 0.05);
            border: 1px solid var(--border-color);
            border-radius: 6px;
            color: var(--text-primary);
            font-size: 0.9rem;
        }

        .search-bar i {
            position: absolute;
            left: 0.8rem;
            top: 50%;
            transform: translateY(-50%);
            color: var(--text-secondary);
        }

        .user-section {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .theme-toggle {
            background: none;
            border: none;
            color: var(--text-secondary);
            cursor: pointer;
            padding: 0.5rem;
            border-radius: 4px;
            transition: all 0.3s ease;
        }

        .theme-toggle:hover {
            color: var(--primary-color);
            background: rgba(0, 255, 157, 0.1);
        }

        .container {
            max-width: 1400px;
            margin: 80px auto 0;
            padding: 2rem;
        }

        .dashboard-header {
            margin-bottom: 2rem;
            padding: 1.5rem;
            background: var(--card-background);
            border-radius: 12px;
            border: 1px solid var(--border-color);
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1rem;
            margin-top: 1rem;
        }

        .stat-card {
            background: rgba(255, 255, 255, 0.03);
            padding: 1.5rem;
            border-radius: 8px;
            border: 1px solid var(--border-color);
            transition: all 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-2px);
            border-color: var(--primary-color);
        }

        .stat-value {
            font-size: 2rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
            color: var(--primary-color);
        }

        .stat-label {
            color: var(--text-secondary);
            font-size: 0.9rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .owner-section {
            background: var(--card-background);
            border-radius: 12px;
            margin-bottom: 2rem;
            border: 1px solid var(--border-color);
            overflow: hidden;
        }

        .owner-header {
            padding: 1.5rem;
            border-bottom: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: rgba(255, 255, 255, 0.02);
        }

        .owner-name {
            font-size: 1.25rem;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .status-stats {
            display: flex;
            gap: 1rem;
            flex-wrap: wrap;
        }

        .status-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 6px;
            font-size: 0.875rem;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .space-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 1.5rem;
            padding: 1.5rem;
        }

        .space-card {
            background: rgba(255, 255, 255, 0.02);
            border-radius: 10px;
            border: 1px solid var(--border-color);
            overflow: hidden;
            transition: all 0.3s ease;
        }

        .space-card:hover {
            transform: translateY(-2px);
            border-color: var(--primary-color);
            box-shadow: 0 0 20px rgba(0, 255, 157, 0.1);
        }

        .space-header {
            padding: 1rem;
            background: rgba(255, 255, 255, 0.02);
            border-bottom: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .space-content {
            padding: 1rem;
        }

        .space-info {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 1rem;
            margin-bottom: 1rem;
        }

        .info-item {
            display: flex;
            flex-direction: column;
            gap: 0.25rem;
        }

        .info-label {
            color: var(--text-secondary);
            font-size: 0.8rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .info-value {
            color: var(--text-primary);
            font-size: 0.9rem;
        }

        .space-metrics {
            padding: 1rem;
            background: rgba(255, 255, 255, 0.02);
            border-radius: 8px;
            margin-bottom: 1rem;
            display: flex;
            justify-content: space-around;
        }

        .metric-item {
            text-align: center;
        }

        .metric-value {
            font-size: 1.25rem;
            font-weight: 600;
            color: var(--primary-color);
        }

        .metric-label {
            font-size: 0.8rem;
            color: var(--text-secondary);
        }

        .action-buttons {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 0.75rem;
            padding: 1rem;
            background: rgba(0, 0, 0, 0.2);
        }

        .action-button {
            padding: 0.75rem;
            border-radius: 6px;
            font-size: 0.9rem;
            font-weight: 500;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            cursor: pointer;
            transition: all 0.3s ease;
            border: 1px solid var(--border-color);
            background: transparent;
            color: var(--text-primary);
            text-decoration: none;
        }

        .action-button:hover {
            border-color: var(--primary-color);
            background: rgba(0, 255, 157, 0.1);
        }

        .action-button.restart {
            border-color: var(--primary-color);
            color: var(--primary-color);
        }

        .action-button.restart:hover {
            background: var(--primary-color);
            color: var(--background-color);
        }

        .status-RUNNING { 
            background: rgba(0, 255, 157, 0.1);
            border: 1px solid var(--success-color);
            color: var(--success-color);
        }

        .status-BUILDING { 
            background: rgba(255, 157, 0, 0.1);
            border: 1px solid var(--warning-color);
            color: var(--warning-color);
        }

        .status-SLEEPING { 
            background: rgba(0, 255, 255, 0.1);
            border: 1px solid var(--sleeping-color);
            color: var(--sleeping-color);
        }

        .status-STOPPED { 
            background: rgba(127, 142, 163, 0.1);
            border: 1px solid var(--text-secondary);
            color: var(--text-secondary);
        }

        .status-FAILED { 
            background: rgba(255, 45, 85, 0.1);
            border: 1px solid var(--danger-color);
            color: var(--danger-color);
        }

        .loading-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(10, 11, 15, 0.8);
            backdrop-filter: blur(5px);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 9999;
        }

        .loading-spinner {
            position: relative;
            width: 60px;
            height: 60px;
        }

        .loading-spinner::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            border: 3px solid var(--border-color);
            border-top-color: var(--primary-color);
            animation: spin 1s infinite linear;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 1rem;
                padding: 1rem;
            }

            .nav-section {
                width: 100%;
                flex-direction: column;
                gap: 1rem;
            }

            .search-bar {
                width: 100%;
            }

            .container {
                padding: 1rem;
            }

            .stats-grid {
                grid-template-columns: 1fr;
            }

            .space-grid {
                grid-template-columns: 1fr;
            }

            .space-info {
                grid-template-columns: 1fr;
            }

            .status-stats {
                flex-direction: column;
                align-items: flex-start;
            }
        }
    </style>
</head>
<body>
    <div id="loading" class="loading-overlay">
        <div class="loading-spinner"></div>
    </div>

    <header class="header">
        <div class="header-content">
            <div class="nav-section">
                <div class="logo">
                    <i class="fas fa-server"></i>
                    HF Space Manager
                </div>
                <div class="search-bar">
                    <i class="fas fa-search"></i>
                    <input type="text" placeholder="搜索 Spaces..." id="spaceSearch">
                </div>
            </div>
            <div class="user-section">
                <button class="theme-toggle" title="切换主题">
                    <i class="fas fa-moon"></i>
                </button>
                <a href="/logout" class="action-button">
                    <i class="fas fa-sign-out-alt"></i>
                    退出
                </a>
            </div>
        </div>
    </header>

    <div class="container">
        {% if spaces %}
            {% set grouped_spaces = {} %}
            {% for space in spaces %}
                {% if space.owner not in grouped_spaces %}
                    {% set _ = grouped_spaces.update({space.owner: []}) %}
                {% endif %}
                {% set _ = grouped_spaces[space.owner].append(space) %}
            {% endfor %}

            <div class="dashboard-header">
                <div class="stats-grid">
                    {% set total_spaces = spaces|length %}
                    {% set running_spaces = spaces|selectattr('status', 'equalto', 'RUNNING')|list|length %}
                    {% set sleeping_spaces = spaces|selectattr('status', 'equalto', 'SLEEPING')|list|length %}
                    {% set stopped_spaces = spaces|selectattr('status', 'equalto', 'STOPPED')|list|length %}
                    {% set failed_spaces = spaces|selectattr('status', 'equalto', 'FAILED')|list|length %}
                    
                    <div class="stat-card">
                        <div class="stat-value">{{ total_spaces }}</div>
                        <div class="stat-label">总空间数</div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-value">{{ running_spaces }}</div>
                        <div class="stat-label">运行中</div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-value">{{ sleeping_spaces }}</div>
                        <div class="stat-label">休眠中</div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-value">{{ stopped_spaces }}</div>
                        <div class="stat-label">已停止</div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-value">{{ failed_spaces }}</div>
                        <div class="stat-label">运行失败</div>
                    </div>
                </div>
            </div>

            {% for owner, owner_spaces in grouped_spaces.items() %}
                {% set running_count = owner_spaces|selectattr('status', 'equalto', 'RUNNING')|list|length %}
                {% set building_count = owner_spaces|selectattr('status', 'equalto', 'BUILDING')|list|length %}
                {% set sleeping_count = owner_spaces|selectattr('status', 'equalto', 'SLEEPING')|list|length %}
                {% set stopped_count = owner_spaces|selectattr('status', 'equalto', 'STOPPED')|list|length %}
                {% set failed_count = owner_spaces|selectattr('status', 'equalto', 'FAILED')|list|length %}
                
                <div class="owner-section">
                    <div class="owner-header">
                        <div class="owner-name">
                            <i class="fas fa-user-circle"></i>
                            {{ owner }}
                        </div>
                        <div class="status-stats">
                            <span class="status-badge status-RUNNING">
                                <i class="fas fa-play-circle"></i>
                                运行中: {{ running_count }}
                            </span>
                            <span class="status-badge status-SLEEPING">
                                <i class="fas fa-moon"></i>
                                休眠: {{ sleeping_count }}
                            </span>
                            <span class="status-badge status-STOPPED">
                                <i class="fas fa-stop-circle"></i>
                                停止: {{ stopped_count }}
                            </span>
                            <span class="status-badge status-FAILED">
                                <i class="fas fa-exclamation-circle"></i>
                                失败: {{ failed_count }}
                            </span>
                        </div>
                    </div>
                    
                    <div class="space-grid">
                        {% for space in owner_spaces %}
                        <div class="space-card" data-space-id="{{ space.repo_id }}">
                            <div class="space-header">
                                <div class="space-name">
                                    <i class="fas fa-cube"></i>
                                    {{ space.name }}
                                </div>
                                <span class="status-badge status-{{ space.status }}">
                                    <i class="fas fa-circle"></i>
                                    {{ space.status }}
                                </span>
                            </div>
                            
                            <div class="space-content">
                                <div class="space-info">
                                    <div class="info-item">
                                        <span class="info-label">Space ID</span>
                                        <span class="info-value">{{ space.repo_id }}</span>
                                    </div>
                                    <div class="info-item">
                                        <span class="info-label">创建时间</span>
                                        <span class="info-value">{{ space.created_at }}</span>
                                    </div>
                                    <div class="info-item">
                                        <span class="info-label">最后修改</span>
                                        <span class="info-value">{{ space.last_modified }}</span>
                                    </div>
                                    <div class="info-item">
                                        <span class="info-label">应用端口</span>
                                        <span class="info-value">{{ space.app_port }}</span>
                                    </div>
                                </div>

                                <div class="space-metrics">
                                    <div class="metric-item">
                                        <div class="metric-value">{{ space.sdk }}</div>
                                        <div class="metric-label">SDK 版本</div>
                                    </div>
                                    <div class="metric-item">
                                        <div class="metric-value">{{ '私有' if space.private else '公开' }}</div>
                                        <div class="metric-label">访问权限</div>
                                    </div>
                                </div>
                            </div>

                            <div class="action-buttons">
                                <a href="{{ space.url }}" target="_blank" class="action-button">
                                    <i class="fas fa-external-link-alt"></i>
                                    查看
                                </a>
                                <button onclick="confirmAction('restart', '{{ space.repo_id }}')" class="action-button restart">
                                    <i class="fas fa-sync-alt"></i>
                                    重启
                                </button>
                                <button onclick="confirmAction('rebuild', '{{ space.repo_id }}')" class="action-button">
                                    <i class="fas fa-tools"></i>
                                    重建
                                </button>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            {% endfor %}
        {% else %}
            <div class="owner-section">
                <p style="text-align: center; padding: 2rem; color: var(--text-secondary);">
                    <i class="fas fa-info-circle"></i>
                    没有找到任何 Spaces。请确保你的账户中有创建的 Spaces,并且提供的 token 有正确的权限。
                </p>
            </div>
        {% endif %}
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
    <script>
        const socket = io();
        
        // 页面加载完成后隐藏加载动画
        window.addEventListener('load', function() {
            document.getElementById('loading').style.display = 'none';
        });

        // 搜索功能
        document.getElementById('spaceSearch').addEventListener('input', function(e) {
            const searchTerm = e.target.value.toLowerCase();
            document.querySelectorAll('.space-card').forEach(card => {
                const spaceName = card.querySelector('.space-name').textContent.toLowerCase();
                const spaceId = card.dataset.spaceId.toLowerCase();
                if (spaceName.includes(searchTerm) || spaceId.includes(searchTerm)) {
                    card.style.display = '';
                } else {
                    card.style.display = 'none';
                }
            });
        });

        // 主题切换
        const themeToggle = document.querySelector('.theme-toggle');
        themeToggle.addEventListener('click', function() {
            document.body.classList.toggle('light-theme');
            const icon = this.querySelector('i');
            icon.classList.toggle('fa-sun');
            icon.classList.toggle('fa-moon');
        });

        // WebSocket 连接
        socket.on('connect', () => {
            console.log('Connected to server');
        });

        socket.on('disconnect', () => {
            console.log('Disconnected from server');
        });

        socket.on('spaces_updated', (data) => {
            updateSpaceStatuses();
        });

        document.addEventListener('visibilitychange', function() {
            if (document.hidden) {
                socket.disconnect();
            } else {
                socket.connect();
            }
        });

        function updateSpaceStatuses() {
            document.querySelectorAll('.space-card').forEach(card => {
                const spaceId = card.dataset.spaceId;
                fetch(`/api/space/${spaceId}/status`)
                    .then(response => response.json())
                    .then(data => {
                        const statusElement = card.querySelector('.status-badge');
                        if (statusElement && data.status) {
                            statusElement.className = `status-badge status-${data.status}`;
                            statusElement.innerHTML = `<i class="fas fa-circle"></i> ${data.status}`;
                        }
                    })
                    .catch(error => console.error('Error updating status:', error));
            });
        }

        function confirmAction(action, spaceId) {
            const actionText = action === 'restart' ? '重启' : '重建';
            if (confirm(`确定要${actionText} "${spaceId}" 吗?`)) {
                document.getElementById('loading').style.display = 'flex';
                window.location.href = `/action/${action}/${spaceId}`;
            }
        }

        // 每30秒更新状态
        setInterval(updateSpaceStatuses, 30000);

        // 添加卡片动画
        document.querySelectorAll('.space-card').forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.style.transform = 'translateY(-5px)';
            });
            
            card.addEventListener('mouseleave', function() {
                this.style.transform = 'translateY(0)';
            });
        });
    </script>
</body>
</html>