aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/TrackerView.swift
blob: 34f7a57e3431582f86bba320484ad11c293e80be (plain)
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
import SwiftUI
import SwiftData
import Foundation
import UniformTypeIdentifiers

enum TrackerSelection: Hashable {
  case bookmark(Bookmark)
  case bookmarkServer(BookmarkServer)
  
  var server: Server? {
    switch self {
    case .bookmark(let b): return b.server
    case .bookmarkServer(let t): return t.server
    }
  }
}

struct TrackerView: View {
  @Environment(\.colorScheme) private var colorScheme
  @Environment(\.openWindow) private var openWindow
  @Environment(\.controlActiveState) private var controlActiveState
  @Environment(\.modelContext) private var modelContext
  
  @State private var refreshing = false
  @State private var trackerSheetPresented: Bool = false
  @State private var trackerSheetBookmark: Bookmark? = nil
  @State private var serverSheetBookmark: Bookmark? = nil
  @State private var attemptedPrepopulate: Bool = false
  @State private var fileDropActive = false
  @State private var bookmarkExportActive = false
  @State private var bookmarkExport: BookmarkDocument? = nil
  @State private var expandedTrackers: Set<Bookmark> = []
  @State private var trackerServers: [Bookmark: [BookmarkServer]] = [:]
  @State private var loadingTrackers: Set<Bookmark> = []
  @State private var fetchTasks: [Bookmark: Task<Void, Never>] = [:]
  @State private var searchText: String = ""
  @State private var isSearching = false

  @Query(sort: \Bookmark.order) private var bookmarks: [Bookmark]
  @Binding var selection: TrackerSelection?

  private var filteredBookmarks: [Bookmark] {
    guard !self.searchText.isEmpty else {
      return self.bookmarks
    }

    let searchWords = self.searchText.lowercased().split(separator: " ").map(String.init)

    return self.bookmarks.filter { bookmark in
      // Always show tracker bookmarks (filter only their servers)
      if bookmark.type == .tracker {
        return true
      }

      // Filter server bookmarks by search text
      return self.bookmarkMatchesSearch(bookmark, searchWords: searchWords)
    }
  }

  private func bookmarkMatchesSearch(_ bookmark: Bookmark, searchWords: [String]) -> Bool {
    let searchableText = "\(bookmark.name) \(bookmark.address)".lowercased()

    // All search words must match
    return searchWords.allSatisfy { word in
      searchableText.contains(word)
    }
  }

  private func filteredServers(for bookmark: Bookmark) -> [BookmarkServer] {
    let servers = self.trackerServers[bookmark] ?? []
    print("TrackerView.filteredServers: Looking up servers for \(bookmark.name), found \(servers.count) servers")

    guard !self.searchText.isEmpty else {
      return servers
    }

    let searchWords = self.searchText.lowercased().split(separator: " ").map(String.init)

    return servers.filter { server in
      let searchableText = "\(server.name ?? "") \(server.address) \(server.description ?? "")".lowercased()

      // All search words must match
      return searchWords.allSatisfy { word in
        searchableText.contains(word)
      }
    }
  }

  var body: some View {
    List(selection: $selection) {
      ForEach(filteredBookmarks, id: \.self) { bookmark in
        TrackerItemView(
          bookmark: bookmark,
          isExpanded: self.expandedTrackers.contains(bookmark),
          isLoading: self.loadingTrackers.contains(bookmark),
          count: self.trackerServers[bookmark]?.count ?? 0
        ) {
          self.toggleExpanded(for: bookmark)
        }
        .tag(TrackerSelection.bookmark(bookmark))

        if bookmark.type == .tracker && self.expandedTrackers.contains(bookmark) {
          ForEach(self.filteredServers(for: bookmark), id: \.self) { trackedServer in
            TrackerBookmarkServerView(server: trackedServer)
              .moveDisabled(true)
              .deleteDisabled(true)
              .tag(TrackerSelection.bookmarkServer(trackedServer))
              .padding(.leading, 16 + 8 + 10)
          }
        }
      }
      .onMove { movedIndexes, destinationIndex in
        Bookmark.move(movedIndexes, to: destinationIndex, context: modelContext)
      }
      .onDelete { deletedIndexes in
        Bookmark.delete(at: deletedIndexes, context: modelContext)
      }
    }
    .onDeleteCommand {
      switch self.selection {
      case .bookmark(let bookmark):
        Bookmark.delete(bookmark, context: modelContext)
      default:
        break
      }
      
//      if let bookmark = selection,
//         bookmark.type != .temporary {
//        Bookmark.delete(bookmark, context: modelContext)
//      }
    }
    .environment(\.defaultMinListRowHeight, 34)
    .listStyle(.inset)
    .alternatingRowBackgrounds(.enabled)
    .onChange(of: AppState.shared.cloudKitReady) {
      if attemptedPrepopulate {
        print("Tracker: Already attempted to prepopulate bookmarks")
        return
      }
      
      print("Tracker: Prepopulating bookmarks")
      
      attemptedPrepopulate = true
      
      // Make sure default bookmarks are there when empty.
      Bookmark.populateDefaults(context: modelContext)
    }
    .onAppear {
//      Bookmark.deleteAll(context: modelContext)
    }
    .contextMenu(forSelectionType: TrackerSelection.self) { items in
      if let item = items.first {
        switch item {
        case .bookmark(let bookmark):
          self.bookmarkContextMenu(bookmark)
        case .bookmarkServer(let server):
          self.bookmarkServerContextMenu(server)
        }
      }
    } primaryAction: { items in
      guard let clickedItem = items.first else {
        return
      }
      
      switch clickedItem {
      case .bookmark(let bookmark):
        if bookmark.type == .server {
          if let s = bookmark.server {
            openWindow(id: "server", value: s)
          }
        }
        else if bookmark.type == .tracker {
          if NSEvent.modifierFlags.contains(.option) {
            trackerSheetBookmark = bookmark
          }
          else {
            self.toggleExpanded(for: bookmark)
          }
        }
        
      case .bookmarkServer(let bookmarkServer):
        openWindow(id: "server", value: bookmarkServer.server)
      }
    }
    .fileExporter(isPresented: $bookmarkExportActive, document: bookmarkExport, contentTypes: [.data], defaultFilename: "\(bookmarkExport?.bookmark.name ?? "Hotline Bookmark").hlbm", onCompletion: { result in
      switch result {
      case .success(let fileURL):
        print("Hotline Bookmark: Successfully exported:", fileURL)
      case .failure(let err):
        print("Hotline Bookmark: Failed to export:", err)
      }
      
      bookmarkExport = nil
      bookmarkExportActive = false
    }, onCancellation: {})
    .onKeyPress(.rightArrow) {
      switch self.selection {
      case .bookmark(let bookmark):
        if bookmark.type == .tracker {
          self.setExpanded(true, for: bookmark)
          return .handled
        }
      default:
        break
      }
      
      return .ignored
    }
    .onKeyPress(.leftArrow) {
      switch self.selection {
      case .bookmark(let bookmark):
        if bookmark.type == .tracker {
          self.setExpanded(false, for: bookmark)
          return .handled
        }
      default:
        break
      }
      
      return .ignored
    }
    .onDrop(of: [UTType.fileURL], isTargeted: $fileDropActive) { providers, dropPoint in
      for provider in providers {
        let _ = provider.loadDataRepresentation(for: UTType.fileURL) { dataRepresentation, err in
          // HOTLINE CREATOR CODE: 1213484099
          // HOTLINE BOOKMARK TYPE CODE: 1213489773
          
          if let filePathData = dataRepresentation,
             let filePath = String(data: filePathData, encoding: .utf8),
             let fileURL = URL(string: filePath) {
            
            print("Hotline Bookmark: Dropped from ", fileURL.path(percentEncoded: false))
            
            DispatchQueue.main.async {
              if let newBookmark = Bookmark(fileURL: fileURL) {
                print("Hotline Bookmark: Added bookmark.")
                Bookmark.add(newBookmark, context: modelContext)
              }
              else {
                print("Hotline Bookmark: Failed to parse.")
              }
            }
          }
        }
      }
      
      return true
    }
    .sheet(item: $trackerSheetBookmark) { item in
      TrackerBookmarkSheet(item)
    }
    .sheet(isPresented: $trackerSheetPresented) {
      TrackerBookmarkSheet()
    }
    .sheet(item: $serverSheetBookmark) { item in
      ServerBookmarkSheet(item)
    }
    .navigationTitle("Servers")
    .toolbar {
      if #available(macOS 26.0, *) {
        ToolbarItem(placement: .navigation) {
          self.hotlineLogoImage
        }
        .sharedBackgroundVisibility(.hidden)
      }
      else {
        ToolbarItem(placement: .navigation) {
          self.hotlineLogoImage
        }
      }
      
      ToolbarItem(placement: .primaryAction) {
        Button {
          self.refreshing = true
          self.refresh()
          self.refreshing = false
        } label: {
          Label("Refresh", systemImage: "arrow.clockwise")
        }
        .disabled(refreshing)
        .help("Refresh Trackers")
      }
      
      ToolbarItem(placement: .primaryAction) {
        Button {
          trackerSheetPresented = true
        } label: {
          Label("Add Tracker", systemImage: "point.3.filled.connected.trianglepath.dotted")
        }
        .help("Add Tracker")
      }
      
      ToolbarItem(placement: .primaryAction) {
        Button {
          openWindow(id: "server")
        } label: {
          Label("Connect to Server", systemImage: "globe.americas.fill")
        }
        .help("Connect to Server")
      }
    }
    .onOpenURL(perform: { url in
      if let s = Server(url: url) {
        openWindow(id: "server", value: s)
      }
    })
    .searchable(text: $searchText, isPresented: $isSearching, placement: .automatic, prompt: "Search")
    .background(Button("", action: { isSearching = true }).keyboardShortcut("f").hidden())
  }
  
  private var hotlineLogoImage: some View {
    Image("Hotline")
      .resizable()
      .renderingMode(.template)
      .scaledToFit()
      .foregroundColor(Color(hex: 0xE10000))
      .frame(width: 9)
      .opacity(controlActiveState == .inactive ? 0.5 : 1.0)
  }
  
  @ViewBuilder
  func bookmarkServerContextMenu(_ server: BookmarkServer) -> some View {
    Button {
      let newBookmark = Bookmark(type: .server, name: server.name ?? server.address, address: server.address, port: server.port, login: nil, password: nil)
      Bookmark.add(newBookmark, context: modelContext)
    } label: {
      Label("Bookmark", systemImage: "bookmark")
    }
    
    Divider()
    
    Button {
      NSPasteboard.general.clearContents()
      let displayAddress = (server.port == HotlinePorts.DefaultServerPort) ? server.address : "\(server.address):\(server.port)"
      NSPasteboard.general.setString(displayAddress, forType: .string)
    } label: {
      Label("Copy Address", systemImage: "doc.on.doc")
    }
  }
  
  @ViewBuilder
  func bookmarkContextMenu(_ bookmark: Bookmark) -> some View {
    Button {
      NSPasteboard.general.clearContents()
      NSPasteboard.general.setString(bookmark.displayAddress, forType: .string)
    } label: {
      Label("Copy Address", systemImage: "doc.on.doc")
    }

    Divider()

    if bookmark.type == .tracker {
      Button {
        trackerSheetBookmark = bookmark
      } label: {
        Label("Edit Tracker...", systemImage: "pencil")
      }
    }

    if bookmark.type == .server {
      Button {
        serverSheetBookmark = bookmark
      } label: {
        Label("Edit Bookmark...", systemImage: "pencil")
      }

      Button {
        bookmarkExport = BookmarkDocument(bookmark: bookmark)
        bookmarkExportActive = true
      } label: {
        Label("Export Bookmark...", systemImage: "square.and.arrow.down")
      }
    }

    Divider()

    Button {
      Bookmark.delete(bookmark, context: modelContext)
    } label: {
      Label(bookmark.type == .tracker ? "Delete Tracker" : "Delete Bookmark", systemImage: "trash")
    }
  }

  
  func refresh() {
    // When a tracker is selected, refresh only that tracker.
    if let trackerSelection = self.selection {
      switch trackerSelection {
      case .bookmark(let bookmark):
        if bookmark.type == .tracker {
          if self.expandedTrackers.contains(bookmark) {
            // Already expanded, cancel old fetch and start new one
            self.fetchTasks[bookmark]?.cancel()
            let task = Task {
              await self.fetchServers(for: bookmark)
            }
            self.fetchTasks[bookmark] = task
          } else {
            // Not expanded, expand it (which also fetches)
            self.setExpanded(true, for: bookmark)
          }
          return
        }
        break
      default:
        break
      }
    }

    // Otherwise refresh/expand all trackers.
    for bookmark in self.bookmarks {
      if bookmark.type == .tracker {
        if self.expandedTrackers.contains(bookmark) {
          // Already expanded, cancel old fetch and start new one
          self.fetchTasks[bookmark]?.cancel()
          let task = Task {
            await self.fetchServers(for: bookmark)
          }
          self.fetchTasks[bookmark] = task
        } else {
          // Not expanded, expand it (which also fetches)
          self.setExpanded(true, for: bookmark)
        }
      }
    }
  }
  
  func toggleExpanded(for bookmark: Bookmark) {
    guard bookmark.type == .tracker else { return }
    self.setExpanded(!self.expandedTrackers.contains(bookmark), for: bookmark)
  }

  func setExpanded(_ expanded: Bool, for bookmark: Bookmark) {
    guard bookmark.type == .tracker else { return }

    if expanded && !self.expandedTrackers.contains(bookmark) {
      self.expandedTrackers.insert(bookmark)
      let task = Task {
        await self.fetchServers(for: bookmark)
      }
      self.fetchTasks[bookmark] = task
    } else if !expanded && self.expandedTrackers.contains(bookmark) {
      // Cancel ongoing fetch and clear data
      self.fetchTasks[bookmark]?.cancel()
      self.fetchTasks[bookmark] = nil
      self.expandedTrackers.remove(bookmark)
      self.trackerServers[bookmark] = nil
      self.loadingTrackers.remove(bookmark)
    }
  }

  private func fetchServers(for bookmark: Bookmark) async {
    print("TrackerView.fetchServers: Starting fetch for bookmark: \(bookmark.name)")
    self.loadingTrackers.insert(bookmark)
    let servers = await bookmark.fetchServers()
    print("TrackerView.fetchServers: Got \(servers.count) servers from bookmark.fetchServers()")
    await MainActor.run {
      print("TrackerView.fetchServers: Assigning \(servers.count) servers to trackerServers[\(bookmark.name)]")
      self.trackerServers[bookmark] = servers
      self.loadingTrackers.remove(bookmark)
      self.fetchTasks[bookmark] = nil  // Clean up completed task
      print("TrackerView.fetchServers: trackerServers now has \(self.trackerServers.count) entries")
      print("TrackerView.fetchServers: Verification - trackerServers[\(bookmark.name)] now has \(self.trackerServers[bookmark]?.count ?? -1) servers")
    }
  }
}

struct TrackerBookmarkSheet: View {
  @Environment(\.dismiss) private var dismiss
  @Environment(\.modelContext) private var modelContext
  
  @State private var bookmark: Bookmark? = nil
  @State private var trackerAddress: String = ""
  @State private var trackerName: String = ""
  
  init() {
    
  }
  
  init(_ editingBookmark: Bookmark) {
    _bookmark = .init(initialValue: editingBookmark)
    _trackerAddress = .init(initialValue: editingBookmark.displayAddress)
    _trackerName = .init(initialValue: editingBookmark.name)
  }
  
  var body: some View {
    VStack(alignment: .leading) {
      if self.bookmark == nil {
        Text("Type the address and name of a Hotline Tracker:")
          .foregroundStyle(.secondary)
          .padding(.bottom, 8)
      }
      
      Form {
        Group {
          TextField(text: $trackerAddress) {
            Text("Address:")
          }
          TextField(text: $trackerName, prompt: Text("Optional")) {
            Text("Name:")
          }
        }
        .textFieldStyle(.roundedBorder)
        .controlSize(.large)
      }
    }
    .frame(width: 300)
    .fixedSize(horizontal: true, vertical: true)
    .padding()
    .toolbar {
      ToolbarItem(placement: .confirmationAction) {
        Button {
          self.saveTracker()
        } label: {
          if self.bookmark != nil {
            Text("Save Tracker")
          }
          else {
            Text("Add Tracker")
          }
        }
      }
      ToolbarItem(placement: .cancellationAction) {
        Button("Cancel") {
          self.trackerName = ""
          self.trackerAddress = ""
          
          self.dismiss()
        }
      }
    }
  }
  
  private func saveTracker() {
    var displayName = trackerName.trimmingCharacters(in: .whitespacesAndNewlines)
    let (host, port) = Tracker.parseTrackerAddressAndPort(trackerAddress)
    
    if displayName.isEmpty {
      displayName = host
    }
    
    if !displayName.isEmpty && !host.isEmpty {
      if !host.isEmpty {
        if self.bookmark != nil {
          // We're editing an existing bookmark.
          self.bookmark?.name = displayName
          self.bookmark?.address = host
          self.bookmark?.port = port
        }
        else {
          // We're creating a new bookmark.
          let newBookmark = Bookmark(type: .tracker, name: displayName, address: host, port: port)
          Bookmark.add(newBookmark, context: modelContext)
        }
        
        self.trackerName = ""
        self.trackerAddress = ""
        
        self.dismiss()
      }
    }
  }
}

struct ServerBookmarkSheet: View {
  @Environment(\.dismiss) private var dismiss
  @Environment(\.modelContext) private var modelContext

  @State private var bookmark: Bookmark
  @State private var serverName: String = ""
  @State private var serverAddress: String = ""
  @State private var serverLogin: String = ""
  @State private var serverPassword: String = ""

  init(_ editingBookmark: Bookmark) {
    _bookmark = .init(initialValue: editingBookmark)
    _serverName = .init(initialValue: editingBookmark.name)
    _serverAddress = .init(initialValue: editingBookmark.displayAddress)
    _serverLogin = .init(initialValue: editingBookmark.login ?? "")
    _serverPassword = .init(initialValue: editingBookmark.password ?? "")
  }

  var body: some View {
    VStack(alignment: .leading) {
      Form {
        Group {
          TextField(text: $serverName) {
            Text("Name:")
          }
          .padding(.bottom)
          
          TextField(text: $serverAddress) {
            Text("Address:")
          }
          TextField(text: $serverLogin, prompt: Text("Optional")) {
            Text("Login:")
          }
          SecureField(text: $serverPassword, prompt: Text("Optional")) {
            Text("Password:")
          }
        }
        .textFieldStyle(.roundedBorder)
        .controlSize(.large)
      }
    }
    .frame(width: 300)
    .fixedSize(horizontal: true, vertical: true)
    .padding()
    .toolbar {
      ToolbarItem(placement: .confirmationAction) {
        Button("Save Bookmark") {
          let displayName = self.serverName.trimmingCharacters(in: .whitespacesAndNewlines)
          let (host, port) = Server.parseServerAddressAndPort(self.serverAddress)
          let login = self.serverLogin.trimmingCharacters(in: .whitespacesAndNewlines)
          let password = self.serverPassword

          if !displayName.isEmpty && !host.isEmpty {
            self.bookmark.name = displayName
            self.bookmark.address = host
            self.bookmark.port = port
            self.bookmark.login = login.isEmpty ? nil : login
            self.bookmark.password = password.isEmpty ? nil : password

            self.dismiss()
          }
        }
      }
      ToolbarItem(placement: .cancellationAction) {
        Button("Cancel") {
          self.dismiss()
        }
      }
    }
  }
}

struct TrackerBookmarkServerView: View {
  let server: BookmarkServer

  var body: some View {
    HStack(alignment: .center, spacing: 6) {
      Image("Server")
        .resizable()
        .scaledToFit()
        .frame(width: 16, height: 16, alignment: .center)
      Text(self.server.name ?? "Server").lineLimit(1).truncationMode(.tail)
      if let serverDescription = self.server.description {
        Text(serverDescription)
          .foregroundStyle(.secondary)
          .lineLimit(1)
          .truncationMode(.tail)
      }
      Spacer(minLength: 0)
      if self.server.users > 0 {
        Text(String(self.server.users))
          .foregroundStyle(.secondary)
          .lineLimit(1)

        Circle()
          .fill(.fileComplete)
          .frame(width: 7, height: 7)
          .keyframeAnimator(initialValue: 1.0, repeating: true) { content, opacity in
            content.opacity(opacity)
          } keyframes: { _ in
            CubicKeyframe(1.0, duration: 2.0)  // Stay visible for 1 second
            CubicKeyframe(0.6, duration: 0.5) // Fade out quickly
            CubicKeyframe(1.0, duration: 0.5) // Fade in quickly
          }
          .padding(.trailing, 6)
      }
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
  }
}

struct TrackerItemView: View {
  let bookmark: Bookmark
  let isExpanded: Bool
  let isLoading: Bool
  let count: Int
  let onToggleExpanded: () -> Void
  @Environment(\.appearsActive) private var appearsActive

  var body: some View {
    HStack(alignment: .center, spacing: 6) {
      if bookmark.type == .tracker {
        Button {
          self.onToggleExpanded()
        } label: {
          Text(Image(systemName: self.isExpanded ? "chevron.down" : "chevron.right"))
            .bold()
            .font(.system(size: 10))
            .opacity(0.5)
            .frame(alignment: .center)
        }
        .buttonStyle(.plain)
        .frame(width: 10)
        .padding(.leading, 4)
        .padding(.trailing, 2)
      }

      switch bookmark.type {
      case .tracker:
        Image("Tracker")
          .resizable()
          .scaledToFit()
          .frame(width: 16, height: 16, alignment: .center)
        Text(bookmark.name).bold().lineLimit(1).truncationMode(.tail)
        if isLoading {
          ProgressView()
            .padding([.leading, .trailing], 2)
            .controlSize(.small)
        }
        Spacer(minLength: 0)
        if isExpanded && count > 0 {
          HStack(spacing: 4) {
            Text(String(count))

            SpinningGlobeView()
              .fontWeight(.semibold)
              .frame(width: 12, height: 12)
          }
          .padding(.horizontal, 6)
          .padding(.vertical, 2)
          .foregroundStyle(.secondary)
//          .background(.quinary)
          .clipShape(.capsule)
        }
      case .server:
        Image(systemName: "bookmark.fill")
          .resizable()
          .scaledToFit()
          .foregroundStyle(Color.secondary)
          .frame(width: 11, height: 11, alignment: .center)
          .opacity(0.75)
          .padding(.leading, 3)
          .padding(.trailing, 2)
        Image("Server")
          .resizable()
          .scaledToFit()
          .frame(width: 16, height: 16, alignment: .center)
        Text(bookmark.name).lineLimit(1).truncationMode(.tail)
        Spacer(minLength: 0)
      }
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
  }
}

#if DEBUG
private struct TrackerViewPreview: View {
  @State var selection: TrackerSelection? = nil

  var body: some View {
    TrackerView(selection: $selection)
  }
}

#Preview {
  TrackerViewPreview()
}
#endif