// 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.importCottonKitimportCottonValuesimportFoundationstructProjectTableSource: SettingsTableSource {let table = "project"staticlet new = "new"staticlet 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) asyncthrows -> [TableRecord] {iflet answer = try? await core.callJSON(CottonMethod.readProjects, ReadArgs()) {returnSelf.rows(answer) }let answer = tryawaitSessionCalls.answer { tryawait core.callJSON(CottonMethod.readSessions, ReadArgs()) }returnSelf.rows(answer) }/// `read.projects`' rows, most recently worked on first, each by its id,/// with the name, slug, About and status the list draws.staticfunc 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 inguardlet id = project.text("id") else { returnnil }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) asyncthrows -> SettingsActed {guard action == Self.new, let name = values[Self.nameField]?.trimmingCharacters(in: .whitespacesAndNewlines), !name.isEmpty else {returntryawait perform(action, record: record, core: core) }let taken = Set(tryawait records(core).compactMap { $0["slug"].text })let slug = ProjectFacts.mintSlug(name, taken: taken)tryawaitSessionCalls.write(core, "create_project", ["slug": .string(slug), "name": .string(name)])return .outcome(.navigate(.projectsSlug(slug: slug))) }}