chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
hud.hudkit 0.4.0 Latest 5 packages
0
0
import HUDKit
import SwiftUI
/// Deleting everything HUD keeps for the account in use, as the web's
/// danger dialog: what it keeps, read as it opens (`getAccountData`), the
/// canvases it owns listed, the account's handle typed to confirm, then
/// one call deletes it all and the app goes on as the account used before
/// it, else signed out (`Browser.deleteData`).
struct DeleteDataSheet: View {
/// What goes.
private static let explanation = "Everything HUD keeps for you goes for good: the canvases you own, for everyone in them, your place in others' canvases and the tiles you put there, your invitations, your tabs, the themes you published and your theme choice. You're then signed out everywhere. Your atproto account, and what other apps keep in it, stay."
let browser: Browser
@Environment(\.dismiss) private var dismiss
/// What HUD keeps; nil until read.
@State private var held: AccountData?
/// Why it couldn't be read.
@State private var loadFailed: String?
@State private var typed = ""
@State private var busy = false
@State private var problem: String?
/// What to type to confirm: the account's handle, else its DID.
private var handle: String {
browser.viewer?.handle ?? browser.session?.did ?? ""
}
var body: some View {
NavigationStack {
Group {
if let held {
content(held)
} else if let loadFailed {
ContentUnavailableView {
Label("Couldn't read your HUD data", systemImage: "exclamationmark.triangle")
} description: {
Text(loadFailed)
} actions: {
Button("Try again") { Task { await load() } }
}
} else {
ProgressView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.navigationTitle("Delete your HUD data")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") { dismiss() }
.disabled(busy)
}
}
.interactiveDismissDisabled(busy)
}
.task { await load() }
}
@ViewBuilder
private func content(_ held: AccountData) -> some View {
if held.isEmpty {
ContentUnavailableView("HUD keeps nothing for you", systemImage: "tray")
} else {
Form {
Section {
Text(summary(held))
} footer: {
Text(Self.explanation)
}
if !held.canvases.isEmpty {
Section {
ForEach(held.canvases) { canvas($0) }
} header: {
Text("Your canvases")
} footer: {
if held.canvases.contains(where: { $0.members > 0 }) {
Text("A canvas you share goes for everyone in it.")
}
}
}
if held.shared > 0 || held.invitations > 0 || held.themes > 0 || held.chosenTheme {
Section {
if held.shared > 0 { count("Others' canvases you're in", held.shared) }
if held.invitations > 0 { count("Invitations", held.invitations) }
if held.themes > 0 { count("Themes you published", held.themes) }
if held.chosenTheme {
LabeledContent("Your theme choice") { Text("Chosen") }
}
}
}
Section {
TextField(handle, text: $typed)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.keyboardType(.URL)
.disabled(busy)
.accessibilityLabel("To confirm, type \(handle)")
if busy {
HStack(spacing: Space.s2) {
ProgressView()
Text("Deleting…").foregroundStyle(.secondary)
}
}
if let problem {
Text(problem).font(.footnote).foregroundStyle(.red)
}
Button("Delete all my HUD data", role: .destructive) {
Task { await remove() }
}
.disabled(busy || typed.trimmingCharacters(in: .whitespaces) != handle)
} header: {
Text("To confirm, type \(handle)")
}
}
}
}
/// What goes, counted, in one sentence.
private func summary(_ held: AccountData) -> String {
var parts: [String] = []
if !held.canvases.isEmpty {
parts.append(held.canvases.count == 1 ? "1 canvas you own" : "\(held.canvases.count.formatted()) canvases you own")
}
if held.shared > 0 {
parts.append(held.shared == 1 ? "your place in 1 canvas of others'" : "your place in \(held.shared.formatted()) canvases of others'")
}
if held.invitations > 0 {
parts.append(held.invitations == 1 ? "1 invitation" : "\(held.invitations.formatted()) invitations")
}
if held.themes > 0 {
parts.append(held.themes == 1 ? "1 theme you published" : "\(held.themes.formatted()) themes you published")
}
if held.chosenTheme { parts.append("your theme choice") }
return "This deletes \(parts.formatted(.list(type: .and))), for good."
}
private func canvas(_ canvas: AccountData.Canvas) -> some View {
VStack(alignment: .leading, spacing: Space.s1) {
Text(canvas.name).lineLimit(1)
if canvas.members > 0 {
Text(canvas.members == 1 ? "Shared with 1 other" : "Shared with \(canvas.members.formatted()) others")
.font(.footnote)
.foregroundStyle(.secondary)
}
}
.accessibilityElement(children: .combine)
}
private func count(_ label: String, _ count: Int) -> some View {
LabeledContent(label) { Text(count.formatted()) }
}
private func load() async {
guard let workspace = browser.workspace else { return }
loadFailed = nil
do {
held = try await workspace.client.accountData()
} catch HUDError.signIn {
// Signed out: the session says so.
} catch {
loadFailed = error.localizedDescription
}
}
private func remove() async {
busy = true
problem = nil
do {
try await browser.deleteData()
dismiss()
} catch {
// Nothing is signed out until all of it is gone: the button tries again.
problem = "\(error.localizedDescription) What's deleted stays deleted: try again to carry on."
busy = false
}
}
}