Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/Inspector.swift3// Description: The trailing inspector of the reader (macOS): sender card and thread details — participants, dates, folders, attachments.4import SPMailCore5import SwiftUI67struct InspectorView: View {8 let scene: MailScene9 private var reader: ReaderState { scene.reader }1011 private var participants: [EmailAddress] {12 var seen = Set<String>(), out: [EmailAddress] = []13 for e in reader.conversation { for a in e.from + e.to + e.cc where seen.insert(a.email.lowercased()).inserted { out.append(a) } }14 return out15 }16 private var folders: [String] {17 let ids = Set(reader.conversation.flatMap(\.mailboxIds))18 return scene.activeMailboxes.filter { ids.contains($0.id) }.map(MailScene.title(of:))19 }20 private var attachments: [BodyPart] { reader.conversation.flatMap { reader.bodies[$0.id]?.visibleAttachments() ?? [] } }2122 var body: some View {23 Group {24 if let email = reader.focusedEmail {25 ScrollView {26 VStack(alignment: .leading, spacing: SPSpacing.xxl) {27 senderCard(email)28 section(L("Conversation")) {29 row(L("Messages"), "\(reader.conversation.count)")30 if let first = reader.conversation.map(\.date).min() { row(L("Début"), first.formatted(date: .abbreviated, time: .shortened)) }31 if reader.conversation.count > 1, let last = reader.conversation.map(\.date).max() { row(L("Dernier message"), last.formatted(date: .abbreviated, time: .shortened)) }32 if !folders.isEmpty { row(folders.count == 1 ? L("Dossier") : L("Dossiers"), folders.joined(separator: ", ")) }33 if let box = scene.app.box(scene.activeAccount) { row(L("Boîte"), box.address) }34 }35 section(L("Participants")) {36 ForEach(participants) { p in37 HStack(spacing: SPSpacing.m) {38 AvatarView(name: p.name, address: p.email, size: 22)39 VStack(alignment: .leading, spacing: 0) {40 Text(p.displayName).font(SPFont.callout).foregroundStyle(SPColor.text).lineLimit(1)41 if p.name?.isEmpty == false { Text(p.email).font(.caption).foregroundStyle(SPColor.text3).lineLimit(1).truncationMode(.middle) }42 }43 }44 .contextMenu {45 Button("Nouveau message") { compose(to: p) }46 Button("Copier l’adresse") { copy(p.email) }47 }48 }49 }50 if !attachments.isEmpty {51 section(L("Pièces jointes")) {52 ForEach(attachments) { part in53 Label { Text(part.displayName).lineLimit(1).truncationMode(.middle) } icon: { Image(systemName: AttachmentInfo(id: part.id, name: part.displayName, type: part.type, size: part.size).glyph) }54 .font(SPFont.callout).foregroundStyle(SPColor.text2)55 }56 }57 }58 }59 .padding(SPSpacing.xl)60 .frame(maxWidth: .infinity, alignment: .leading)61 }62 } else {63 SPEmptyState(icon: "sidebar.trailing", title: L("Aucun message sélectionné"))64 }65 }66 .background(SPColor.bg)67 }6869 private func senderCard(_ email: EmailSummary) -> some View {70 VStack(alignment: .leading, spacing: SPSpacing.l) {71 AvatarView(name: email.sender?.displayName, address: email.sender?.email ?? "", size: 52)72 VStack(alignment: .leading, spacing: 2) {73 Text(email.sender?.displayName ?? L("(expéditeur inconnu)")).font(SPFont.headline).foregroundStyle(SPColor.text).textSelection(.enabled)74 Text(email.sender?.email ?? "").font(SPFont.callout).foregroundStyle(SPColor.text2).textSelection(.enabled)75 }76 if let b = reader.bodies[email.id] {77 let auth = AuthenticationSummary.parse(b.authenticationResults)78 AuthBadges(auth: auth, risk: auth.risk(fromDomain: email.sender?.domain ?? ""))79 }80 if let sender = email.sender {81 HStack(spacing: SPSpacing.m) {82 Button { compose(to: sender) } label: { Label("Écrire", systemImage: "square.and.pencil") }83 Button { copy(sender.email) } label: { Label("Copier l’adresse", systemImage: "doc.on.doc") }84 }85 .controlSize(.small)86 // the sender as a contact: profile when saved, « add to contacts » otherwise87 if scene.app.next.has(.contacts) { InspectorContactCard(scene: scene, sender: sender) }88 }89 }90 .accessibilityElement(children: .contain)91 }9293 private func section<V: View>(_ title: String, @ViewBuilder _ content: () -> V) -> some View {94 VStack(alignment: .leading, spacing: SPSpacing.m) {95 Text(title).font(SPFont.caption).foregroundStyle(SPColor.text3).textCase(.uppercase).accessibilityAddTraits(.isHeader)96 content()97 }98 }99 private func row(_ label: String, _ value: String) -> some View {100 LabeledContent { Text(value).foregroundStyle(SPColor.text).multilineTextAlignment(.trailing).textSelection(.enabled) } label: { Text(label).foregroundStyle(SPColor.text3) }101 .font(SPFont.callout)102 }103 private func compose(to address: EmailAddress) {104 guard let url = URL(string: "mailto:" + (address.email.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? address.email)) else { return }105 Task { await scene.compose(.mailto(url)) }106 }107 private func copy(_ s: String) {108 #if os(macOS)109 NSPasteboard.general.clearContents(); NSPasteboard.general.setString(s, forType: .string)110 #else111 UIPasteboard.general.string = s112 #endif113 }114}115