Files
Pulse/Sources/PulseUI/Features/MessageDetails/ConsoleMessageMetadataView.swift
T
2023-01-30 11:24:04 -05:00

60 lines
1.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The MIT License (MIT)
//
// Copyright (c) 20202023 Alexander Grebenyuk (github.com/kean).
import SwiftUI
import Pulse
struct ConsoleMessageMetadataView: View {
let message: LoggerMessageEntity
var body: some View {
RichTextView(viewModel: .init(string: string))
#if !os(macOS)
.navigationTitle("Message Details")
#endif
}
private var string: NSAttributedString {
let renderer = TextRenderer()
renderer.render(sections)
return renderer.make()
}
private var sections: [KeyValueSectionViewModel] {
return [
KeyValueSectionViewModel(title: "Summary", color: .textColor(for: message.logLevel), items: [
("Date", DateFormatter.fullDateFormatter.string(from: message.createdAt)),
("Level", LoggerStore.Level(rawValue: message.level)?.name),
("Label", message.label.nonEmpty)
]),
KeyValueSectionViewModel(title: "Details", color: .primary, items: [
("File", message.file.nonEmpty),
("Function", message.function.nonEmpty),
("Line", message.line == 0 ? nil : "\(message.line)"),
]),
KeyValueSectionViewModel(title: "Metadata", color: .indigo, items: metadataItems)
]
}
private var metadataItems: [(String, String?)] {
message.metadata.sorted(by: { $0.key < $1.key }).map { ($0.key, $0.value )}
}
}
private extension String {
var nonEmpty: String? {
isEmpty ? nil : self
}
}
#if DEBUG
struct ConsoleMessageMetadataView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
ConsoleMessageMetadataView(message: makeMockMessage())
}
}
}
#endif