File size: 7,438 Bytes
b8370f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import ImageIO
import SwiftUI

@MainActor
struct ChatSourcePreviewsView: View {
    let sources: [ChatSourcePreview]
    let contextRevision: UUID
    let faviconsEnabled: Bool
    let loadFavicon: @MainActor @Sendable (String) async -> Data?
    @State private var selectedSource: ChatSourcePreview?

    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text(String(localized: "Sources"))
                .font(OpenClawChatTypography.captionSemiBold)
                .foregroundStyle(.secondary)
            ScrollView(.horizontal) {
                HStack(alignment: .top, spacing: 8) {
                    ForEach(self.sources) { source in
                        Button {
                            self.selectedSource = source
                        } label: {
                            self.card(source)
                        }
                        .buttonStyle(.plain)
                        .accessibilityLabel(Text(verbatim: "\(source.title), \(source.domain)"))
                        .accessibilityHint(String(localized: "Shows recorded source preview"))
                        .accessibilityIdentifier("chat-source-preview-card")
                        #if os(macOS)
                        .popover(isPresented: self.isSelected(source), arrowEdge: .bottom) {
                            ChatSourcePreviewDetail(source: source) { self.selectedSource = nil }
                                .frame(width: 320)
                        }
                        #endif
                    }
                }
                .padding(.vertical, 2)
            }
            .scrollIndicators(.hidden)
        }
        .accessibilityIdentifier("chat-source-previews")
        .onChange(of: self.sources) { _, sources in
            if let selected = self.selectedSource {
                self.selectedSource = sources.first { $0.id == selected.id }
            }
        }
        #if os(iOS)
        .sheet(item: self.$selectedSource) { source in
            ScrollView {
                ChatSourcePreviewDetail(source: source) { self.selectedSource = nil }
            }
            .presentationDetents([.medium, .large])
            .presentationDragIndicator(.visible)
        }
        #endif
    }

    private func card(_ source: ChatSourcePreview) -> some View {
        VStack(alignment: .leading, spacing: 6) {
            Text(verbatim: source.title)
                .font(OpenClawChatTypography.footnoteSemiBold)
                .lineLimit(2)
                .multilineTextAlignment(.leading)
                .frame(maxWidth: .infinity, alignment: .leading)
            HStack(spacing: 5) {
                ChatSourceFavicon(
                    host: source.domain,
                    contextRevision: self.contextRevision,
                    enabled: self.faviconsEnabled,
                    load: self.loadFavicon)
                Text(verbatim: source.domain)
                    .font(OpenClawChatTypography.caption2)
                    .lineLimit(1)
            }
            .foregroundStyle(.secondary)
        }
        .padding(10)
        .frame(width: 184, alignment: .leading)
        .frame(minHeight: 74, alignment: .topLeading)
        .background(.primary.opacity(0.035), in: RoundedRectangle(cornerRadius: 10))
        .overlay(RoundedRectangle(cornerRadius: 10).strokeBorder(.primary.opacity(0.12), lineWidth: 0.5))
        .contentShape(RoundedRectangle(cornerRadius: 10))
    }

    #if os(macOS)
    private func isSelected(_ source: ChatSourcePreview) -> Binding<Bool> {
        Binding(
            get: { self.selectedSource?.id == source.id },
            set: { if !$0 { self.selectedSource = nil } })
    }
    #endif
}

private struct ChatSourcePreviewDetail: View {
    let source: ChatSourcePreview
    let dismiss: () -> Void

    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            HStack {
                Text(verbatim: self.source.domain)
                    .font(OpenClawChatTypography.caption)
                    .foregroundStyle(.secondary)
                Spacer()
                Button(action: self.dismiss) {
                    Image(systemName: "xmark")
                        .font(.system(size: 13, weight: .semibold))
                        .frame(width: 28, height: 28)
                        .contentShape(Rectangle())
                }
                .buttonStyle(.plain)
                .accessibilityLabel(String(localized: "Close source preview"))
            }
            Text(verbatim: self.source.title)
                .font(OpenClawChatTypography.headline)
                .fixedSize(horizontal: false, vertical: true)
            if let excerpt = self.source.excerpt {
                Text(self.source.excerptKind == .page
                    ? String(localized: "Page excerpt")
                    : String(localized: "Search snippet"))
                    .font(OpenClawChatTypography.captionSemiBold)
                    .foregroundStyle(.secondary)
                Text(verbatim: excerpt)
                    .font(OpenClawChatTypography.callout)
                    .fixedSize(horizontal: false, vertical: true)
                    .textSelection(.enabled)
            } else {
                Text(String(localized: "No recorded excerpt available."))
                    .font(OpenClawChatTypography.callout)
                    .foregroundStyle(.secondary)
            }
            Link(destination: self.source.url) {
                Label {
                    Text(String(localized: "Open source"))
                        .font(OpenClawChatTypography.footnoteSemiBold)
                } icon: {
                    Image(systemName: "arrow.up.right")
                }
            }
        }
        .padding(20)
        .frame(maxWidth: .infinity, alignment: .leading)
        .accessibilityIdentifier("chat-source-preview-detail")
    }
}

@MainActor
private struct ChatSourceFavicon: View {
    let host: String
    let contextRevision: UUID
    let enabled: Bool
    let load: @MainActor @Sendable (String) async -> Data?
    @State private var image: CGImage?

    private struct RequestIdentity: Hashable {
        let host: String
        let revision: UUID
        let enabled: Bool
    }

    var body: some View {
        Group {
            if self.enabled, let image {
                Image(decorative: image, scale: 1)
                    .resizable()
                    .scaledToFit()
            } else {
                Image(systemName: "globe")
                    .font(.system(size: 12))
            }
        }
        .frame(width: 14, height: 14)
        .accessibilityHidden(true)
        .task(id: RequestIdentity(host: self.host, revision: self.contextRevision, enabled: self.enabled)) {
            self.image = nil
            guard self.enabled, let data = await self.load(self.host), !Task.isCancelled,
                  data.count <= 256 * 1024,
                  let source = CGImageSourceCreateWithData(data as CFData, nil)
            else { return }
            let options: [CFString: Any] = [
                kCGImageSourceCreateThumbnailFromImageAlways: true,
                kCGImageSourceCreateThumbnailWithTransform: true,
                kCGImageSourceThumbnailMaxPixelSize: 32,
            ]
            self.image = CGImageSourceCreateThumbnailAtIndex(source, 0, options as CFDictionary)
        }
    }
}