summaryrefslogtreecommitdiff
path: root/Hutch/Views/Repositories/RepositoryRowView.swift
blob: 814ea76beb10db39ae5e7eae90f253bbb11cda6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import SwiftUI
import UIKit

struct RepositoryRowView: View {
    let repository: RepositorySummary
    let buildStatus: RepositoryBuildStatus

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            HStack(alignment: .firstTextBaseline) {
                Text(repository.name)
                    .font(.headline)

                Spacer()

                if repository.service == .hg {
                    Text("HG")
                        .font(.caption2.weight(.medium))
                        .padding(.horizontal, 6)
                        .padding(.vertical, 2)
                        .background(Color.cyan.opacity(0.15), in: Capsule())
                        .foregroundStyle(.cyan)
                }

                if buildStatus != .none {
                    RepositoryBuildStatusIndicator(status: buildStatus)
                }
                VisibilityBadge(visibility: repository.visibility)
            }

            Text(repository.owner.canonicalName)
                .font(.subheadline)
                .foregroundStyle(.secondary)

            if let description = repository.description,
               !description.isEmpty {
                Text(description)
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
                    .lineLimit(2)
            }

            HStack(spacing: 12) {
                if let defaultBranchName = repository.defaultBranchName {
                    Label(defaultBranchName, systemImage: "arrow.triangle.branch")
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }

                Spacer()

                Text(repository.updated.relativeDescription)
                    .font(.caption)
                    .foregroundStyle(.tertiary)
            }
        }
        .padding(.vertical, 2)
        .contextMenu {
            Button {
                UIPasteboard.general.string = httpsCloneURL(for: repository)
            } label: {
                Label("Copy HTTPS URL", systemImage: "doc.on.doc")
            }

            Button {
                UIPasteboard.general.string = sshCloneURL(for: repository)
            } label: {
                Label("Copy SSH URL", systemImage: "terminal")
            }
        }
    }
}

private func httpsCloneURL(for repository: RepositorySummary) -> String {
    let host = "\(repository.service.rawValue).sr.ht"
    let owner = repository.owner.canonicalName
    return "https://\(host)/\(owner)/\(repository.name)"
}

private func sshCloneURL(for repository: RepositorySummary) -> String {
    let host = "\(repository.service.rawValue).sr.ht"
    let user = repository.service == .hg ? "hg" : "git"
    let owner = repository.owner.canonicalName
    return "\(user)@\(host):\(owner)/\(repository.name)"
}

private struct RepositoryBuildStatusIndicator: View {
    let status: RepositoryBuildStatus

    var body: some View {
        Circle()
            .fill(color)
            .frame(width: 8, height: 8)
            .overlay {
                Circle()
                    .strokeBorder(.primary.opacity(0.08))
            }
            .accessibilityLabel(accessibilityLabel)
    }

    private var color: Color {
        switch status {
        case .success:
            .green
        case .failed:
            .red
        case .running:
            .orange
        case .none:
            .clear
        }
    }

    private var accessibilityLabel: String {
        switch status {
        case .success:
            "Latest build succeeded"
        case .failed:
            "Latest build failed"
        case .running:
            "Latest build is running"
        case .none:
            "No recent builds"
        }
    }
}

// MARK: - VisibilityBadge

struct VisibilityBadge: View {
    let visibility: Visibility

    var body: some View {
        Text(label)
            .font(.caption2.weight(.medium))
            .padding(.horizontal, 6)
            .padding(.vertical, 2)
            .background(color.opacity(0.15), in: Capsule())
            .foregroundStyle(color)
    }

    private var label: String {
        switch visibility {
        case .public:   "PUBLIC"
        case .unlisted: "UNLISTED"
        case .private:  "PRIVATE"
        }
    }

    private var color: Color {
        switch visibility {
        case .public:   .green
        case .unlisted: .orange
        case .private:  .red
        }
    }
}