chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.5.0 Latest 5 packages
0
0
import HUDKit
import SwiftUI
/// A canvas's people, as the web's share dialog: its owner, then each
/// member or invitation with their role. Its owner invites by handle, makes
/// a member an editor or a viewer, and takes one off; anyone else may leave.
struct ShareSheet: View {
let canvas: String
let workspace: Workspace
@Environment(\.dismiss) private var dismiss
@State private var actor = ""
@State private var role = "editor"
@State private var busy = false
@State private var problem: String?
@State private var leaving = false
var body: some View {
NavigationStack {
Group {
if let info = workspace.info(canvas) {
list(info)
} else {
ContentUnavailableView("This canvas is gone", systemImage: "rectangle.slash")
}
}
.navigationTitle("Share")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") { dismiss() }
}
}
}
}
private func list(_ info: CanvasInfo) -> some View {
Form {
Section {
ForEach(info.members.sorted { ($0.role == "owner" ? 0 : 1) < ($1.role == "owner" ? 0 : 1) }) { member in
row(member, owned: info.owned)
}
} header: {
Text(info.name)
} footer: {
if info.owned { Text("Editors add and change tiles; viewers only look.") }
}
if info.owned {
Section {
TextField("Handle, like alice.bsky.social", text: $actor)
.textContentType(.username)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.submitLabel(.send)
.onSubmit(invite)
Picker("Role", selection: $role) {
Text("Editor").tag("editor")
Text("Viewer").tag("viewer")
}
Button("Invite", action: invite)
.disabled(busy || actor.trimmingCharacters(in: .whitespaces).isEmpty)
} header: {
Text("Invite")
} footer: {
if let problem { Text(problem).foregroundStyle(.red) }
}
} else {
Section {
Button("Leave canvas", role: .destructive) { leaving = true }
} footer: {
if let problem { Text(problem).foregroundStyle(.red) }
}
.confirmationDialog("Leave “\(info.name)”?", isPresented: $leaving, titleVisibility: .visible) {
Button("Leave canvas", role: .destructive) {
act { try await workspace.leave(canvas); dismiss() }
}
} message: {
Text("It leaves your tabs. Its owner can invite you again.")
}
}
}
.disabled(busy)
}
@ViewBuilder
private func row(_ member: Member, owned: Bool) -> some View {
let name = member.handle.map { "@\($0)" } ?? member.did
let me = member.did == workspace.me?.did
HStack {
VStack(alignment: .leading, spacing: 2) {
Text(me ? "\(name) (you)" : name).lineLimit(1)
if member.pending {
Text("Invited").font(.caption).foregroundStyle(.secondary)
}
}
Spacer()
if owned, member.role != "owner" {
Menu {
Picker("Role", selection: Binding(
get: { member.role },
set: { new in act { try await workspace.setRole(canvas, did: member.did, role: new) } }
)) {
Text("Editor").tag("editor")
Text("Viewer").tag("viewer")
}
Button("Remove", systemImage: "person.badge.minus", role: .destructive) {
act { try await workspace.removeMember(canvas, did: member.did) }
}
} label: {
Text(member.role.capitalized)
}
} else {
Text(member.role.capitalized).foregroundStyle(.secondary)
}
}
.swipeActions {
if owned, member.role != "owner" {
Button("Remove", role: .destructive) {
act { try await workspace.removeMember(canvas, did: member.did) }
}
}
}
}
private func invite() {
let who = actor.trimmingCharacters(in: .whitespaces).trimmingCharacters(in: CharacterSet(charactersIn: "@"))
guard !who.isEmpty else { return }
act {
try await workspace.invite(canvas, actor: who, role: role)
actor = ""
}
}
/// One change at a time; why it failed said under the form.
private func act(_ change: @escaping @MainActor () async throws -> Void) {
busy = true
problem = nil
Task {
do {
try await change()
} catch {
problem = error.localizedDescription
}
busy = false
}
}
}