import SwiftUI import DevPlaceSwiftSDK struct CommentView: View { let comment: Comment var indentationLevel: Int = 0 var linksToProfile: Bool = true var maxAttachments: Int? = nil var onSingleTap: (() -> Void)? = nil var onDoubleTap: (() -> Void)? = nil var onReact: ((String) -> Void)? = nil var onDelete: (() -> Void)? = nil var onSubmitEdit: ((String) -> Void)? = nil var onReply: (() -> Void)? = nil var isReplying: Bool = false var replyText: Binding = .constant("") var replyAttachments: [UploadResponse] = [] var onReplyAttachmentsChange: (([UploadResponse]) -> Void)? = nil var onSubmitReply: ((String) -> Void)? = nil var onCancelReply: (() -> Void)? = nil var mentionParticipants: [UserSearch.Result] = [] var editingCommentId: Binding = .constant(nil) var pendingEditQuote: String? = nil var onConsumeEditQuote: (() -> Void)? = nil var showsTopDivider: Bool = false var previousIndentationLevel: Int? = nil @State private var isConfirmingDelete = false @State private var isEditing = false @State private var editedText = "" @State private var contentHeight: CGFloat = 0 private let maxIndentationLevel = 3 private let indentWidth: CGFloat = 16 private let lineOpacity: Double = 0.3 private var effectiveLevel: Int { min(indentationLevel, maxIndentationLevel) } private var dividerLeadingPadding: CGFloat { let previousEffectiveLevel = previousIndentationLevel.map { min($0, maxIndentationLevel) } ?? effectiveLevel let deepestLevel = max(effectiveLevel, previousEffectiveLevel) return indentWidth + CGFloat(deepestLevel - effectiveLevel) * indentWidth } private var displayedAttachments: [Attachment] { guard let maxAttachments else { return comment.attachments } return Array(comment.attachments.prefix(maxAttachments)) } var body: some View { HStack(alignment: .top, spacing: 0) { if effectiveLevel > 0 { ForEach(1...effectiveLevel, id: \.self) { level in guideLine(dashed: level == maxIndentationLevel) .frame(width: indentWidth) .frame(maxHeight: .infinity) } } VStack(alignment: .leading, spacing: 0) { if showsTopDivider { Divider() .padding(.leading, dividerLeadingPadding) .padding(.trailing, indentWidth) } commentBody() } } .foregroundStyle(Color.FG_1) } @ViewBuilder private func commentBody() -> some View { VStack(alignment: .leading, spacing: 8) { PostHeaderView(author: comment.author, date: comment.data.createdAt, linksToProfile: linksToProfile) if isEditing { editor() .transition(.opacity) } else { PostContentView(topic: nil, title: nil, content: comment.data.content) .onGeometryChange(for: CGFloat.self) { proxy in proxy.size.height } action: { newHeight in contentHeight = newHeight } .transition(.opacity) } ForEach(displayedAttachments, id: \.id) { attachment in AttachmentViewer(attachment: attachment) } HStack(spacing: 12) { VoteView( targetType: .comment, targetId: comment.data.id, count: comment.voteCount, currentVote: comment.myVote, ) ReactionsBar(reactions: comment.reactions, onReact: onReact ?? { _ in }) Spacer(minLength: 0) if !isEditing { HStack(spacing: 24) { if let onReply, !isReplying { replyButton(onReply: onReply) } ReportButton(targetType: .comment, targetId: comment.data.id, authorId: comment.data.userId) if canEdit { editButton() } if onDelete != nil { deleteButton() } } .transition(.opacity) } } if isReplying && !isEditing { CommentEditorView( text: replyText, initialLineCount: 3, focusOnAppear: true, attachments: replyAttachments, onAttachmentsChange: onReplyAttachmentsChange, mentionParticipants: mentionParticipants, onCancel: { onCancelReply?() }, onSubmit: { content in onSubmitReply?(content) }, ) .transition(.opacity) } } .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal) .padding(.vertical, 8) .contentShape(Rectangle()) .onTapGesture(count: 2) { handleDoubleTap() } .onTapGesture { if !isEditing && !isReplying { onSingleTap?() } } } private static let editModeAnimation: Animation = .smooth(duration: 0.28) private func handleDoubleTap() { guard !isEditing, !isReplying else { return } if canEdit { beginEditing() } else { onDoubleTap?() } } @ViewBuilder private func editor() -> some View { CommentEditorView( text: $editedText, placeholder: "Edit your comment…", initialHeight: contentHeight > 0 ? contentHeight : nil, mentionParticipants: mentionParticipants, onCancel: { endEditing() }, onSubmit: { content in onSubmitEdit?(content) endEditing() }, ) .onChange(of: pendingEditQuote) { _, quote in guard let quote else { return } editedText += quote onConsumeEditQuote?() } } @ViewBuilder private func replyButton(onReply: @escaping () -> Void) -> some View { Button { onReply() } label: { Label("Reply to comment", systemImage: "arrowshape.turn.up.left") .labelStyle(.iconOnly) } .buttonStyle(.plain) .foregroundStyle(Color.FG_1) } @ViewBuilder private func editButton() -> some View { Button { beginEditing() } label: { Label("Edit comment", systemImage: "pencil") .labelStyle(.iconOnly) } .buttonStyle(.plain) .foregroundStyle(Color.FG_1) } private var canEdit: Bool { onSubmitEdit != nil } private func beginEditing() { editedText = comment.data.content withAnimation(Self.editModeAnimation) { isEditing = true editingCommentId.wrappedValue = comment.id } } private func endEditing() { withAnimation(Self.editModeAnimation) { isEditing = false if editingCommentId.wrappedValue == comment.id { editingCommentId.wrappedValue = nil } } } @ViewBuilder private func deleteButton() -> some View { Button { isConfirmingDelete = true } label: { Label("Delete comment", systemImage: "trash") .labelStyle(.iconOnly) } .buttonStyle(.plain) .foregroundStyle(Color.FG_1) .confirmationDialog( "Delete this comment?", isPresented: $isConfirmingDelete, titleVisibility: .visible, ) { Button("Delete", role: .destructive) { onDelete?() } Button("Cancel", role: .cancel) {} } } @ViewBuilder private func guideLine(dashed: Bool) -> some View { VerticalLine() .stroke( Color.FG_2.opacity(lineOpacity), style: StrokeStyle(lineWidth: 1, dash: dashed ? [3, 3] : []), ) } } private struct VerticalLine: Shape { func path(in rect: CGRect) -> Path { var path = Path() path.move(to: CGPoint(x: rect.maxX, y: rect.minY)) path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) return path } } #Preview { NavigationStack { ScrollView { VStack(spacing: 0) { let comments = [Comment].mock CommentView(comment: comments[0], indentationLevel: 0) CommentView(comment: comments[1], indentationLevel: 1) CommentView(comment: comments[0], indentationLevel: 2) CommentView(comment: comments[1], indentationLevel: 3) CommentView(comment: comments[0], indentationLevel: 4) } .frame(maxWidth: .infinity) } .background { Color.BG_1.ignoresSafeArea() } .environment(\.api, .mock) } }