Branchwell

Jotley / app / macOS / Views / TabViews / SummaryTabView.swift

1import SwiftUI
2import AppKit
3
4// View for the Summary tab content
5struct SummaryTabView: View {
6 @ObservedObject var note: Note
7 @Binding var editableSummary: String
8 @FocusState.Binding var isSummaryEditorFocused: Bool
9 @Binding var inlineEditRequestCounter: Int
10 @Binding var isGeneratingSummary: Bool
11 @Binding var summaryGenerationError: String?
12 @Binding var showRawMarkdown: Bool
13 let summaryLoadingStartedAt: Date?
14 @ObservedObject var streamingSummaryViewModel: StreamingSummaryViewModel
15 @ObservedObject private var themeManager = ThemeManager.shared
16 @State private var inlineEditRequested = false
17 @State private var previousRawMarkdownMode = false
18
19 // Optional callback for generating summary
20 private let compactContentPadding = EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8)
21
22 private var isPlaceholderSummaryText: Bool {
23 let editable = editableSummary.trimmingCharacters(in: .whitespacesAndNewlines)
24 let persisted = (note.summary ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
25 return editable == "Generating summary..." || persisted == "Generating summary..."
26 }
27
28 private var isInlineEditing: Bool {
29 !isGeneratingSummary && (showRawMarkdown || inlineEditRequested || isSummaryEditorFocused)
30 }
31
32 private var summaryTextToRender: String {
33 if isGeneratingSummary {
34 return streamingSummaryViewModel.displayedText
35 }
36 // Stale "Generating summary..." placeholder is not real content — render nothing
37 // so the tab falls through to its empty state instead of showing placeholder text.
38 if isPlaceholderSummaryText {
39 return ""
40 }
41 return editableSummary
42 }
43
44 private var hasSummaryTextToRender: Bool {
45 !summaryTextToRender.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
46 }
47
48 private var shouldShowSummaryLoader: Bool {
49 isGeneratingSummary && !hasSummaryTextToRender
50 }
51
52 private func beginInlineEditing() {
53 guard !isInlineEditing else { return }
54
55 previousRawMarkdownMode = showRawMarkdown
56 inlineEditRequested = true
57 showRawMarkdown = true
58
59 DispatchQueue.main.async {
60 isSummaryEditorFocused = true
61 }
62 }
63
64 private func finishInlineEditing() {
65 isSummaryEditorFocused = false
66
67 if inlineEditRequested {
68 inlineEditRequested = false
69 showRawMarkdown = previousRawMarkdownMode
70 }
71 }
72
73 private func handleInlineEditRequest(_ newValue: Int) {
74 guard newValue > 0, !isGeneratingSummary else { return }
75 // Consume the request so recreating this view cannot reopen the editor.
76 inlineEditRequestCounter = 0
77 beginInlineEditing()
78 }
79
80 private func endInlineEditingIfNeeded(oldValue: Bool, newValue: Bool) {
81 guard oldValue, !newValue, inlineEditRequested else { return }
82 inlineEditRequested = false
83 showRawMarkdown = previousRawMarkdownMode
84 }
85
86 var body: some View {
87 Group {
88 if shouldShowSummaryLoader {
89 Text("Long transcripts are reviewed in sections on this device. Your summary will appear here as it is written. You can open other notes while it finishes; it is saved automatically.")
90 .font(.system(size: 15)).foregroundColor(.themeTextSecondary)
91 .frame(maxWidth: .infinity, alignment: .leading).padding(16)
92 .accessibilityIdentifier("noteDetailSummaryLoading")
93 } else if isGeneratingSummary || hasSummaryTextToRender || isSummaryEditorFocused {
94 // Use our controlled displaySummary instead of directly accessing note.summary
95 VStack(alignment: .leading, spacing: 8) {
96 if let summaryGenerationError, !summaryGenerationError.isEmpty {
97 HStack(alignment: .top, spacing: 8) {
98 Image(systemName: "exclamationmark.triangle.fill")
99 .foregroundColor(.orange.opacity(0.9))
100 Text(summaryGenerationError)
101 .font(.system(size: 13))
102 .foregroundColor(Color.themeTextSecondary)
103 .fixedSize(horizontal: false, vertical: true)
104 }
105 .padding(.horizontal, 12)
106 .padding(.vertical, 10)
107 .frame(maxWidth: .infinity, alignment: .leading)
108 .background(.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 10))
109 }
110
111 ZStack(alignment: .topLeading) {
112 if isInlineEditing {
113 VStack(alignment: .leading, spacing: 8) {
114 HStack {
115 Spacer()
116
117 Button {
118 finishInlineEditing()
119 } label: {
120 Label {
121 Text("Done")
122 } icon: {
123 Image(systemName: "checkmark")
124 .foregroundStyle(Color.themePrimaryAccent)
125 }
126 }
127 .jotleyDismissButtonStyle()
128 .accessibilityIdentifier("noteDetailSummaryDoneButton")
129 }
130 .padding(.horizontal, 8)
131
132 NoteFindReplace(summary: $editableSummary)
133
134 ZStack(alignment: .topLeading) {
135 InlineEditableSummaryTextView(
136 text: $editableSummary,
137 isFocused: $isSummaryEditorFocused,
138 accentColor: NSColor(themeManager.currentThemeColor.color),
139 textColor: NSColor(Color.themeTextPrimary)
140 )
141 .clipped()
142 .padding(compactContentPadding)
143 .frame(
144 maxWidth: .infinity,
145 minHeight: ContentLayoutConstants.standardMinHeight,
146 alignment: .topLeading
147 )
148 .onAppear {
149 if inlineEditRequested {
150 DispatchQueue.main.async {
151 isSummaryEditorFocused = true
152 }
153 }
154 }
155 .accessibilityIdentifier("noteDetailSummaryEditor")
156
157 if editableSummary.isEmpty && !note.isFault && !isSummaryEditorFocused {
158 Text("No summary available")
159 .font(.body)
160 .foregroundColor(Color.themeTextSecondary.opacity(0.7))
161 .italic()
162 .padding(.leading, 24)
163 .padding(.top, 24)
164 .frame(maxWidth: .infinity, alignment: .topLeading)
165 }
166 }
167 .modifier(JotleyEditorField(isFocused: isSummaryEditorFocused))
168 }
169 } else {
170 if !hasSummaryTextToRender {
171 Text("No summary available")
172 .font(.body)
173 .foregroundColor(Color.themeTextSecondary.opacity(0.7))
174 .italic()
175 .padding(compactContentPadding)
176 .frame(maxWidth: .infinity, minHeight: ContentLayoutConstants.standardMinHeight, alignment: .center)
177 } else {
178 StructuredSummaryView(content: summaryTextToRender, isStreaming: isGeneratingSummary)
179 .padding(compactContentPadding)
180 .frame(maxWidth: .infinity, alignment: .topLeading)
181 .contentShape(Rectangle())
182 .onTapGesture(count: 2, perform: beginInlineEditing)
183 .accessibilityIdentifier("noteDetailSummaryContent")
184 .help("Double-click to edit summary")
185 }
186 }
187 }
188 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
189 .onExitCommand {
190 if isInlineEditing {
191 finishInlineEditing()
192 }
193 }
194 }
195 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
196 }
197 }
198 .onChange(of: isGeneratingSummary) { _, generating in
199 if generating {
200 inlineEditRequested = false
201 inlineEditRequestCounter = 0
202 isSummaryEditorFocused = false
203 showRawMarkdown = false
204 previousRawMarkdownMode = false
205 }
206 }
207 .onChange(of: isSummaryEditorFocused) { oldValue, newValue in
208 endInlineEditingIfNeeded(oldValue: oldValue, newValue: newValue)
209 }
210 .onAppear {
211 handleInlineEditRequest(inlineEditRequestCounter)
212 }
213 .onChange(of: inlineEditRequestCounter) { _, newValue in
214 handleInlineEditRequest(newValue)
215 }
216 }
217}