Cotton code
70 lines · 3.4 KB · SwiftRaw
// The `project` table: the projects of the database a window shows — `/projects`// is its list (a row opens the database page with that project chosen, which// is the project's page), and it is what a session's Project column picks// from and names, and what a link to a project is resolved against.//// The rows are `read.projects`: every project, active and archived — the list// sections them by status (`project.json`'s `group`). An archived project is still a row, so a link to one reaches its page, where// Restore is.//// **New project is the list's act**: the name typed, the slug minted from it// off every slug already in the database (archived ones included — the// column's UNIQUE does not read status, `ProjectFacts.mintSlug`), and the new// project's page opened. A project is changed on its page, never here.import CottonKitimport CottonValuesimport Foundationstruct ProjectTableSource: SettingsTableSource {    let table = "project"    static let new = "new"    static let nameField = "name"    /// `read.projects`, every project; a build whose core cannot answer it    /// falls back on `read.sessions`' own `projects` — the active ones the old    /// "Move to" sheet offered — so a session's Project column still names    /// its project.    func records(_ core: any CottonCalling) async throws -> [TableRecord] {        if let answer = try? await core.callJSON(CottonMethod.readProjects, ReadArgs()) {            return Self.rows(answer)        }        let answer = try await SessionCalls.answer { try await core.callJSON(CottonMethod.readSessions, ReadArgs()) }        return Self.rows(answer)    }    /// `read.projects`' rows, most recently worked on first, each by its id,    /// with the name, slug, About and status the list draws.    static func rows(_ answer: JSONValue) -> [TableRecord] {        // Most recently worked on first (`ProjectFacts.order`), as the caps        // board was.        let projects = ProjectFacts.order(answer.array ?? answer.list("projects"))        return projects.compactMap { project in            guard let id = project.text("id") else { return nil }            return settingsRecord(id, [                "name": .maybe(project.text("name") ?? project.text("slug")),                "slug": .maybe(project.text("slug")),                "description": .maybe(project.text("description")),                "status": .maybe(project.text("status")),            ])        }    }    func listActions(_ rows: [TableRecord]) -> [RecordAction] {        [RecordAction(            id: Self.new, label: CottonStrings.newProject,            fields: [ActField(key: Self.nameField, label: ShellLines.line("projects", "table.project_name"), required: true)],            placement: .main)]    }    func perform(_ action: String, values: [String: String], record: TableRecord?, core: any CottonCalling) async throws -> SettingsActed {        guard action == Self.new, let name = values[Self.nameField]?.trimmingCharacters(in: .whitespacesAndNewlines), !name.isEmpty else {            return try await perform(action, record: record, core: core)        }        let taken = Set(try await records(core).compactMap { $0["slug"].text })        let slug = ProjectFacts.mintSlug(name, taken: taken)        try await SessionCalls.write(core, "create_project", ["slug": .string(slug), "name": .string(name)])        return .outcome(.navigate(.projectsSlug(slug: slug)))    }}