~kris/9p

krisyotam.net

ref: f269e2c7d5c244d42779b420cd5e3683ef2d58ca krisyotam.net/reference/tui.tsx -rw-r--r-- 35.8 KiB
f269e2c7 — Kris Yotam merge: bring emergency backup commit from plan9.krisyotam.com a month ago
                                                                                
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
/*
+------------------+----------------------------------------------------------+
| FILE             | tui.tsx                                                  |
| ROLE             | Plan 9 / Acme-inspired TUI content browser               |
| OWNER            | Kris Yotam                                               |
| CREATED          | 2026-02-28                                               |
| UPDATED          | 2026-02-28                                               |
+------------------+----------------------------------------------------------+
| @type component                                                             |
| @path src/components/core/tui.tsx                                           |
+------------------+----------------------------------------------------------+
| SUMMARY                                                                     |
| Full Plan 9 desktop layout: teal desktop background with island sections.  |
| 3 top bars, central Acme content browser, footer taskbar — all floating    |
| with gaps between them. Draggable Cirno mascot with minimize.              |
| Left pane: collapsible directory tree. Right pane: raw MDX viewer.          |
| Keyboard navigation (arrows, Enter, Escape) and search.                    |
+-----------------------------------------------------------------------------+
*/

"use client"

import { useState, useCallback, useEffect, useRef, useMemo } from "react"
import { ScrollArea } from "@/components/ui/scroll-area"

// Types

interface TreeEntry {
  slug: string
  title: string
  date: string
  preview: string
}

interface TUIProps {
  tree: Record<string, TreeEntry[]>
}

interface SelectedFile {
  type: string
  slug: string
}

interface FileContent {
  frontmatter: string
  body: string
}

// Plan 9 Color Palette

const P9 = {
  desktop: "#B5B5AD",       // grey desktop background
  tagBar: "#EAFFFF",        // pale cyan — Acme tag/command bar
  titleBar: "#FFFFEA",      // pale yellow — Acme window title bars
  body: "#FFFFF5",          // cream — Acme body/content background
  text: "#000000",
  textMuted: "#555555",
  selected: "#9EE09E",
  border: "#9EEEEE",        // Plan 9 teal blue window borders
  borderInner: "#888888",   // inner dividers (pane separators)
  borderLight: "#9EEEEE",
  hoverBg: "#EEEEE5",
  barAccent1: "#98D1CB",
  barAccent2: "#E8A0A0",
  barAccent3: "#88CC88",
  footerBg: "#9EDBDA",
  wsGrid: "#EAFFFF",
  wsHighlight: "#55CCCC",
} as const

const GAP = "14px"

// SVG Icons (Plan 9 pixel-art style, no emoji)

function FolderIcon({ size = 12 }: { size?: number }) {
  return (
    <svg width={size} height={size} viewBox="0 0 12 10" style={{ flexShrink: 0 }}>
      <rect x="0" y="2" width="12" height="8" fill="none" stroke="#000" strokeWidth="1" />
      <rect x="0" y="2" width="5" height="2" fill="#000" />
    </svg>
  )
}

function HomeIcon({ size = 12 }: { size?: number }) {
  return (
    <svg width={size} height={size} viewBox="0 0 12 12" style={{ flexShrink: 0 }}>
      <polygon points="6,1 11,6 9,6 9,11 3,11 3,6 1,6" fill="none" stroke="#000" strokeWidth="1" />
    </svg>
  )
}

// Floating Cirno

function Cirno({ minimized, setMinimized }: { minimized: boolean; setMinimized: (v: boolean) => void }) {
  const [pos, setPos] = useState(() => {
    if (typeof window !== "undefined") {
      return {
        x: Math.round((window.innerWidth - 560) / 2),
        y: Math.round((window.innerHeight - 600) / 2),
      }
    }
    return { x: 200, y: 100 }
  })
  const [dragging, setDragging] = useState(false)
  const dragOffset = useRef({ x: 0, y: 0 })

  const onMouseDown = useCallback((e: React.MouseEvent) => {
    const target = e.target as HTMLElement
    if (target.closest("[data-cirno-close]")) return
    e.preventDefault()
    setDragging(true)
    dragOffset.current = {
      x: e.clientX - pos.x,
      y: e.clientY - pos.y,
    }
  }, [pos])

  useEffect(() => {
    if (!dragging) return

    function onMove(e: MouseEvent) {
      setPos({
        x: e.clientX - dragOffset.current.x,
        y: e.clientY - dragOffset.current.y,
      })
    }

    function onUp() {
      setDragging(false)
    }

    window.addEventListener("mousemove", onMove)
    window.addEventListener("mouseup", onUp)
    return () => {
      window.removeEventListener("mousemove", onMove)
      window.removeEventListener("mouseup", onUp)
    }
  }, [dragging])

  if (minimized) {
    return null
  }

  return (
    <div
      style={{
        position: "fixed",
        left: pos.x,
        top: pos.y,
        zIndex: 999,
        border: `2px solid ${P9.border}`,
        background: P9.body,
        boxShadow: "2px 2px 0 rgba(0,0,0,0.15)",
        cursor: dragging ? "grabbing" : "default",
        userSelect: "none",
        maxWidth: "560px",
      }}
    >
      <div
        onMouseDown={onMouseDown}
        style={{
          background: P9.tagBar,
          borderBottom: `1px solid ${P9.borderInner}`,
          padding: "1px 4px",
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
          cursor: dragging ? "grabbing" : "grab",
          fontFamily: "var(--font-mono), monospace",
          fontSize: "11px",
        }}
      >
        <span>/dev/cirno</span>
        <button
          data-cirno-close
          onClick={() => setMinimized(true)}
          title="Minimize"
          style={{
            background: P9.titleBar,
            border: `2px solid ${P9.borderInner}`,
            width: "24px",
            height: "18px",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            cursor: "pointer",
            fontFamily: "inherit",
            fontSize: "13px",
            fontWeight: "bold",
            lineHeight: 1,
            padding: 0,
            color: P9.text,
          }}
        >
          &#x2212;
        </button>
      </div>
      <img
        src="/plan9-cirno.jpeg"
        alt="Cirno"
        draggable={false}
        style={{
          display: "block",
          width: "100%",
          height: "auto",
          imageRendering: "auto",
        }}
      />
    </div>
  )
}

// Top Bars (3 separate island bars)

function TopBars({ totalFiles }: { totalFiles: number }) {
  const now = new Date()
  const timeStr = now.toLocaleTimeString("en-US", { hour12: false, hour: "2-digit", minute: "2-digit" })

  return (
    <div
      style={{
        display: "flex",
        gap: GAP,
        flexShrink: 0,
      }}
    >
      {/* Bar 1: Status bar */}
      <div
        style={{
          flex: "1 1 33%",
          border: `2px solid ${P9.border}`,
          overflow: "hidden",
          background: P9.body,
        }}
      >
        <div
          style={{
            background: P9.titleBar,
            padding: "1px 8px",
            display: "flex",
            justifyContent: "space-between",
            alignItems: "center",
            fontFamily: "var(--font-mono), monospace",
            fontSize: "12px",
            borderBottom: `1px solid ${P9.borderInner}`,
            minHeight: "18px",
          }}
        >
          <span>krisyotam(9)</span>
          <span>front</span>
        </div>
        {/* Plan 9 stats graph — 4 rows spelling KRIS */}
        <div style={{ display: "flex", flexDirection: "column", borderTop: `1px solid ${P9.borderInner}` }}>
          {[
            { letter: "K", color: "#CC4444", bar1: "55%", bar2: "30%" },
            { letter: "R", color: "#4444BB", bar1: "70%", bar2: "45%" },
            { letter: "I", color: "#228822", bar1: "35%", bar2: "80%" },
            { letter: "S", color: "#77BB55", bar1: "60%", bar2: "20%" },
          ].map((row, i) => (
            <div
              key={row.letter}
              style={{
                display: "flex",
                alignItems: "stretch",
                height: "14px",
                borderBottom: `1px solid ${P9.borderInner}`,
              }}
            >
              {/* Small square with letter */}
              <div
                style={{
                  width: "16px",
                  flexShrink: 0,
                  background: row.color,
                  display: "flex",
                  alignItems: "center",
                  justifyContent: "center",
                  color: "#fff",
                  fontSize: "9px",
                  fontWeight: "bold",
                  fontFamily: "var(--font-mono), monospace",
                  lineHeight: 1,
                  borderRight: `1px solid ${P9.borderInner}`,
                }}
              >
                {row.letter}
              </div>
              {/* Bar 1 */}
              <div style={{ flex: "1 1 50%", position: "relative", background: P9.body }}>
                <div style={{ position: "absolute", left: 0, top: 0, bottom: 0, width: row.bar1, background: row.color, opacity: 0.7 }} />
              </div>
              {/* Vertical divider */}
              <div style={{ width: "1px", background: P9.borderInner, flexShrink: 0 }} />
              {/* Bar 2 */}
              <div style={{ flex: "1 1 50%", position: "relative", background: P9.body }}>
                <div style={{ position: "absolute", left: 0, top: 0, bottom: 0, width: row.bar2, background: row.color, opacity: 0.7 }} />
              </div>
            </div>
          ))}
        </div>
      </div>

      {/* Bar 2: File browser (Plan 9 file manager style) */}
      <div
        style={{
          flex: "1 1 34%",
          border: `2px solid ${P9.border}`,
          overflow: "hidden",
          background: P9.body,
        }}
      >
        {/* Title bar: home, folder, up arrow | /usr/krisyotam | new folder, new file */}
        <div
          style={{
            background: P9.titleBar,
            padding: "1px 8px",
            display: "flex",
            alignItems: "center",
            justifyContent: "space-between",
            fontFamily: "var(--font-mono), monospace",
            fontSize: "12px",
            borderBottom: `1px solid ${P9.borderInner}`,
            minHeight: "18px",
          }}
        >
          <div style={{ display: "flex", alignItems: "center", gap: "5px" }}>
            <HomeIcon size={12} />
            <FolderIcon size={11} />
            <svg width="10" height="10" viewBox="0 0 10 10"><polygon points="5,1 9,9 1,9" fill={P9.text} /></svg>
            <span style={{ fontWeight: "bold", marginLeft: "4px" }}>/usr/krisyotam</span>
          </div>
          <div style={{ display: "flex", alignItems: "center", gap: "5px" }}>
            <svg width="12" height="10" viewBox="0 0 12 10" aria-label="New folder">
              <rect x="0" y="2" width="10" height="8" fill="none" stroke={P9.text} strokeWidth="1" />
              <rect x="0" y="2" width="5" height="2" fill={P9.text} />
              <line x1="9" y1="0" x2="9" y2="6" stroke={P9.text} strokeWidth="1.2" />
              <line x1="6.5" y1="3" x2="11.5" y2="3" stroke={P9.text} strokeWidth="1.2" />
            </svg>
            <svg width="12" height="10" viewBox="0 0 12 10" aria-label="New file">
              <rect x="0" y="0" width="8" height="10" fill="none" stroke={P9.text} strokeWidth="1" />
              <line x1="9" y1="2" x2="9" y2="8" stroke={P9.text} strokeWidth="1.2" />
              <line x1="6.5" y1="5" x2="11.5" y2="5" stroke={P9.text} strokeWidth="1.2" />
            </svg>
          </div>
        </div>
        {/* Directory listing */}
        <div
          style={{
            fontFamily: "var(--font-mono), monospace",
            fontSize: "11px",
          }}
        >
          <div style={{ padding: "1px 8px", display: "flex", alignItems: "center", gap: "6px" }}>
            <FolderIcon size={10} />
            <span>Blog</span>
            <span style={{ marginLeft: "auto", color: P9.textMuted }}>0</span>
            <span style={{ color: P9.textMuted }}>Feb 28 10:15</span>
          </div>
          <div style={{ padding: "1px 8px", display: "flex", alignItems: "center", gap: "6px" }}>
            <FolderIcon size={10} />
            <span>Books</span>
            <span style={{ marginLeft: "auto", color: P9.textMuted }}>0</span>
            <span style={{ color: P9.textMuted }}>Mar 14 23:30</span>
          </div>
          <div style={{ padding: "1px 8px", display: "flex", alignItems: "center", gap: "6px" }}>
            <FolderIcon size={10} />
            <span>Documents</span>
            <span style={{ marginLeft: "auto", color: P9.textMuted }}>0</span>
            <span style={{ color: P9.textMuted }}>Jun 27 23:43</span>
          </div>
        </div>
      </div>

      {/* Bar 3: Navigation grid — command menu items */}
      <div
        style={{
          flex: "1 1 33%",
          border: `2px solid ${P9.border}`,
          overflow: "hidden",
          background: P9.body,
          display: "flex",
          flexDirection: "column",
        }}
      >
        <div
          style={{
            background: P9.titleBar,
            padding: "1px 8px",
            display: "flex",
            justifyContent: "space-between",
            alignItems: "center",
            fontFamily: "var(--font-mono), monospace",
            fontSize: "12px",
            borderBottom: `1px solid ${P9.borderInner}`,
            minHeight: "18px",
          }}
        >
          <span>navigate(1)</span>
          <span style={{ color: P9.textMuted }}>site</span>
        </div>
        <div
          style={{
            fontFamily: "var(--font-mono), monospace",
            fontSize: "10px",
            display: "grid",
            gridTemplateColumns: "repeat(5, 1fr)",
            flex: 1,
          }}
        >
          {[
            { label: "home", path: "/" },
            { label: "essays", path: "/essays" },
            { label: "blog", path: "/blog" },
            { label: "diary", path: "/diary" },
            { label: "reviews", path: "/reviews" },
            { label: "fiction", path: "/fiction" },
            { label: "verse", path: "/verse" },
            { label: "til", path: "/til" },
            { label: "now", path: "/now" },
            { label: "film", path: "/film" },
            { label: "anime", path: "/anime" },
            { label: "manga", path: "/manga" },
            { label: "reading", path: "/reading" },
            { label: "library", path: "/library" },
            { label: "tags", path: "/tags" },
            { label: "about", path: "/me" },
            { label: "stats", path: "/stats" },
            { label: "globe", path: "/globe" },
            { label: "contact", path: "/contact" },
          ].map((item) => (
            <a
              key={item.path}
              href={item.path}
              target="_blank"
              rel="noopener noreferrer"
              onMouseEnter={(e) => {
                e.currentTarget.style.background = P9.wsHighlight
                e.currentTarget.style.color = "#fff"
              }}
              onMouseLeave={(e) => {
                e.currentTarget.style.background = "#D8D8D0"
                e.currentTarget.style.color = P9.text
              }}
              style={{
                padding: "1px 3px",
                textAlign: "center",
                background: "#D8D8D0",
                color: P9.text,
                border: "1px solid #FFFFFF",
                whiteSpace: "nowrap",
                overflow: "hidden",
                textOverflow: "ellipsis",
                cursor: "pointer",
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
                fontFamily: "inherit",
                fontSize: "inherit",
              }}
            >
              {item.label}
            </a>
          ))}
        </div>
      </div>
    </div>
  )
}

// Footer Taskbar

function Footer({ cirnoMinimized, onRestoreCirno }: { cirnoMinimized: boolean; onRestoreCirno: () => void }) {
  const [time, setTime] = useState("")

  useEffect(() => {
    function tick() {
      const d = new Date()
      const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
      const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
      setTime(`${days[d.getDay()]} ${months[d.getMonth()]} ${d.getDate()} ${d.toLocaleTimeString("en-US", { hour12: false, hour: "2-digit", minute: "2-digit" })}`)
    }
    tick()
    const id = setInterval(tick, 10000)
    return () => clearInterval(id)
  }, [])

  return (
    <div
      style={{
        flexShrink: 0,
        border: `2px solid ${P9.border}`,
        background: P9.body,
        display: "flex",
        alignItems: "stretch",
        height: "32px",
        fontFamily: "var(--font-mono), monospace",
        fontSize: "12px",
      }}
    >
      {/* Home button */}
      <a
        href="/"
        style={{
          display: "flex",
          alignItems: "center",
          padding: "0 10px",
          borderRight: `1px solid ${P9.borderInner}`,
          color: P9.text,
          textDecoration: "none",
          fontWeight: "bold",
        }}
      >
        /home
      </a>
      {/* Collapsed cirno.jpg — Plan 9 collapsed window style */}
      {cirnoMinimized && (
        <div
          onClick={onRestoreCirno}
          style={{
            display: "flex",
            alignItems: "stretch",
            borderRight: `1px solid ${P9.borderInner}`,
            cursor: "pointer",
          }}
        >
          <div
            style={{
              background: P9.tagBar,
              padding: "0 6px",
              display: "flex",
              alignItems: "center",
              borderRight: `1px solid ${P9.borderInner}`,
              fontSize: "11px",
              color: P9.textMuted,
            }}
          >
            &#x25A0;
          </div>
          <div
            style={{
              background: P9.titleBar,
              padding: "0 8px",
              display: "flex",
              alignItems: "center",
              fontSize: "11px",
            }}
          >
            cirno.jpg
          </div>
        </div>
      )}
      {/* Colored center panel */}
      <div
        style={{
          flex: "0 0 14%",
          borderRight: `1px solid ${P9.borderInner}`,
          background: P9.footerBg,
        }}
      />
      {/* Clock + info */}
      <div
        style={{
          flex: 1,
          display: "flex",
          alignItems: "center",
          justifyContent: "flex-end",
          gap: "16px",
          padding: "0 12px",
          color: P9.text,
        }}
      >
        <span>{time}</span>
        <span style={{ color: P9.textMuted }}>krisyotam.com</span>
        <span style={{ color: P9.textMuted }}>9front</span>
      </div>
    </div>
  )
}

// Main TUI Component

export function TUI({ tree }: TUIProps) {
  const [expandedDirs, setExpandedDirs] = useState<Set<string>>(new Set())
  const [selectedFile, setSelectedFile] = useState<SelectedFile | null>(null)
  const [fileContent, setFileContent] = useState<FileContent | null>(null)
  const [searchQuery, setSearchQuery] = useState("")
  const [loading, setLoading] = useState(false)
  const [focusIndex, setFocusIndex] = useState(-1)
  const [cirnoMinimized, setCirnoMinimized] = useState(false)

  const treeRef = useRef<HTMLDivElement>(null)

  const contentTypes = useMemo(() => Object.keys(tree).sort(), [tree])
  const totalFiles = useMemo(
    () => Object.values(tree).reduce((sum, entries) => sum + entries.length, 0),
    [tree]
  )

  const navItems = useMemo(() => {
    const items: { kind: "dir" | "file"; type: string; slug?: string }[] = []
    const query = searchQuery.toLowerCase()

    for (const type of contentTypes) {
      const entries = tree[type]
      const filtered = query
        ? entries.filter(
            (e) =>
              e.slug.toLowerCase().includes(query) ||
              e.title.toLowerCase().includes(query)
          )
        : entries

      if (query && filtered.length === 0) continue
      items.push({ kind: "dir", type })

      if (expandedDirs.has(type)) {
        for (const entry of filtered) {
          items.push({ kind: "file", type, slug: entry.slug })
        }
      }
    }
    return items
  }, [contentTypes, tree, expandedDirs, searchQuery])

  const filteredTree = useMemo(() => {
    if (!searchQuery) return tree
    const query = searchQuery.toLowerCase()
    const result: Record<string, TreeEntry[]> = {}
    for (const type of contentTypes) {
      const filtered = tree[type].filter(
        (e) =>
          e.slug.toLowerCase().includes(query) ||
          e.title.toLowerCase().includes(query)
      )
      if (filtered.length > 0) result[type] = filtered
    }
    return result
  }, [tree, searchQuery, contentTypes])

  const catFile = useCallback(async (type: string, slug: string) => {
    setSelectedFile({ type, slug })
    setLoading(true)
    setFileContent(null)

    try {
      const res = await fetch(`/api/tui/${type}/${slug}`)
      if (!res.ok) throw new Error(`HTTP ${res.status}`)
      const data = await res.json()
      setFileContent({ frontmatter: data.frontmatter, body: data.body })
    } catch {
      setFileContent({
        frontmatter: "",
        body: `Error: failed to read ${type}/${slug}.mdx`,
      })
    } finally {
      setLoading(false)
    }
  }, [])

  const toggleDir = useCallback((type: string) => {
    setExpandedDirs((prev) => {
      const next = new Set(prev)
      if (next.has(type)) next.delete(type)
      else next.add(type)
      return next
    })
  }, [])

  useEffect(() => {
    function handleKeyDown(e: KeyboardEvent) {
      if ((e.target as HTMLElement).tagName === "INPUT") return

      switch (e.key) {
        case "ArrowDown":
          e.preventDefault()
          setFocusIndex((prev) => Math.min(prev + 1, navItems.length - 1))
          break
        case "ArrowUp":
          e.preventDefault()
          setFocusIndex((prev) => Math.max(prev - 1, 0))
          break
        case "ArrowRight": {
          e.preventDefault()
          const item = navItems[focusIndex]
          if (item?.kind === "dir" && !expandedDirs.has(item.type)) {
            toggleDir(item.type)
          }
          break
        }
        case "ArrowLeft": {
          e.preventDefault()
          const item = navItems[focusIndex]
          if (item?.kind === "dir" && expandedDirs.has(item.type)) {
            toggleDir(item.type)
          } else if (item?.kind === "file") {
            const dirIdx = navItems.findIndex(
              (n) => n.kind === "dir" && n.type === item.type
            )
            if (dirIdx >= 0) setFocusIndex(dirIdx)
          }
          break
        }
        case "Enter": {
          e.preventDefault()
          const item = navItems[focusIndex]
          if (!item) break
          if (item.kind === "dir") toggleDir(item.type)
          else if (item.slug) catFile(item.type, item.slug)
          break
        }
        case "Escape":
          e.preventDefault()
          setSelectedFile(null)
          setFileContent(null)
          break
      }
    }

    window.addEventListener("keydown", handleKeyDown)
    return () => window.removeEventListener("keydown", handleKeyDown)
  }, [navItems, focusIndex, expandedDirs, toggleDir, catFile])

  useEffect(() => {
    if (focusIndex < 0 || !treeRef.current) return
    const el = treeRef.current.querySelector(`[data-nav-index="${focusIndex}"]`)
    el?.scrollIntoView({ block: "nearest" })
  }, [focusIndex])

  const breadcrumb = selectedFile
    ? `/content/${selectedFile.type}/${selectedFile.slug}`
    : "/content"

  // Render

  return (
    <div
      style={{
        fontFamily: "var(--font-mono), 'IBM Plex Mono', monospace",
        fontSize: "13px",
        lineHeight: "1.4",
        color: P9.text,
        background: P9.desktop,
        height: "100vh",
        display: "flex",
        flexDirection: "column",
        padding: GAP,
        gap: GAP,
        overflow: "hidden",
      }}
    >
      {/* Floating Cirno */}
      <Cirno minimized={cirnoMinimized} setMinimized={setCirnoMinimized} />

      {/* ================================================================== */}
      {/* Top 3 Bars (separate islands) */}
      {/* ================================================================== */}
      <TopBars totalFiles={totalFiles} />

      {/* ================================================================== */}
      {/* Central Island: Acme workspace */}
      {/* ================================================================== */}
      <div
        style={{
          flex: 1,
          border: `2px solid ${P9.border}`,
          background: P9.body,
          display: "flex",
          flexDirection: "column",
          overflow: "hidden",
        }}
      >
        {/* Acme command/tag bar */}
        <div
          style={{
            background: P9.tagBar,
            borderBottom: `1px solid ${P9.borderInner}`,
            padding: "3px 8px",
            display: "flex",
            alignItems: "center",
            gap: "10px",
            flexShrink: 0,
            minHeight: "24px",
            fontSize: "12px",
          }}
        >
          <span style={{ fontWeight: "bold" }}>New</span>
          <span style={{ fontWeight: "bold" }}>Cut</span>
          <span style={{ fontWeight: "bold" }}>Paste</span>
          <span style={{ fontWeight: "bold" }}>Snarf</span>
          <span style={{ fontWeight: "bold" }}>Sort</span>
          <span style={{ fontWeight: "bold" }}>Zerox</span>
          <span style={{ fontWeight: "bold" }}>Delcol</span>
          <span style={{ color: P9.textMuted, margin: "0 4px" }}>|</span>
          <input
            type="text"
            placeholder="Look"
            value={searchQuery}
            onChange={(e) => {
              setSearchQuery(e.target.value)
              setFocusIndex(-1)
            }}
            style={{
              background: P9.body,
              border: `1px solid ${P9.borderInner}`,
              padding: "1px 6px",
              fontFamily: "inherit",
              fontSize: "inherit",
              width: "180px",
              outline: "none",
            }}
          />
        </div>

        {/* Panes */}
        <div data-tui-panes style={{ display: "flex", flex: 1, overflow: "hidden" }}>
          {/* Left Pane — Directory Tree */}
          <div
            data-tui-tree
            style={{
              width: "30%",
              minWidth: "200px",
              maxWidth: "360px",
              borderRight: `1px solid ${P9.borderInner}`,
              display: "flex",
              flexDirection: "column",
              overflow: "hidden",
            }}
          >
            <div
              style={{
                background: P9.tagBar,
                borderBottom: `1px solid ${P9.borderInner}`,
                padding: "1px 8px",
                display: "flex",
                alignItems: "center",
                justifyContent: "space-between",
                flexShrink: 0,
                minHeight: "20px",
                fontSize: "12px",
              }}
            >
              <span style={{ fontWeight: "bold" }}>{breadcrumb}</span>
              <span style={{ color: P9.textMuted }}>{totalFiles}</span>
            </div>

            <div
              style={{
                background: P9.titleBar,
                borderBottom: `1px solid ${P9.borderInner}`,
                padding: "1px 8px",
                fontWeight: "bold",
                flexShrink: 0,
                minHeight: "18px",
                display: "flex",
                alignItems: "center",
                fontSize: "12px",
              }}
            >
              Del Snarf | Look Put Mail
            </div>

            <ScrollArea style={{ flex: 1 }}>
              <div ref={treeRef} style={{ padding: "2px 0" }}>
                {contentTypes.map((type) => {
                  const entries = filteredTree[type]
                  if (!entries && searchQuery) return null

                  const isExpanded = expandedDirs.has(type)
                  const fileCount = tree[type].length
                  const dirNavIdx = navItems.findIndex(
                    (n) => n.kind === "dir" && n.type === type
                  )

                  return (
                    <div key={type}>
                      <div
                        data-nav-index={dirNavIdx}
                        onClick={() => toggleDir(type)}
                        style={{
                          padding: "1px 8px",
                          cursor: "pointer",
                          display: "flex",
                          alignItems: "center",
                          gap: "4px",
                          background:
                            focusIndex === dirNavIdx ? P9.selected : "transparent",
                          userSelect: "none",
                        }}
                        onMouseEnter={(e) => {
                          if (focusIndex !== dirNavIdx)
                            e.currentTarget.style.background = P9.hoverBg
                        }}
                        onMouseLeave={(e) => {
                          if (focusIndex !== dirNavIdx)
                            e.currentTarget.style.background = "transparent"
                        }}
                      >
                        <span style={{ width: "12px", display: "inline-block" }}>
                          {isExpanded ? "\u25BE" : "\u25B8"}
                        </span>
                        <span style={{ fontWeight: "bold" }}>{type}/</span>
                        <span
                          style={{
                            color: P9.textMuted,
                            fontSize: "11px",
                            marginLeft: "auto",
                          }}
                        >
                          {fileCount}
                        </span>
                      </div>

                      {isExpanded &&
                        (entries || tree[type]).map((entry) => {
                          const fileNavIdx = navItems.findIndex(
                            (n) =>
                              n.kind === "file" &&
                              n.type === type &&
                              n.slug === entry.slug
                          )
                          const isSelected =
                            selectedFile?.type === type &&
                            selectedFile?.slug === entry.slug

                          return (
                            <div
                              key={entry.slug}
                              data-nav-index={fileNavIdx}
                              onClick={() => catFile(type, entry.slug)}
                              title={`${entry.title}\n${entry.date}\n${entry.preview}`}
                              style={{
                                padding: "1px 8px 1px 28px",
                                cursor: "pointer",
                                whiteSpace: "nowrap",
                                overflow: "hidden",
                                textOverflow: "ellipsis",
                                background: isSelected
                                  ? P9.selected
                                  : focusIndex === fileNavIdx
                                  ? P9.selected
                                  : "transparent",
                                userSelect: "none",
                              }}
                              onMouseEnter={(e) => {
                                if (!isSelected && focusIndex !== fileNavIdx)
                                  e.currentTarget.style.background = P9.hoverBg
                              }}
                              onMouseLeave={(e) => {
                                if (!isSelected && focusIndex !== fileNavIdx)
                                  e.currentTarget.style.background = "transparent"
                              }}
                            >
                              {entry.slug}.mdx
                            </div>
                          )
                        })}
                    </div>
                  )
                })}
              </div>
            </ScrollArea>
          </div>

          {/* Right Pane — Content Viewer */}
          <div
            style={{
              flex: 1,
              display: "flex",
              flexDirection: "column",
              overflow: "hidden",
            }}
          >
            <div
              style={{
                background: P9.tagBar,
                borderBottom: `1px solid ${P9.borderInner}`,
                padding: "1px 8px",
                display: "flex",
                alignItems: "center",
                justifyContent: "space-between",
                flexShrink: 0,
                minHeight: "20px",
                fontSize: "12px",
              }}
            >
              <span style={{ fontWeight: "bold" }}>
                {selectedFile
                  ? `${selectedFile.type}/${selectedFile.slug}.mdx`
                  : "output"}
              </span>
              <span style={{ color: P9.textMuted }}>
                Del Snarf | Look Put
              </span>
            </div>

            <div
              style={{
                background: P9.titleBar,
                borderBottom: `1px solid ${P9.borderInner}`,
                padding: "1px 8px",
                flexShrink: 0,
                minHeight: "18px",
                display: "flex",
                alignItems: "center",
                fontSize: "12px",
                fontWeight: "bold",
              }}
            >
              {selectedFile
                ? `$ cat ${selectedFile.type}/${selectedFile.slug}.mdx`
                : "cat"}
            </div>

            <ScrollArea style={{ flex: 1 }}>
              <div style={{ padding: "8px 12px" }}>
                {!selectedFile && !loading && (
                  <div style={{ color: P9.textMuted }}>
                    <div>; Plan 9 / Acme content browser</div>
                    <div>; click a file to cat it, or use arrow keys + Enter</div>
                    <div>; Escape to clear, Look to search</div>
                    <div>;</div>
                    <div>
                      ; {contentTypes.length} directories, {totalFiles} files
                    </div>
                  </div>
                )}

                {loading && (
                  <div style={{ color: P9.textMuted }}>
                    reading...
                  </div>
                )}

                {selectedFile && fileContent && !loading && (
                  <div>
                    {fileContent.frontmatter && (
                      <>
                        <div>---</div>
                        <div
                          style={{
                            whiteSpace: "pre-wrap",
                            wordBreak: "break-word",
                          }}
                        >
                          {fileContent.frontmatter}
                        </div>
                        <div>---</div>
                        <div style={{ height: "8px" }} />
                      </>
                    )}

                    <div
                      style={{
                        whiteSpace: "pre-wrap",
                        wordBreak: "break-word",
                      }}
                    >
                      {fileContent.body}
                    </div>
                  </div>
                )}
              </div>
            </ScrollArea>
          </div>
        </div>
      </div>

      {/* ================================================================== */}
      {/* Footer Taskbar (separate island) */}
      {/* ================================================================== */}
      <Footer cirnoMinimized={cirnoMinimized} onRestoreCirno={() => setCirnoMinimized(false)} />

      {/* Mobile responsive */}
      <style>{`
        @media (max-width: 640px) {
          [data-tui-panes] {
            flex-direction: column !important;
          }
          [data-tui-tree] {
            width: 100% !important;
            max-width: none !important;
            max-height: 40vh !important;
            border-right: none !important;
            border-bottom: 1px solid ${P9.borderInner};
          }
        }
      `}</style>
    </div>
  )
}