Files
hapi/ios/Hapi/Features/Chat/SessionShareSheet.swift
30f775cb84 feat(ios): preview HTML files and share sessions
HTML preview:
- HTML/HTM files get a Source/Preview toggle in the file viewer. Preview
  uses a WKWebView with JavaScript disabled and a non-persistent data
  store, matching the web's inline "preview sandbox · scripts disabled".
- The mobile viewport meta is inserted only when the document lacks one
  (web prepareHtmlDocument), and tapped links leave for Safari instead of
  navigating the preview.

Session sharing:
- New share endpoints client (create/read/revoke) and wire models. The
  share link points at the hub's web /shared-session/<token> guest page;
  the six-digit access code is kept from the create response because the
  hub never returns it again.
- A Share Session sheet creates/regenerates/revokes the link, copies the
  link or code, and offers the native share sheet. Reopening the sheet
  keeps a code created earlier in the same chat.

Adds endpoint, URL-building, model, and HTML-document tests.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-12-01 09:29:55 +08:00

177 lines
5.9 KiB
Swift

import HapiProtocol
import SwiftUI
import UIKit
/// Public-share sheet: create a link + one-time access code, copy/share it,
/// regenerate (rotates both), or revoke. The code is never recoverable, so
/// after a reload only the link is shown.
struct SessionShareSheet: View {
@Environment(\.dismiss) private var dismiss
let model: SessionShareModel
@State private var copiedLink = false
@State private var copiedCode = false
@State private var confirmRegenerate = false
@State private var confirmRevoke = false
var body: some View {
NavigationStack {
Form {
if let error = model.errorMessage {
Section {
Text(error)
.foregroundStyle(.red)
Button("Retry") {
Task { await model.load() }
}
.disabled(model.isLoading)
}
}
if let share = model.share {
activeShare(share)
} else if model.isLoading || model.isCreating {
Section {
HStack(spacing: 8) {
ProgressView()
Text("Creating…")
.foregroundStyle(.secondary)
}
}
} else {
createShare
}
}
.navigationTitle("Share Session")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") { dismiss() }
}
}
.task { await model.loadIfNeeded() }
.alert("Regenerate share?", isPresented: $confirmRegenerate) {
Button("Regenerate", role: .destructive) {
Task { await model.create() }
}
Button("Cancel", role: .cancel) {}
} message: {
Text("Regenerating creates a new link and invalidates the old link and code.")
}
.alert("Revoke share?", isPresented: $confirmRevoke) {
Button("Revoke", role: .destructive) {
Task { await model.revoke() }
}
Button("Cancel", role: .cancel) {}
} message: {
Text("This permanently disables the current share link.")
}
}
.presentationDetents([.medium, .large])
}
private var createShare: some View {
Section {
Text("Anyone with the link and access code can view this conversation. Shares do not expire.")
.font(.footnote)
.foregroundStyle(.secondary)
Button {
Task { await model.create() }
} label: {
Label("Create share link", systemImage: "link.badge.plus")
}
.disabled(model.isCreating)
}
}
@ViewBuilder
private func activeShare(_ share: SessionShare) -> some View {
Section {
Label {
HStack {
Text("Active share")
Spacer()
Text("No expiry")
.foregroundStyle(.secondary)
}
} icon: {
Image(systemName: "checkmark.circle.fill")
.foregroundStyle(.green)
}
}
Section("Share link") {
if let url = model.shareURL {
Text(url.absoluteString)
.font(.footnote)
.textSelection(.enabled)
Button {
copy(url.absoluteString, code: false)
} label: {
Label(copiedLink ? "Copied" : "Copy link", systemImage: "doc.on.doc")
}
ShareLink(item: url) {
Label("Share…", systemImage: "square.and.arrow.up")
}
} else {
Text("This hub address cannot form a share link.")
.foregroundStyle(.secondary)
}
}
Section("Access code") {
if let code = model.accessCode {
Text(code)
.font(.system(.title2, design: .monospaced))
.tracking(4)
.textSelection(.enabled)
Button {
copy(code, code: true)
} label: {
Label(copiedCode ? "Copied" : "Copy access code", systemImage: "doc.on.doc")
}
} else {
Text("The access code is only shown when a share is created or regenerated.")
.font(.footnote)
.foregroundStyle(.secondary)
}
}
Section {
Text("Created \(createdText(share))")
.font(.caption)
.foregroundStyle(.secondary)
Button("Regenerate") {
confirmRegenerate = true
}
.disabled(model.isCreating || model.isRevoking)
Button("Revoke share", role: .destructive) {
confirmRevoke = true
}
.disabled(model.isCreating || model.isRevoking)
}
}
private func createdText(_ share: SessionShare) -> String {
Date(timeIntervalSince1970: Double(share.createdAt) / 1000)
.formatted(date: .abbreviated, time: .shortened)
}
private func copy(_ value: String, code: Bool) {
UIPasteboard.general.string = value
if code {
copiedCode = true
} else {
copiedLink = true
}
Task {
try? await Task.sleep(for: .seconds(1.8))
if code {
copiedCode = false
} else {
copiedLink = false
}
}
}
}