~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
158 lines · 6.3 KB Raw History
import SwiftUI

struct PostRow: View {
    @Environment(AppModel.self) private var model
    @State private var revealedContent = false
    let post: Post
    let openPost: () -> Void
    let openProfile: () -> Void
    let openActor: (String) -> Void
    let openQuotedPost: (String) -> Void
    let openComposer: (String?, String?) -> Void
    var accessibilityIdentifierPrefix = ""
    var moderationContext: ModerationPolicy.Context = .contentList
    var allowsPostNavigation = true
    var contentInsets = EdgeInsets()
    var threadPosition: ThreadPosition?

    private func accessibilityIdentifier(_ identifier: String) -> String {
        "\(accessibilityIdentifierPrefix)\(identifier)"
    }

    var body: some View {
        let _: Void = ViewUpdateMetrics.record("PostRow-\(post.id)")
        let state = model.postViewState(for: post)
        Group {
            if let decision = state.decision {
                let presentation = decision.ui(moderationContext)
                if decision.action == .hide {
                    Text("Post hidden by your content preferences")
                } else if presentation.blur && (!revealedContent || presentation.noOverride) {
                    VStack(alignment: .leading, spacing: 8) {
                        Text("Content warning")
                        if !presentation.noOverride { Button("Show post") { revealedContent = true } }
                    }
                } else {
                    postContent
                }
            } else {
                VStack(alignment: .leading, spacing: 8) {
                    Text(model.contentPreferencesError ?? "Loading content preferences")
                    if model.contentPreferencesError != nil {
                        Button("Retry") { Task { await model.reloadContentPreferences() } }
                        Link("Manage preferences in Bluesky", destination: BlueskyLinks.moderation)
                    }
                }
            }
        }
        .modifier(
            PostNavigationPadding(insets: contentInsets, action: allowsPostNavigation && canOpenPost ? openPost : nil)
        )
        .modifier(
            PostNavigationTarget(
                action: allowsPostNavigation && canOpenPost ? openPost : nil,
                accessibilityID: accessibilityIdentifier("openPost-\(post.id)"),
                label: "Open post by \(post.author.name)"
            )
        )
        .onChange(of: post) { _, _ in resetReveals() }
        .onChange(of: state.decision) { _, _ in resetReveals() }
        .onChange(of: model.contentRevision) { _, _ in resetReveals() }
    }

    private var canOpenPost: Bool {
        guard let decision = model.postViewState(for: post).decision, decision.action != .hide else { return false }
        let presentation = decision.ui(moderationContext)
        return !presentation.blur || (revealedContent && !presentation.noOverride)
    }

    private func resetReveals() {
        revealedContent = false
    }

    private var postContent: some View {
        let state = model.postViewState(for: post)
        let author = model.profile(actorID: post.author.id)?.author ?? post.author
        return PostContentStack(spacing: 12, openPost: allowsPostNavigation ? openPost : nil) {
            PostAuthorHeader(
                author: author,
                postID: post.id,
                date: post.createdAt,
                threadPosition: threadPosition,
                allowsAvatarImage: model.contentDecision(author: post.author)?.ui(.avatar).blur == false,
                accessibilityID: accessibilityIdentifier("profileLink-\(post.author.id)"),
                openProfile: openProfile
            )
            PostText(
                text: post.text,
                facets: post.facets,
                preparedText: model.preparedPostText.renderedText(id: post.id, text: post.text, facets: post.facets),
                accessibilityID: accessibilityIdentifier("postText-\(post.id)"),
                openPost: openPost,
                openActor: openActor,
                openLinkedPost: openQuotedPost
            )

            PostMedia(
                imageURLs: post.imageURLs,
                imageThumbnailURLs: post.imageThumbnailURLs,
                pendingImages: post.pendingImages,
                gifURLs: post.gifURLs,
                inlineGIFVideoURLs: post.inlineGIFVideoURLs,
                videoURLs: post.videoURLs,
                descriptions: post.imageDescriptions,
                decision: state.decision,
                accessibilityID: accessibilityIdentifier(post.id)
            )
            PostLinks(urls: post.links, openPost: openQuotedPost)

            if let quote = post.quote {
                QuoteContextRow(
                    quote: quote, fallbackPost: state.quotedPost, topPadding: 2, openActor: openActor,
                    openLinkedPost: openQuotedPost
                ) {
                    openQuotedPost(quote.postID)
                }
            }

            PostActions(
                post: post,
                accessibilityIdentifierPrefix: accessibilityIdentifierPrefix,
                openPost: allowsPostNavigation ? openPost : nil,
                openComposer: openComposer
            )
        }
    }
}

#if DEBUG
    @MainActor
    private enum PostRowPreviewData {
        static var activePost: Post {
            var post = PreviewContent.post
            post.isLiked = true
            post.isReposted = true
            post.replyCount = 8
            post.createdAt = PreviewContent.date
            post.quote = Post.Quote(postID: "quoted", author: PreviewContent.author, text: "A view worth sharing.")
            return post
        }
    }

    #Preview("Post rows") {
        PreviewHost { _ in
            ScrollView {
                VStack(alignment: .leading, spacing: 24) {
                    PostRow(
                        post: PreviewContent.post, openPost: {}, openProfile: {}, openActor: { _ in },
                        openQuotedPost: { _ in }, openComposer: { _, _ in })
                    PostDivider()
                    PostRow(
                        post: PostRowPreviewData.activePost, openPost: {}, openProfile: {}, openActor: { _ in },
                        openQuotedPost: { _ in }, openComposer: { _, _ in })
                }
                .padding()
            }
        }
    }
#endif