~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
162 lines · 4.3 KB Raw History
import HUDKit
import SwiftUI

/// The account and how the app is drawn, a sheet over the workspace from
/// the account button beside Home: who is signed in (their repository on a
/// tap) and the theme the workspace is drawn in; the accounts signed in to
/// this server, each a tap from being the one in use, another added by
/// signing in, and signing out of the one in use, the app going on as the
/// one used before it, else to signing in; and deleting what HUD keeps for
/// the one in use (`DeleteDataSheet`). In Debug builds, the gallery.
struct SettingsScreen: View {
	let browser: Browser
	@State private var adding = false
	@State private var deleting = false
	#if DEBUG
	@State private var showingGallery = false
	#endif

	var body: some View {
		Form {
			Section {
				if let session = browser.session {
					Button {
						browser.launch(Call("describe_repo", ["actor": .string(session.did)]))
					} label: {
						Label {
							Text(who(session)).foregroundStyle(.primary)
						} icon: {
							Image(systemName: "person.crop.circle.fill")
						}
					}
					.accessibilityHint("Opens your repository")
				}
				if let workspace = browser.workspace {
					theme(workspace)
				}
			}

			Section {
				ForEach(browser.accounts) { account in
					row(account)
				}
				Button {
					browser.signInError = nil
					adding = true
				} label: {
					Label("Add account", systemImage: "person.crop.circle.badge.plus")
				}
				Button("Sign out", role: .destructive) {
					Task { await browser.signOut() }
				}
			} header: {
				Text("Accounts")
			} footer: {
				if let next = browser.accounts.dropFirst().first {
					Text("Signing out goes on as \(name(next)).")
				}
			}

			if browser.workspace != nil {
				Section {
					Button("Delete your HUD data", role: .destructive) { deleting = true }
				}
			}

			#if DEBUG
			development
			#endif
		}
		.navigationTitle("Settings")
		.navigationBarTitleDisplayMode(.inline)
		.task(id: browser.session?.did) { await browser.identify() }
		.sheet(isPresented: $adding) {
			NavigationStack {
				SignInScreen(browser: browser, why: "Add another account: switch between them here.")
					.navigationTitle("Add account")
					.navigationBarTitleDisplayMode(.inline)
					.toolbar {
						ToolbarItem(placement: .cancellationAction) {
							Button("Cancel") { adding = false }
						}
					}
			}
		}
		.sheet(isPresented: $deleting) {
			DeleteDataSheet(browser: browser)
		}
		#if DEBUG
		.fullScreenCover(isPresented: $showingGallery) {
			NavigationStack {
				GalleryScreen(server: browser.serverURL, done: { showingGallery = false })
			}
		}
		#endif
	}

	/// A kept account: the one in use checked, another a tap from being it.
	private func row(_ account: KeptAccount) -> some View {
		let on = account.did == browser.session?.did
		return Button {
			if !on { Task { await browser.use(account.did) } }
		} label: {
			HStack {
				Label {
					Text(name(account)).foregroundStyle(.primary)
				} icon: {
					Image(systemName: "person.crop.circle")
				}
				Spacer(minLength: 0)
				if on {
					Image(systemName: "checkmark")
						.foregroundStyle(.tint)
						.accessibilityHidden(true)
				}
			}
		}
		.accessibilityAddTraits(on ? .isSelected : [])
		.accessibilityHint(on ? "" : "Switches to this account")
	}

	/// The theme the workspace is drawn in, by name: chosen on a screen of
	/// its own (`ThemeChooser`).
	private func theme(_ workspace: Workspace) -> some View {
		NavigationLink {
			ThemeChooser(workspace: workspace)
		} label: {
			LabeledContent {
				Text(workspace.theme?.name ?? "System")
			} label: {
				Label("Theme", systemImage: "paintpalette")
			}
		}
	}

	/// The account in use, by handle once a result has named it.
	private func who(_ session: AccountSession) -> String {
		if let handle = browser.viewer?.handle { return "@\(handle)" }
		return browser.accounts.first { $0.did == session.did }.map(name) ?? session.did
	}

	private func name(_ account: KeptAccount) -> String {
		account.handle.map { "@\($0)" } ?? account.did
	}

	#if DEBUG
	// Debug builds alone: the gallery, every drawing as a tile, for design review.

	private var development: some View {
		Section {
			Button {
				showingGallery = true
			} label: {
				Label("Gallery", systemImage: "rectangle.split.3x1")
			}
		} header: {
			Text("Development")
		} footer: {
			Text("Every drawing as a workspace tile, from `just gallery-data`.")
		}
	}
	#endif
}