File size: 21,005 Bytes
63de3ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Bloggig - AI Blog Writing Agent</title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap"
      rel="stylesheet"
    />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css"
    />
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <style>
      :root {
        --bg-dark: #0f1117;
        --sidebar-bg: #161922;
        --accent: #6366f1;
        --accent-hover: #4f46e5;
        --text-primary: #f8fafc;
        --text-secondary: #94a3b8;
        --glass-bg: rgba(255, 255, 255, 0.03);
        --border: rgba(255, 255, 255, 0.1);
        --card-bg: #1e293b;
      }

      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: "Inter", sans-serif;
      }

      body {
        background-color: var(--bg-dark);
        color: var(--text-primary);
        height: 100vh;
        display: flex;
        overflow: hidden;
      }

      /* Sidebar Styles */
      aside {
        width: 300px;
        background-color: var(--sidebar-bg);
        border-right: 1px solid var(--border);
        display: flex;
        flex-direction: column;
        padding: 1.5rem;
        flex-shrink: 0;
        transition: transform 0.3s ease;
      }

      .logo {
        font-size: 1.5rem;
        font-weight: 700;
        margin-bottom: 2rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        color: var(--accent);
      }

      .new-chat-btn {
        background: var(--accent);
        color: white;
        border: none;
        padding: 0.8rem;
        border-radius: 0.5rem;
        cursor: pointer;
        font-weight: 600;
        margin-bottom: 1.5rem;
        transition: all 0.2s;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
      }

      .new-chat-btn:hover {
        background: var(--accent-hover);
        transform: translateY(-1px);
      }

      .history-list {
        flex: 1;
        overflow-y: auto;
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
      }

      .history-item {
        padding: 0.75rem;
        border-radius: 0.4rem;
        cursor: pointer;
        transition: background 0.2s;
        font-size: 0.9rem;
        color: var(--text-secondary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        border: 1px solid transparent;
      }

      .history-item:hover {
        background: var(--glass-bg);
        color: var(--text-primary);
        border-color: var(--border);
      }

      .history-item.active {
        background: rgba(99, 102, 241, 0.15);
        color: var(--accent);
        border-color: rgba(99, 102, 241, 0.3);
      }

      /* Main Content Styles */
      main {
        flex: 1;
        display: flex;
        flex-direction: column;
        position: relative;
        background: radial-gradient(
          circle at bottom right,
          rgba(99, 102, 241, 0.05),
          transparent
        );
      }

      header {
        height: 60px;
        border-bottom: 1px solid var(--border);
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 2rem;
        background: rgba(15, 17, 23, 0.8);
        backdrop-filter: blur(8px);
        z-index: 10;
      }

      .blog-actions {
        display: flex;
        gap: 1rem;
      }

      .btn-icon {
        background: transparent;
        border: 1px solid var(--border);
        color: var(--text-secondary);
        padding: 0.5rem;
        border-radius: 0.4rem;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 0.4rem;
        font-weight: 500;
        transition: all 0.2s;
      }

      .btn-icon:hover:not(:disabled) {
        background: var(--glass-bg);
        color: var(--text-primary);
        border-color: var(--text-secondary);
      }

      .btn-delete:hover:not(:disabled) {
        background: rgba(239, 68, 68, 0.1);
        color: #ef4444;
        border-color: rgba(239, 68, 68, 0.3);
      }

      /* Chat/Content Area */
      #content-area {
        flex: 1;
        overflow-y: auto;
        padding: 2rem;
        display: flex;
        flex-direction: column;
        align-items: center;
      }

      .welcome-screen {
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        text-align: center;
        max-width: 600px;
      }

      .welcome-screen h1 {
        font-size: 2.5rem;
        margin-bottom: 1rem;
        background: linear-gradient(to right, #818cf8, #6366f1);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }

      .welcome-screen p {
        color: var(--text-secondary);
        font-size: 1.1rem;
        line-height: 1.6;
      }

      /* Markdown Display */
      .markdown-body {
        width: 100%;
        max-width: 800px;
        color: var(--text-primary);
        line-height: 1.7;
        display: none;
      }

      .markdown-body h1,
      .markdown-body h2,
      .markdown-body h3 {
        margin-top: 2rem;
        margin-bottom: 1rem;
        color: white;
      }

      .markdown-body p {
        margin-bottom: 1rem;
      }
      .markdown-body img {
        max-width: 100%;
        border-radius: 0.8rem;
        margin: 2rem 0;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
      }

      .markdown-body pre {
        background: #1e1e1e;
        padding: 1.5rem;
        border-radius: 0.8rem;
        overflow-x: auto;
        margin-bottom: 1.5rem;
        border: 1px solid var(--border);
      }

      /* Pipeline Progression (Console) */
      #pipeline-status {
        width: 100%;
        max-width: 800px;
        background: #000;
        color: #10b981;
        font-family: "Courier New", Courier, monospace;
        padding: 1.5rem;
        border-radius: 0.8rem;
        margin-bottom: 2rem;
        font-size: 0.9rem;
        display: none;
        border: 1px solid #10b98133;
        max-height: 400px;
        overflow-y: auto;
        box-shadow: 0 0 20px rgba(16, 185, 129, 0.1);
      }

      .status-line {
        margin-bottom: 0.5rem;
        animation: fadeIn 0.3s ease-out;
      }

      @keyframes fadeIn {
        from {
          opacity: 0;
          transform: translateY(5px);
        }
        to {
          opacity: 1;
          transform: translateY(0);
        }
      }

      /* Input Area at the bottom */
      .input-container {
        padding: 2rem;
        width: 100%;
        display: flex;
        justify-content: center;
      }

      .input-wrapper {
        max-width: 800px;
        width: 100%;
        position: relative;
        background: var(--glass-bg);
        border: 1px solid var(--border);
        border-radius: 1rem;
        padding: 0.5rem;
        display: flex;
        align-items: center;
        transition: all 0.3s;
      }

      .input-wrapper:focus-within {
        border-color: var(--accent);
        background: rgba(255, 255, 255, 0.05);
        box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
      }

      input {
        flex: 1;
        background: transparent;
        border: none;
        color: white;
        padding: 0.75rem 1rem;
        outline: none;
        font-size: 1rem;
      }

      .send-btn {
        background: var(--accent);
        color: white;
        border: none;
        width: 40px;
        height: 40px;
        border-radius: 0.5rem;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.2s;
      }

      .send-btn:hover {
        background: var(--accent-hover);
      }

      .send-btn:disabled {
        background: var(--text-secondary);
        cursor: not-allowed;
        opacity: 0.5;
      }

      /* Loading Spinner */
      .spinner {
        width: 20px;
        height: 20px;
        border: 2px solid rgba(255, 255, 255, 0.3);
        border-radius: 50%;
        border-top-color: white;
        animation: spin 0.8s linear infinite;
      }

      @keyframes spin {
        to {
          transform: rotate(360deg);
        }
      }
    </style>
  </head>
  <body>
    <aside>
      <div class="logo">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        >
          <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
          <polyline points="7 10 12 15 17 10" />
          <line x1="12" y1="15" x2="12" y2="3" />
        </svg>
        Bloggig
      </div>
      <button class="new-chat-btn" onclick="startNewBlog()">
        <svg
          width="20"
          height="20"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        >
          <line x1="12" y1="5" x2="12" y2="19" />
          <line x1="5" y1="12" x2="19" y2="12" />
        </svg>
        New Blog
      </button>
      <div class="history-list" id="history-container">
        <!-- Blogs will be loaded here -->
      </div>
    </aside>

    <main>
      <header>
        <div id="blog-title-display" style="font-weight: 600">
          Blog Overview
        </div>
        <div class="blog-actions">
          <button
            class="btn-icon"
            id="download-btn"
            onclick="downloadCurrentBlog()"
            disabled
          >
            <svg
              width="18"
              height="18"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
            >
              <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
              <polyline points="7 10 12 15 17 10" />
              <line x1="12" y1="15" x2="12" y2="3" />
            </svg>
            Download
          </button>
          <button
            class="btn-icon btn-delete"
            id="delete-btn"
            onclick="deleteCurrentBlog()"
            disabled
          >
            <svg
              width="18"
              height="18"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
            >
              <polyline points="3 6 5 6 21 6" />
              <path
                d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"
              />
              <line x1="10" y1="11" x2="10" y2="17" />
              <line x1="14" y1="11" x2="14" y2="17" />
            </svg>
            Delete
          </button>
        </div>
      </header>

      <div id="content-area">
        <div class="welcome-screen" id="welcome-screen">
          <h1>Craft something amazing.</h1>
          <p>
            Welcome to Bloggig. Enter a topic below to start generating a
            high-quality, research-backed blog post with AI-generated visuals.
          </p>
        </div>

        <div id="pipeline-status"></div>
        <div class="markdown-body" id="blog-content"></div>
      </div>

      <div class="input-container">
        <div class="input-wrapper">
          <input
            type="text"
            id="topic-input"
            placeholder="Enter blog topic..."
            onkeypress="if (event.key === 'Enter') startGeneration();"
          />
          <button
            class="send-btn"
            id="generate-btn"
            onclick="startGeneration()"
          >
            <svg
              id="send-icon"
              width="20"
              height="20"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
            >
              <line x1="22" y1="2" x2="11" y2="13" />
              <polyline points="22 2 15 22 11 13 2 9 22 2" />
            </svg>
          </button>
        </div>
      </div>
    </main>

    <script>
      let currentBlogData = null;
      let isGenerating = false;

      // Initialize marked and highlight.js
      marked.setOptions({
        highlight: function (code, lang) {
          if (lang && hljs.getLanguage(lang)) {
            return hljs.highlight(code, { language: lang }).value;
          }
          return hljs.highlightAuto(code).value;
        },
        breaks: true,
      });

      async function loadHistory() {
        const res = await fetch("/blogs");
        const blogs = await res.json();
        const container = document.getElementById("history-container");
        container.innerHTML = "";

        blogs.forEach((title) => {
          const div = document.createElement("div");
          div.className = "history-item";
          div.textContent = title;
          div.onclick = () => loadBlog(title);
          container.appendChild(div);
        });
      }

      async function loadBlog(title) {
        if (isGenerating) return;

        const res = await fetch(`/blog/${encodeURIComponent(title)}`);
        if (!res.ok) return;

        const data = await res.json();
        currentBlogData = {
          topic: title,
          plan: { blog_title: title },
          final: data.content,
        };

        displayBlog(data.content);
        document.getElementById("blog-title-display").textContent = title;

        // Mark as active in sidebar
        document.querySelectorAll(".history-item").forEach((item) => {
          item.classList.toggle("active", item.textContent === title);
        });

        // Enable actions
        document.getElementById("download-btn").disabled = false;
        document.getElementById("delete-btn").disabled = false;
      }

      function displayBlog(markdown) {
        const blogContent = document.getElementById("blog-content");
        const welcome = document.getElementById("welcome-screen");
        const pipeline = document.getElementById("pipeline-status");

        welcome.style.display = "none";
        pipeline.style.display = "none";
        blogContent.style.display = "block";

        // Rewrite image paths to use the /images static mount if they are internal
        const processedMd = markdown.replace(/\(\.\.\/images\//g, "(/images/");

        blogContent.innerHTML = marked.parse(processedMd);
        hljs.highlightAll();
      }

      function startNewBlog() {
        if (isGenerating) return;

        currentBlogData = null;
        document.getElementById("welcome-screen").style.display = "flex";
        document.getElementById("blog-content").style.display = "none";
        document.getElementById("pipeline-status").style.display = "none";
        document.getElementById("blog-title-display").textContent =
          "Blog Overview";
        document.getElementById("topic-input").value = "";
        document.getElementById("download-btn").disabled = true;
        document.getElementById("delete-btn").disabled = true;

        document.querySelectorAll(".history-item").forEach((item) => {
          item.classList.remove("active");
        });
      }

      function appendStatus(msg) {
        const pipeline = document.getElementById("pipeline-status");
        pipeline.style.display = "block";
        const line = document.createElement("div");
        line.className = "status-line";
        line.innerHTML = `<span style="color: #6ee7b7;">[${new Date().toLocaleTimeString()}]</span> ${msg}`;
        pipeline.appendChild(line);
        pipeline.scrollTop = pipeline.scrollHeight;
      }

      function startGeneration() {
        const topicInput = document.getElementById("topic-input");
        const topic = topicInput.value.trim();
        if (!topic || isGenerating) return;

        isGenerating = true;
        setLoading(true);

        document.getElementById("welcome-screen").style.display = "none";
        document.getElementById("blog-content").style.display = "none";
        const pipeline = document.getElementById("pipeline-status");
        pipeline.innerHTML = "";
        pipeline.style.display = "block";

        appendStatus(`Initializing generation for topic: "${topic}"...`);

        const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
        const ws = new WebSocket(
          `${protocol}//${window.location.host}/ws/generate_blog`,
        );

        ws.onopen = () => {
          ws.send(JSON.stringify({ topic }));
        };

        ws.onmessage = (event) => {
          const data = JSON.parse(event.data);

          if (data.status === "completed") {
            appendStatus("Done! Finalizing blog post...");
          } else if (data.final) {
            // This is the final state
            currentBlogData = data;
            displayBlog(data.final);
            loadHistory(); // Refresh history
            setLoading(false);
            isGenerating = false;
            document.getElementById("download-btn").disabled = false;
            document.getElementById("delete-btn").disabled = false;
            if (data.plan && data.plan.blog_title) {
              document.getElementById("blog-title-display").textContent =
                data.plan.blog_title;
            }
          } else if (data.error) {
            appendStatus(
              `<span style="color: #f87171;">ERROR: ${data.error}</span>`,
            );
            setLoading(false);
            isGenerating = false;
          } else {
            // Try to infer what happened from the state keys
            if (data.sections && data.sections.length > 0) {
              const lastSection = data.sections[data.sections.length - 1];
              appendStatus(
                `Generated section: ${lastSection[1].split("\n")[0].replace("## ", "")}`,
              );
            } else if (data.plan) {
              appendStatus(
                `Plan created: "${data.plan.blog_title}" with ${data.plan.tasks.length} sections.`,
              );
            } else if (data.queries && data.queries.length > 0) {
              appendStatus(`Researching: ${data.queries.join(", ")}`);
            }
          }
        };

        ws.onerror = (err) => {
          appendStatus(
            `<span style="color: #f87171;">Connection error.</span>`,
          );
          setLoading(false);
          isGenerating = false;
        };

        ws.onclose = () => {
          if (isGenerating) {
            appendStatus("Connection closed.");
            setLoading(false);
            isGenerating = false;
          }
        };
      }

      function setLoading(loading) {
        const btn = document.getElementById("generate-btn");
        const icon = document.getElementById("send-icon");
        if (loading) {
          btn.disabled = true;
          btn.innerHTML = '<div class="spinner"></div>';
        } else {
          btn.disabled = false;
          btn.innerHTML = `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polyline points="22 2 15 22 11 13 2 9 22 2"/></svg>`;
        }
      }

      async function deleteCurrentBlog() {
        if (
          !currentBlogData ||
          !confirm(
            "Are you sure you want to delete this blog and all related images?",
          )
        )
          return;

        const res = await fetch("/delete_blog", {
          method: "DELETE",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ data: currentBlogData }),
        });

        if (res.ok) {
          startNewBlog();
          loadHistory();
        } else {
          alert("Failed to delete blog.");
        }
      }

      function downloadCurrentBlog() {
        if (!currentBlogData) return;
        const title = currentBlogData.plan
          ? currentBlogData.plan.blog_title
          : currentBlogData.topic;
        window.location.href = `/download_blog/${encodeURIComponent(title)}`;
      }

      // Initial history load
      loadHistory();
    </script>
  </body>
</html>