import SwiftUI import SwiftData struct RunHistoryView: View { @Environment(\.modelContext) private var modelContext @Query(sort: \SyncRun.startedAt, order: .reverse) private var runs: [SyncRun] @State private var selection: SyncRun? @State private var persistenceError: String? let onBack: () -> Void var body: some View { HSplitView { VStack(spacing: 0) { HStack(spacing: 10) { VStack(alignment: .leading, spacing: 2) { Text("Recent Runs") .font(.headline) Text("\(runs.count) recorded") .font(.caption) .foregroundStyle(.secondary) } Spacer() Button("Clear", role: .destructive) { runs.forEach(modelContext.delete) selection = nil saveHistoryChanges() } .buttonStyle(.bordered) .controlSize(.small) .disabled(runs.isEmpty) } .padding(14) Divider() if runs.isEmpty { ContentUnavailableView( "No Run History", systemImage: "clock.arrow.circlepath", description: Text("Completed, failed and background synchronisations will appear here.") ) .frame(maxWidth: .infinity, maxHeight: .infinity) } else { List(selection: $selection) { ForEach(runs) { run in HStack(spacing: 11) { Image(systemName: statusIcon(run)) .foregroundStyle(statusColour(run)) VStack(alignment: .leading, spacing: 4) { HStack { Text(run.jobName) .fontWeight(.semibold) .lineLimit(1) Spacer() StatusBadge(run: run) } Text(run.startedAt, format: .dateTime.day().month(.abbreviated).year().hour().minute()) .font(.caption) .foregroundStyle(.secondary) HStack(spacing: 6) { Label(run.trigger.title, systemImage: run.trigger.systemImage) .font(.caption2.weight(.semibold)) .foregroundStyle(run.trigger == .watched ? Color.purple : Color.blue) if run.wasDryRun { Text("Dry run") .font(.caption2.weight(.semibold)) .foregroundStyle(.blue) } } } } .padding(.vertical, 3) .tag(run) .contextMenu { Button("Delete Log", role: .destructive) { if selection?.id == run.id { selection = nil } modelContext.delete(run) saveHistoryChanges() } } } } .listStyle(.sidebar) } } .frame(minWidth: 300, idealWidth: 340, maxWidth: 390) Group { if let selection { RunLogView(run: selection) } else { ContentUnavailableView( "Select a Run", systemImage: "clock.arrow.circlepath", description: Text("Select a completed or failed run to inspect its summary and full log.") ) } } .frame(minWidth: 620, maxWidth: .infinity, maxHeight: .infinity) } .frame(maxWidth: .infinity, maxHeight: .infinity) .alert("Database Error", isPresented: Binding( get: { persistenceError != nil }, set: { if !$0 { persistenceError = nil } } )) { Button("OK", role: .cancel) { persistenceError = nil } } message: { Text(persistenceError ?? "Unable to update run history.") } } private func saveHistoryChanges() { do { try modelContext.save() } catch { persistenceError = error.localizedDescription } } private func statusIcon(_ run: SyncRun) -> String { if run.isRunning { return "arrow.triangle.2.circlepath" } if run.wasCancelled { return "xmark.circle.fill" } return run.succeeded ? "checkmark.circle.fill" : "exclamationmark.triangle.fill" } private func statusColour(_ run: SyncRun) -> Color { if run.isRunning { return .blue } if run.wasCancelled { return .secondary } return run.succeeded ? .green : .orange } }