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

geesawra.industries/old-fella

Old people's social media app

0
1
main
65 lines · 2.5 KB Raw History
import SwiftUI

struct ProfileConnectionsView: View {
    @Environment(AppModel.self) private var model
    @State private var accounts: [Post.Author] = []
    @State private var cursor: String?
    @State private var usedCursors: Set<String> = []
    @State private var requestPending = true
    @State private var loadFailed = false

    let actorID: String
    let kind: ProfileConnectionKind
    let openProfile: (String) -> Void

    var body: some View {
        let visibleAccounts = accounts.filter(model.isAccountSummaryVisible)
        List {
            PeopleListRows(accounts: visibleAccounts, rowIdentifierPrefix: "profileConnection", openProfile: openProfile)

            Group {
                if requestPending {
                    ProgressView("Loading accounts")
                } else if loadFailed {
                    ContentUnavailableView {
                        Label("Could not load accounts", systemImage: "exclamationmark.triangle")
                    } actions: {
                        Button("Retry") { requestPending = true }
                            .buttonStyle(.bordered)
                    }
                } else if cursor != nil {
                    Button("Load more accounts") { requestPending = true }
                        .buttonStyle(.bordered)
                } else if visibleAccounts.isEmpty {
                    ContentUnavailableView(
                        kind == .followers ? "No followers to show" : "No accounts to show",
                        systemImage: "person.2")
                }
            }
            .frame(maxWidth: .infinity)
            .listRowSeparator(.hidden)
        }
        .listStyle(.plain)
        .appNavigationTitle(kind.title)
        .accessibilityIdentifier("profileConnectionsList")
        .task(id: requestPending) {
            if requestPending { await loadPage() }
        }
    }

    private func loadPage() async {
        defer { if !Task.isCancelled { requestPending = false } }
        do {
            let page = try await model.loadProfileConnections(actorID: actorID, kind: kind, cursor: cursor)
            var seen = Set(accounts.map(\.id))
            accounts.append(contentsOf: page.accounts.filter { seen.insert($0.id).inserted })
            if let cursor { usedCursors.insert(cursor) }
            cursor = page.cursor.flatMap { $0.isEmpty || usedCursors.contains($0) ? nil : $0 }
            loadFailed = false
        } catch is CancellationError {
            return
        } catch {
            if !Task.isCancelled { loadFailed = true }
        }
    }
}