import SwiftUI /// Server-side git grep over a repository's files. struct GrepView: View { @State private var model: GrepViewModel @State private var query = "" init(client: GitbayClient, repo: String) { _model = State(initialValue: GrepViewModel(client: client, repoPath: repo)) } var body: some View { List { ForEach(model.byFile, id: \.file) { group in Section { ForEach(group.matches) { match in NavigationLink(value: RepoRoute.file( repo: model.repoPath, path: match.path, ref: nil )) { HStack(alignment: .firstTextBaseline, spacing: 8) { Text(String(match.line)) .font(.gbMono(.caption)) .foregroundStyle(.tertiary) .frame(minWidth: 32, alignment: .trailing) Text(match.text.trimmingCharacters(in: .whitespaces)) .font(.gbMono(.caption)) .lineLimit(2) } } } } header: { Text(group.file) .font(.gbMono(.caption)) .textCase(nil) } } } .overlay { if let state = model.state { LoadStateOverlay(state: state) } else { ContentUnavailableView { Label("Search in files", systemImage: "text.magnifyingglass") } description: { Text("Literal, case-insensitive, over the default branch — git grep, server-side.") } } } .searchable(text: $query, prompt: "Search file contents") .onSubmit(of: .search) { Task { await model.search(query) } } .navigationTitle("Grep") .navigationBarTitleDisplayMode(.inline) } }