summaryrefslogtreecommitdiff
path: root/Hutch/Views/Repositories/RepositoryDetailView.swift
blob: 2715134a44768fe0dada001fcf1b08706a5324df (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import SwiftUI

struct RepositoryDetailView: View {
    @Environment(\.openURL) private var openURL

    let onRepositoryUpdated: ((RepositorySummary) -> Void)?
    var onDeleted: (() -> Void)?

    @Environment(AppState.self) private var appState
    @Environment(\.dismiss) private var dismiss
    @State private var viewModel: RepositoryDetailViewModel?
    @State private var selectedTab: RepositoryDetailViewModel.Tab = .summary
    @State private var showSettings = false
    @State private var showACLs = false
    @State private var currentRepository: RepositorySummary

    private var canManageRepository: Bool {
        guard let currentUser = appState.currentUser else { return false }
        return normalizedUsername(currentUser.username) == normalizedUsername(currentRepository.owner.canonicalName)
    }

    init(
        repository: RepositorySummary,
        onRepositoryUpdated: ((RepositorySummary) -> Void)? = nil,
        onDeleted: (() -> Void)? = nil
    ) {
        self.onRepositoryUpdated = onRepositoryUpdated
        self.onDeleted = onDeleted
        self._currentRepository = State(initialValue: repository)
    }

    var body: some View {
        if currentRepository.service == .hg {
            HgRepositoryDetailView(repository: currentRepository, onDeleted: onDeleted)
        } else {
            Group {
                if let viewModel {
                    detailContent(viewModel)
                } else {
                    SRHTLoadingStateView(message: "Loading repository…")
                }
            }
            .navigationTitle(currentRepository.name)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItemGroup(placement: .topBarTrailing) {
                    repositoryActionsMenu

                    SRHTShareButton(url: SRHTWebURL.repository(currentRepository), target: .repository) {
                        Image(systemName: "square.and.arrow.up")
                    }
                }
            }
            .sheet(isPresented: $showSettings) {
                RepositorySettingsView(
                    repository: currentRepository,
                    branches: viewModel?.branches ?? [],
                    client: appState.client,
                    onUpdated: { updatedRepository in
                        currentRepository = updatedRepository
                        onRepositoryUpdated?(updatedRepository)
                    },
                    onDeleted: {
                        dismiss()
                        onDeleted?()
                    }
                )
            }
            .sheet(isPresented: $showACLs) {
                NavigationStack {
                    RepositoryACLView(
                        repository: currentRepository,
                        client: appState.client,
                        showsDoneButton: true
                    )
                }
            }
            .task {
                if viewModel == nil {
                    viewModel = RepositoryDetailViewModel(
                        repository: currentRepository,
                        client: appState.client
                    )
                }
            }
        }
    }

    @ViewBuilder
    private func detailContent(_ viewModel: RepositoryDetailViewModel) -> some View {
        VStack(spacing: 0) {
            Picker("Tab", selection: $selectedTab) {
                ForEach(RepositoryDetailViewModel.Tab.allCases, id: \.self) { tab in
                    Text(tab.rawValue).tag(tab)
                }
            }
            .pickerStyle(.segmented)
            .padding(.horizontal)
            .padding(.vertical, 8)

            Divider()

            switch selectedTab {
            case .summary:
                ReadmeView(viewModel: viewModel)
            case .tree:
                FileTreeView(
                    repository: currentRepository,
                    client: appState.client
                )
            case .log:
                CommitLogView(viewModel: viewModel)
            case .refs:
                ReferencesListView(viewModel: viewModel)
            case .artifacts:
                ArtifactsView(viewModel: viewModel)
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
        .srhtErrorBanner(error: Binding(
            get: { viewModel.error },
            set: { viewModel.error = $0 }
        ))
    }

    private func normalizedUsername(_ value: String) -> String {
        let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
        return trimmed.hasPrefix("~") ? String(trimmed.dropFirst()) : trimmed
    }

    private var repositoryActionsMenu: some View {
        Menu {
            if let repositoryURL = SRHTWebURL.repository(currentRepository) {
                Button {
                    openURL(repositoryURL)
                } label: {
                    Label("Open in Browser", systemImage: "safari")
                }

                Button {
                    appState.copyToPasteboard(repositoryURL.absoluteString, label: "repository URL")
                } label: {
                    Label("Copy URL", systemImage: "doc.on.doc")
                }
            }

            if let httpsURL = SRHTWebURL.httpsCloneURL(currentRepository) {
                Button {
                    appState.copyToPasteboard(httpsURL, label: "HTTPS clone URL")
                } label: {
                    Label("Copy HTTPS URL", systemImage: "doc.on.doc")
                }
            }

            Button {
                appState.copyToPasteboard(SRHTWebURL.sshCloneURL(currentRepository), label: "SSH clone URL")
            } label: {
                Label("Copy SSH URL", systemImage: "terminal")
            }

            Button {
                appState.copyToPasteboard(currentRepository.rid, label: "repository RID")
            } label: {
                Label("Copy RID", systemImage: "number")
            }

            if canManageRepository {
                Divider()

                Button {
                    showACLs = true
                } label: {
                    Label("Manage ACLs", systemImage: "person.2")
                }

                Button {
                    showSettings = true
                } label: {
                    Label("Repository Settings", systemImage: "gear")
                }
            }
        } label: {
            Image(systemName: "ellipsis.circle")
        }
        .accessibilityLabel("Repository actions")
    }
}