Files
hapi/ios/Hapi/Features/Chat/SessionShareModel.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

90 lines
2.6 KiB
Swift

import Foundation
import HapiClient
import HapiProtocol
import Observation
/// Endpoint seam so the sheet's state machine is testable without HTTP.
struct SessionShareService {
var load: @Sendable (String) async throws -> SessionShare?
var create: @Sendable (String) async throws -> SessionShareCreateResponse
var revoke: @Sendable (String) async throws -> Void
}
/// One session's public-share lifecycle. The access code is only returned by
/// the create response, so it lives here until this chat closes.
@MainActor @Observable
final class SessionShareModel {
let sessionId: String
let baseURL: URL
private let service: SessionShareService
private(set) var share: SessionShare?
private(set) var accessCode: String?
private(set) var isLoading = false
private(set) var isCreating = false
private(set) var isRevoking = false
private(set) var errorMessage: String?
@ObservationIgnored private var hasLoaded = false
init(sessionId: String, baseURL: URL, service: SessionShareService) {
self.sessionId = sessionId
self.baseURL = baseURL
self.service = service
}
var shareURL: URL? {
share.flatMap {
SessionShareLink.url(baseURL: baseURL, shareToken: $0.shareToken)
}
}
/// The sheet can be dismissed and reopened; do not wipe a code that was
/// shown by an earlier create response.
func loadIfNeeded() async {
guard !hasLoaded else { return }
hasLoaded = true
await load()
}
func load() async {
guard !isLoading else { return }
isLoading = true
errorMessage = nil
defer { isLoading = false }
do {
share = try await service.load(sessionId)
} catch {
errorMessage = error.localizedDescription
}
}
func create() async {
guard !isCreating else { return }
isCreating = true
errorMessage = nil
defer { isCreating = false }
do {
let created = try await service.create(sessionId)
share = created.share
accessCode = created.accessCode
hasLoaded = true
} catch {
errorMessage = error.localizedDescription
}
}
func revoke() async {
guard let share, !isRevoking else { return }
isRevoking = true
errorMessage = nil
defer { isRevoking = false }
do {
try await service.revoke(share.id)
self.share = nil
accessCode = nil
} catch {
errorMessage = error.localizedDescription
}
}
}