import SwiftUI import AppKit extension NoteDetailPlaceholderView { var headerContent: some View { Group { if MacScreenshotFixture.isActive { Text(note.titleOrUntitled) .font(.system(size: 24, weight: .semibold)) .foregroundStyle(Color.themeTextPrimary) .lineLimit(2) .padding(.horizontal, 2) } else { editableTitleField } } .frame(maxWidth: .infinity, alignment: .leading) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("noteDetailTitle") } private var editableTitleField: some View { ThemedTextField( text: $manualTitle, placeholder: "Enter a title...", font: NSFont.systemFont(ofSize: 24, weight: .semibold), textColor: NSColor(Color.themeTextPrimary), onCommit: { let trimmed = manualTitle.trimmingCharacters(in: .whitespacesAndNewlines) if !trimmed.isEmpty && note.title != trimmed { note.title = trimmed do { try persistenceController.save() } catch { viewModel.report(error) } } }, maxLines: 2 ) .onAppear { manualTitle = note.titleOrUntitled // Load tags directly, they are already formatted and de-duplicated in CoreData currentTags = note.tagsAsString?.split(separator: ",").map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }.sorted() ?? [] currentTags.sort() // When appearing, ensure we're showing original transcript if this note is being recorded let vm = viewModel if vm.recorder.isRecording && vm.newNoteBeingRecorded == note { // REMOVED preferCleanedTranscript = false } } .onChange(of: note.title) { _, _ in manualTitle = note.titleOrUntitled } .onChange(of: note.objectID) { _, _ in manualTitle = note.titleOrUntitled editableSummary = note.summary ?? "" editableTranscript = note.transcription ?? "" // Load tags directly, they are already formatted and de-duplicated in CoreData currentTags = note.tagsAsString?.split(separator: ",").map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }.sorted() ?? [] currentTags.sort() // Don't reset preferCleanedTranscript, since we want it to be sticky } .onChange(of: manualTitle) { _, newText in let trimmed = newText.trimmingCharacters(in: .whitespacesAndNewlines) if !trimmed.isEmpty && note.title != trimmed { note.title = trimmed do { try persistenceController.save() } catch { viewModel.report(error) } } } .onChange(of: note.tagsAsString) { _, newTags in // Load tags directly, they are already formatted and de-duplicated in CoreData currentTags = newTags?.split(separator: ",").map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }.sorted() ?? [] } } var dateAndTagsContent: some View { VStack(alignment: .leading, spacing: 6) { // Top row: date + folder + collapsed tag summary HStack(alignment: .center, spacing: 12) { createdDateText folderChip if showTags { collapsedTagHeader } Spacer() } // Expanded tag flow (shown below the top row when expanded) if showTags && isTagsExpanded { expandedTagsContent } } .padding(.bottom, 6) } /// Compact clickable tag summary: "🏷 N tags >" or "+ Add Tag" @ViewBuilder private var collapsedTagHeader: some View { if currentTags.isEmpty { Button { showAddTagPopover = true } label: { JotleyAccentIconLabel(title: "Add Tag", systemImage: "plus.circle.fill") .noteMetadataPill() } .buttonStyle(.plain) .accessibilityIdentifier("noteDetailAddTagButton") .popover(isPresented: $showAddTagPopover, arrowEdge: .bottom) { AddTagPopoverView( note: note, showPopover: $showAddTagPopover, newTagText: $newTagText, currentTags: $currentTags, isEditingTags: $isEditingTags, saveContext: { self.saveContext() } ) .frame(idealWidth: 250) } } else { Button { withAnimation(.easeInOut(duration: 0.2)) { isTagsExpanded.toggle() } if isTagsExpanded { isEditingTags = true } } label: { HStack(spacing: 4) { Image(systemName: "tag") .foregroundStyle(Color.themePrimaryAccent) Text("\(currentTags.count) \(currentTags.count == 1 ? "tag" : "tags")") .foregroundStyle(.primary) Image(systemName: "chevron.right") .font(.system(size: 9, weight: .semibold)) .rotationEffect(.degrees(isTagsExpanded ? 90 : 0)) } .foregroundStyle(.secondary) .noteMetadataPill() } .buttonStyle(.plain) .accessibilityIdentifier("noteDetailTagCount") } } /// Full tag flow layout with edit/add controls. private var expandedTagsContent: some View { FlowLayout(spacing: 6) { ForEach(currentTags, id: \.self) { tag in TagView(tag: tag, isEditing: isEditingTags, onRemove: { self.removeTag(tag) }) } if isEditingTags && currentTags.count < LocalNoteTags.maximumCount { Button { showAddTagPopover = true } label: { JotleyAccentIconLabel(title: "Add Tag", systemImage: "plus.circle.fill") .noteMetadataPill() } .buttonStyle(.plain) .popover(isPresented: $showAddTagPopover, arrowEdge: .bottom) { AddTagPopoverView( note: note, showPopover: $showAddTagPopover, newTagText: $newTagText, currentTags: $currentTags, isEditingTags: $isEditingTags, saveContext: { self.saveContext() } ) .frame(idealWidth: 250) } } } .padding(.horizontal, 6) .padding(.vertical, 4) .onTapGesture { isEditingTags.toggle() } } var createdDateText: some View { HStack(spacing: 4) { Image(systemName: "calendar") HStack(spacing: 6) { Text(formatDatePart(note.timestampOrNow)) Rectangle() .fill(Color.gray.opacity(0.3)) .frame(width: 1, height: 12) Text(formatTimePart(note.timestampOrNow)) } } .foregroundColor(Color.gray) .noteMetadataPill() } func formatDatePart(_ date: Date) -> String { date.formatted(.dateTime.month(.abbreviated).day().year()) } func formatTimePart(_ date: Date) -> String { date.formatted(.dateTime.hour().minute()) } var contentArea: some View { // Note-scoped summary loading binding to prevent cross-note loader bleed. let isGeneratingSummaryBinding = Binding( get: { viewModel.isGeneratingSummary && viewModel.generatingSummaryNoteID == note.objectID }, set: { newValue in if newValue { viewModel.beginSummaryGeneration(for: note.objectID) } else { viewModel.endSummaryGeneration(for: note.objectID) } } ) return Group { if isEditingTranscript && selectedTab == DetailTab.transcript.rawValue { LocalMacTranscriptEditor(note: note, viewModel: viewModel) { isEditingTranscript = false } .environmentObject(persistenceController) } else { DetailTabPanel( note: note, selectedTab: $selectedTab, isGeneratingSummary: isGeneratingSummaryBinding, summaryGenerationError: $summaryGenerationError, editableSummary: $editableSummary, isSummaryEditorFocused: $isSummaryEditorFocused, summaryInlineEditRequestCounter: $summaryInlineEditRequestCounter, editableTranscript: $editableTranscript, isTranscriptEditorFocused: $isTranscriptEditorFocused, showRawMarkdown: bindingForCurrentTabShowRawMarkdown, viewModel: viewModel, // Note-scoped saved-audio loading flag — drives the transcript spinner without // a persisted placeholder. Read here (where the VM is observed) so the // value flips trigger a re-render down through DetailTabPanel. isTranscriptLoading: viewModel.isTranscriptLoading(for: note), summaryLoadingStartedAt: viewModel.summaryLoadingStartedAt(for: note.objectID), streamingSummaryViewModel: viewModel.streamingSummaryViewModel ) .padding(.horizontal, 4) .padding(.top, 4) .frame(maxWidth: .infinity, maxHeight: .infinity) } } } // Computed property to select the correct binding for the current tab var bindingForCurrentTabShowRawMarkdown: Binding { switch DetailTab(rawValue: selectedTab) { case .summary: return $showRawMarkdownSummary case .transcript: return .constant(false) // Transcript is always in bubble view case .none: return .constant(false) } } }