import AVFoundation import FoundationModels import PhotosUI import SwiftUI import UIKit import UniformTypeIdentifiers extension ComposeView { var visibleMentionAccounts: [Post.Author] { mentionResults.filter { model.isAccountSummaryVisible($0) } } func mentionDropdown(editorFrame: CGRect, viewport: CGSize) -> some View { let rowCount = max(1, visibleMentionAccounts.count) let contentHeight = loadedMention == activeMention && mentionError == nil ? CGFloat(rowCount) * mentionRowHeight : mentionRowHeight * (mentionError == nil ? 1 : 2) return ComposerSuggestionDropdown( caret: caretRect.offsetBy(dx: editorFrame.minX, dy: editorFrame.minY), viewport: viewport, contentHeight: contentHeight ) { mentionSuggestions } } @ViewBuilder var mentionSuggestions: some View { if let mention = activeMention { VStack(alignment: .leading, spacing: 0) { if loadedMention != mention { ProgressView("Looking up accounts") .frame(maxWidth: .infinity, minHeight: mentionRowHeight) } else if let mentionError { VStack(alignment: .leading, spacing: 8) { Text(mentionError).appFont(.footnote).foregroundStyle(.secondary) Button("Retry") { mentionRetry += 1 } } .padding(12) } else if visibleMentionAccounts.isEmpty { Text("No accounts found.") .appFont(.subheadline) .foregroundStyle(.secondary) .frame(maxWidth: .infinity, minHeight: mentionRowHeight) } else { ForEach(visibleMentionAccounts, id: \.id) { author in Button { guard activeMention == mention, ComposerMention.active(in: text, selection: textSelection) == mention, let replacement = mention.inserting(handle: author.handle, in: text) else { return } text = replacement.text textSelection = replacement.selection activeMention = nil textIsFocused = true } label: { HStack(spacing: 10) { AuthorAvatar(author: author, size: 32) VStack(alignment: .leading, spacing: 2) { Text(displayPreferences.name(author)).foregroundStyle(.primary) if !displayPreferences.handlesOnly { Text("@\(author.handle)").appFont(.subheadline).foregroundStyle(.secondary) } } .lineLimit(1) Spacer(minLength: 0) } .padding(.horizontal, 12) .frame(height: mentionRowHeight) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityLabel( displayPreferences.handlesOnly ? "Mention @\(author.handle)" : "Mention \(author.name), @\(author.handle)" ) .accessibilityIdentifier("mentionSuggestion-\(author.id)") } } } } } func loadMentionSuggestions() async { mentionResults = [] loadedMention = nil mentionError = nil guard let mention = activeMention, textIsFocused, !model.isPublishing else { return } do { try await Task.sleep(for: .milliseconds(300)) let accounts = try await model.mentionSuggestions(query: mention.query) guard !Task.isCancelled, activeMention == mention else { return } mentionResults = accounts loadedMention = mention } catch { guard !Task.isCancelled, activeMention == mention else { return } if NetworkFailure.isCancellation(error) { activeMention = nil return } mentionError = NetworkFailure.isConnectionFailure(error) ? NetworkFailure.message(for: error) : "Account suggestions are unavailable. Try again." loadedMention = mention } } }