Branchwell

Jotley / app / macOS / Views / NoteDetailPlaceholderView+MetadataContent.swift

1import SwiftUI
2import AppKit
3
4extension NoteDetailPlaceholderView {
5 var headerContent: some View {
6 Group {
7 if MacScreenshotFixture.isActive {
8 Text(note.titleOrUntitled)
9 .font(.system(size: 24, weight: .semibold))
10 .foregroundStyle(Color.themeTextPrimary)
11 .lineLimit(2)
12 .padding(.horizontal, 2)
13 } else {
14 editableTitleField
15 }
16 }
17 .frame(maxWidth: .infinity, alignment: .leading)
18 .fixedSize(horizontal: false, vertical: true)
19 .accessibilityIdentifier("noteDetailTitle")
20 }
21
22 private var editableTitleField: some View {
23 ThemedTextField(
24 text: $manualTitle,
25 placeholder: "Enter a title...",
26 font: NSFont.systemFont(ofSize: 24, weight: .semibold),
27 textColor: NSColor(Color.themeTextPrimary),
28 onCommit: {
29 let trimmed = manualTitle.trimmingCharacters(in: .whitespacesAndNewlines)
30 if !trimmed.isEmpty && note.title != trimmed {
31 note.title = trimmed
32 do {
33 try persistenceController.save()
34 } catch {
35 viewModel.report(error)
36 }
37 }
38 },
39 maxLines: 2
40 )
41 .onAppear {
42 manualTitle = note.titleOrUntitled
43 // Load tags directly, they are already formatted and de-duplicated in CoreData
44 currentTags = note.tagsAsString?.split(separator: ",").map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }.sorted() ?? []
45 currentTags.sort()
46 // When appearing, ensure we're showing original transcript if this note is being recorded
47 let vm = viewModel
48 if vm.recorder.isRecording &&
49 vm.newNoteBeingRecorded == note {
50 // REMOVED preferCleanedTranscript = false
51 }
52 }
53 .onChange(of: note.title) { _, _ in
54 manualTitle = note.titleOrUntitled
55 }
56 .onChange(of: note.objectID) { _, _ in
57 manualTitle = note.titleOrUntitled
58 editableSummary = note.summary ?? ""
59 editableTranscript = note.transcription ?? ""
60 // Load tags directly, they are already formatted and de-duplicated in CoreData
61 currentTags = note.tagsAsString?.split(separator: ",").map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }.sorted() ?? []
62 currentTags.sort()
63 // Don't reset preferCleanedTranscript, since we want it to be sticky
64 }
65 .onChange(of: manualTitle) { _, newText in
66 let trimmed = newText.trimmingCharacters(in: .whitespacesAndNewlines)
67 if !trimmed.isEmpty && note.title != trimmed {
68 note.title = trimmed
69 do { try persistenceController.save() } catch { viewModel.report(error) }
70 }
71 }
72 .onChange(of: note.tagsAsString) { _, newTags in
73 // Load tags directly, they are already formatted and de-duplicated in CoreData
74 currentTags = newTags?.split(separator: ",").map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }.sorted() ?? []
75 }
76 }
77
78 var dateAndTagsContent: some View {
79 VStack(alignment: .leading, spacing: 6) {
80 // Top row: date + folder + collapsed tag summary
81 HStack(alignment: .center, spacing: 12) {
82 createdDateText
83
84 folderChip
85
86 if showTags {
87 collapsedTagHeader
88 }
89
90 Spacer()
91 }
92
93 // Expanded tag flow (shown below the top row when expanded)
94 if showTags && isTagsExpanded {
95 expandedTagsContent
96 }
97 }
98 .padding(.bottom, 6)
99 }
100
101 /// Compact clickable tag summary: "🏷 N tags >" or "+ Add Tag"
102 @ViewBuilder
103 private var collapsedTagHeader: some View {
104 if currentTags.isEmpty {
105 Button {
106 showAddTagPopover = true
107 } label: {
108 JotleyAccentIconLabel(title: "Add Tag", systemImage: "plus.circle.fill")
109 .noteMetadataPill()
110 }
111 .buttonStyle(.plain)
112 .accessibilityIdentifier("noteDetailAddTagButton")
113 .popover(isPresented: $showAddTagPopover, arrowEdge: .bottom) {
114 AddTagPopoverView(
115 note: note,
116 showPopover: $showAddTagPopover,
117 newTagText: $newTagText,
118 currentTags: $currentTags,
119 isEditingTags: $isEditingTags,
120 saveContext: { self.saveContext() }
121 )
122 .frame(idealWidth: 250)
123 }
124 } else {
125 Button {
126 withAnimation(.easeInOut(duration: 0.2)) {
127 isTagsExpanded.toggle()
128 }
129 if isTagsExpanded {
130 isEditingTags = true
131 }
132 } label: {
133 HStack(spacing: 4) {
134 Image(systemName: "tag")
135 .foregroundStyle(Color.themePrimaryAccent)
136 Text("\(currentTags.count) \(currentTags.count == 1 ? "tag" : "tags")")
137 .foregroundStyle(.primary)
138 Image(systemName: "chevron.right")
139 .font(.system(size: 9, weight: .semibold))
140 .rotationEffect(.degrees(isTagsExpanded ? 90 : 0))
141 }
142 .foregroundStyle(.secondary)
143 .noteMetadataPill()
144 }
145 .buttonStyle(.plain)
146 .accessibilityIdentifier("noteDetailTagCount")
147 }
148 }
149
150 /// Full tag flow layout with edit/add controls.
151 private var expandedTagsContent: some View {
152 FlowLayout(spacing: 6) {
153 ForEach(currentTags, id: \.self) { tag in
154 TagView(tag: tag,
155 isEditing: isEditingTags,
156 onRemove: {
157 self.removeTag(tag)
158 })
159 }
160
161 if isEditingTags && currentTags.count < LocalNoteTags.maximumCount {
162 Button {
163 showAddTagPopover = true
164 } label: {
165 JotleyAccentIconLabel(title: "Add Tag", systemImage: "plus.circle.fill")
166 .noteMetadataPill()
167 }
168 .buttonStyle(.plain)
169 .popover(isPresented: $showAddTagPopover, arrowEdge: .bottom) {
170 AddTagPopoverView(
171 note: note,
172 showPopover: $showAddTagPopover,
173 newTagText: $newTagText,
174 currentTags: $currentTags,
175 isEditingTags: $isEditingTags,
176 saveContext: { self.saveContext() }
177 )
178 .frame(idealWidth: 250)
179 }
180 }
181 }
182 .padding(.horizontal, 6)
183 .padding(.vertical, 4)
184 .onTapGesture {
185 isEditingTags.toggle()
186 }
187 }
188
189 var createdDateText: some View {
190 HStack(spacing: 4) {
191 Image(systemName: "calendar")
192
193 HStack(spacing: 6) {
194 Text(formatDatePart(note.timestampOrNow))
195
196 Rectangle()
197 .fill(Color.gray.opacity(0.3))
198 .frame(width: 1, height: 12)
199
200 Text(formatTimePart(note.timestampOrNow))
201 }
202 }
203 .foregroundColor(Color.gray)
204 .noteMetadataPill()
205 }
206
207 func formatDatePart(_ date: Date) -> String {
208 date.formatted(.dateTime.month(.abbreviated).day().year())
209 }
210
211 func formatTimePart(_ date: Date) -> String {
212 date.formatted(.dateTime.hour().minute())
213 }
214
215 var contentArea: some View {
216 // Note-scoped summary loading binding to prevent cross-note loader bleed.
217 let isGeneratingSummaryBinding = Binding(
218 get: {
219 viewModel.isGeneratingSummary &&
220 viewModel.generatingSummaryNoteID == note.objectID
221 },
222 set: { newValue in
223 if newValue {
224 viewModel.beginSummaryGeneration(for: note.objectID)
225 } else {
226 viewModel.endSummaryGeneration(for: note.objectID)
227 }
228 }
229 )
230
231 return Group {
232 if isEditingTranscript && selectedTab == DetailTab.transcript.rawValue {
233 LocalMacTranscriptEditor(note: note, viewModel: viewModel) {
234 isEditingTranscript = false
235 }
236 .environmentObject(persistenceController)
237 } else {
238 DetailTabPanel(
239 note: note,
240 selectedTab: $selectedTab,
241 isGeneratingSummary: isGeneratingSummaryBinding,
242 summaryGenerationError: $summaryGenerationError,
243 editableSummary: $editableSummary,
244 isSummaryEditorFocused: $isSummaryEditorFocused,
245 summaryInlineEditRequestCounter: $summaryInlineEditRequestCounter,
246 editableTranscript: $editableTranscript,
247 isTranscriptEditorFocused: $isTranscriptEditorFocused,
248 showRawMarkdown: bindingForCurrentTabShowRawMarkdown,
249 viewModel: viewModel,
250 // Note-scoped saved-audio loading flag — drives the transcript spinner without
251 // a persisted placeholder. Read here (where the VM is observed) so the
252 // value flips trigger a re-render down through DetailTabPanel.
253 isTranscriptLoading: viewModel.isTranscriptLoading(for: note),
254 summaryLoadingStartedAt: viewModel.summaryLoadingStartedAt(for: note.objectID),
255 streamingSummaryViewModel: viewModel.streamingSummaryViewModel
256 )
257 .padding(.horizontal, 4)
258 .padding(.top, 4)
259 .frame(maxWidth: .infinity, maxHeight: .infinity)
260 }
261 }
262 }
263
264 // Computed property to select the correct binding for the current tab
265 var bindingForCurrentTabShowRawMarkdown: Binding<Bool> {
266 switch DetailTab(rawValue: selectedTab) {
267 case .summary:
268 return $showRawMarkdownSummary
269 case .transcript:
270 return .constant(false) // Transcript is always in bubble view
271 case .none:
272 return .constant(false)
273 }
274 }
275}