summaryrefslogtreecommitdiff
path: root/Hutch/Views/Repositories/RepositorySummarySupport.swift
blob: 393e3ff623a18afd03438f0af4b48624c890fbd1 (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
import SwiftUI

enum RepositoryBuildStatus: Sendable {
    case success
    case failed
    case running
    case none
}

struct RepositoryCloneURLs {
    let readOnly: String
    let readWrite: String
}

func repositoryCloneURLs(for repository: RepositorySummary) -> RepositoryCloneURLs {
    let owner = repository.owner.canonicalName
    let name = repository.name

    switch repository.service {
    case .git:
        return RepositoryCloneURLs(
            readOnly: "https://git.sr.ht/\(owner)/\(name)",
            readWrite: "[email protected]:\(owner)/\(name)"
        )
    case .hg:
        return RepositoryCloneURLs(
            readOnly: "https://hg.sr.ht/\(owner)/\(name)",
            readWrite: "ssh://[email protected]/\(owner)/\(name)"
        )
    default:
        return RepositoryCloneURLs(
            readOnly: "https://\(repository.service.rawValue).sr.ht/\(owner)/\(name)",
            readWrite: ""
        )
    }
}

func repositoryVisibilityLabel(_ visibility: Visibility) -> String {
    switch visibility {
    case .publicVisibility:
        return "Public"
    case .unlisted:
        return "Unlisted"
    case .privateVisibility:
        return "Private"
    }
}

func repositoryForgeLabel(_ service: SRHTService) -> String {
    switch service {
    case .git:
        return "GIT"
    case .hg:
        return "HG"
    default:
        return service.rawValue.uppercased()
    }
}

func repositoryPrimaryBranchLabel(for repository: RepositorySummary, hgTipBranch: String? = nil) -> String? {
    switch repository.service {
    case .git:
        return repository.defaultBranchName
    case .hg:
        return hgTipBranch ?? repository.defaultBranchName ?? "tip"
    default:
        return repository.defaultBranchName
    }
}

struct SummaryMetadataRow: View {
    let icon: String
    let title: String
    var subtitle: String? = nil

    var body: some View {
        HStack(alignment: .top, spacing: 10) {
            Image(systemName: icon)
                .foregroundStyle(.secondary)
                .frame(width: 18)

            VStack(alignment: .leading, spacing: 2) {
                Text(title)
                if let subtitle, !subtitle.isEmpty {
                    Text(subtitle)
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                }
            }
        }
    }
}

struct SummaryDetailRow: View {
    let label: String
    let value: String
    var monospace: Bool = false

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            Text(label)
                .font(.caption)
                .foregroundStyle(.secondary)
            Text(value)
                .font(monospace ? .system(.body, design: .monospaced) : .body)
                .textSelection(.enabled)
        }
    }
}

struct RepositoryForgeBadge: View {
    let service: SRHTService

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

    private var color: Color {
        switch service {
        case .git:
            .indigo
        case .hg:
            .cyan
        default:
            .secondary
        }
    }
}