Cotton code
179 lines · 6.5 KB · SwiftRaw
// A session's conversation on paper — the transcript the project page draws// its conversation's last exchanges with.//// Moved unchanged from the old `/code` screens' `SessionPaper.swift` when// `/code` became the session table's list: the project page is the one// screen still drawing it. A session's own page is the session table's// conversation page (`session.json`, `ConversationPage`), which draws the// same notes from the definition.//// The conversation is the approved chat: the session's words on cream notes at// the left, mine on pale blue at the right, a long turn a page-wide letter, the// started and ended lines handwritten in pencil down the middle.import CottonKitimport SwiftUI// MARK: - the conversation/// A line handwritten in pencil down the middle of the conversation — when the/// session started, and when it ended.struct SessionSystemLine: View {    let text: String    var body: some View {        Text(text)            .paperHand(15)            .multilineTextAlignment(.center)            .frame(maxWidth: .infinity, alignment: .center)            .padding(.vertical, 12)    }}/// One turn or one message, on the paper the approved chat puts it on: the/// session's on cream at the left, mine on pale blue at the right. A long one is/// a letter — page wide, a wider margin. Attachments sit above the words as/// prints; the time (and, on mine, where the work stands) is handwritten under/// the paper on its outer side.struct SessionNote<Answer: View>: View {    let key: String    let words: String    let mine: Bool    let footer: String?    let media: [SessionFacts.Attachment]    @ViewBuilder let answer: () -> Answer    private static var noteInset: CGFloat { 14 }    private static var letterInset: CGFloat { 22 }    private static var printWidth: CGFloat { 132 }    private static var sideLane: CGFloat { 44 }    private var isLetter: Bool { ChatPaper.isLetter(words) }    private var dye: PaperDye { mine ? .blue : .cream }    var body: some View {        HStack(alignment: .top, spacing: 0) {            if mine && !isLetter { Spacer(minLength: Self.sideLane) }            VStack(alignment: mine ? .trailing : .leading, spacing: 6) {                if !media.isEmpty {                    prints                }                note                if let footer {                    Text(footer)                        .paperHand(14)                        .padding(.horizontal, 4)                }            }            if !mine && !isLetter { Spacer(minLength: Self.sideLane) }        }        .padding(.top, 8)        .accessibilityElement(children: .contain)        .accessibilityIdentifier("session-note-\(key)")    }    @ViewBuilder private var note: some View {        let inset = isLetter ? Self.letterInset : Self.noteInset        let paper = Paper(dye: dye, edge: .cut, seed: key, inset: inset) {            wordsBlock(inset: inset)        }        if isLetter {            paper        } else {            HuggingWidth(inset: inset) {                wordsBlock(inset: inset).hidden().accessibilityHidden(true)                paper            }        }    }    private func wordsBlock(inset: CGFloat) -> some View {        VStack(alignment: .leading, spacing: 10) {            Text(words)                .paperBody(15)                .lineSpacing(isLetter ? 6 : 3)                .fixedSize(horizontal: false, vertical: true)                .textSelection(.enabled)            answer()        }    }    private var prints: some View {        HStack(spacing: 8) {            ForEach(media) { entry in                InstantPrint(seed: "\(key)-\(entry.key)", size: .grid) {                    CapturedImage(                        key: entry.key,                        title: entry.name ?? ShellLines.line("projects", "attachment"),                        width: Self.printWidth,                        height: Self.printWidth                    )                }                .frame(width: Self.printWidth)            }        }    }}// MARK: - the transcript/// A session's conversation, as the lines it is made of.////// **ONE TRANSCRIPT, TWO PAGES.** `/sessions/[id]` draws it whole and a/// project page draws its conversation's last exchanges inline, and two copies/// of these five cases is two places a new kind of line has to be added. It/// is read only: Cotton sends nothing to a session (Nov 2026), so an old/// message is history and offers no answer.struct SessionTranscript: View {    let session: JSONValue    let messages: [JSONValue]    let turns: [JSONValue]    /// The session and session-message definitions, whose conversation says    /// the words under each note.    @Environment(\.tableDefinitions) private var definitions    /// How many lines a surface that does not OWN this transcript draws — the    /// last `limit` of them (`SessionFacts.recent`). `nil` on the session's    /// own page, which is where the whole of it lives.    var limit: Int?    var body: some View {        VStack(alignment: .leading, spacing: 0) {            ForEach(lines) { item in                line(item)            }        }    }    private var lines: [SessionFacts.ChatItem] {        let all = SessionFacts.chat(session, messages: messages, turns: turns)        guard let limit else { return all }        return SessionFacts.recent(all, limit: limit)    }    @ViewBuilder private func line(_ item: SessionFacts.ChatItem) -> some View {        switch item {        case .started(let line), .ended(let line):            if let line { SessionSystemLine(text: line) }        case .turn(let key, let turn, let mine):            // A TURN has no state — it happened — so its footer is the time            // alone. It still HAS one: a run is otherwise a wall of notes with            // nothing between "9:04 am" and "6:12 pm".            SessionNote(                key: key,                words: turn.text("text") ?? "",                mine: mine,                footer: turn.day("ts")?.clock,                media: []            ) { EmptyView() }        case .message(let key, let message, let mine):            SessionNote(                key: key,                words: message.text("body") ?? "",                mine: mine,                // The time, in the definitions' words.                footer: SessionFacts.messageFooter(message, mine: mine, definitions: definitions),                media: SessionFacts.media(message)            ) { EmptyView() }        }    }}