╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2378 try self.session.navigate(route); 2379 } 2380 2381 // commit a new or edited comment and navigate to its permalink. this is the 2382 // terminal path; the web posts the form to its comment route. 2383 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2384 if (comptime wasm) return; 2385 const io = self.session.io orelse return; 2386 const src = self.data.repo_source orelse return; 2387 const author = (try self.session.eventAuthor()) orelse return; 2388 const form = self.threadForm() orelse return; 2389 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2390 const body = try body_input.text(allocator); 2391 defer allocator.free(body); 2392 if (!evt.Comment.fieldsValid(body)) return; 2393 2394 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2395 @memcpy(&thread_id, self.data.selected_id); 2396 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2397 var parent: [evt.event_id_size * 2]u8 = undefined; 2398 @memcpy(&parent, self.data.comment_id); 2399 break :blk parent; 2400 }; 2401 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2402 const editing = self.data.view == .edit_comment; 2403 switch (src.repo_kind) { 2404 inline else => |repo_kind| { 2405 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2406 defer any_repo.deinit(io, allocator); 2407 switch (any_repo) { 2408 inline else => |*repo| if (editing) { 2409 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2410 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2411 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2412 } else { 2413 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2414 }, 2415 } 2416 }, 2417 } 2418 2419 body_input.clear(allocator); 2420 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2421 try self.session.navigate(route); 2422 } 2423 2424 // the terminal submit paths: the new form commits a new thread event, the 2425 // edit form re-emits the selected thread's event with the form's content. 2426 // the web posts the forms to their routes instead. 2427 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2428 if (self.data.view == .edit) { 2429 try self.submitEditedThread(allocator); 2430 } else { 2431 try self.submitNewThread(allocator); 2432 } 2433 } 2434 2435 // commit the new thread to the repo's events branch and navigate to it. 2436 // this is the terminal path; the web posts the form to the thread route, 2437 // so the wasm side never runs (or compiles) the repo access below. 2438 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2439 if (comptime wasm) return; 2440 const io = self.session.io orelse return; 2441 const src = self.data.repo_source orelse return; 2442 const author = (try self.session.eventAuthor()) orelse return; 2443 2444 const form = self.threadForm() orelse return; 2445 const title_input = &form.children.values()[title_field_index].widget.text_input; 2446 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2447 const description_input = &form.children.values()[description_field_index].widget.text_input; 2448 2449 const title = try title_input.text(allocator); 2450 defer allocator.free(title); 2451 const tags = try tags_input.text(allocator); 2452 defer allocator.free(tags); 2453 const description = try description_input.text(allocator); 2454 defer allocator.free(description); 2455 2456 if (!Event.fieldsValid(title, tags)) return; 2457 2458 if (comptime supports_drafts) { 2459 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2460 title_input.clear(allocator); 2461 tags_input.clear(allocator); 2462 description_input.clear(allocator); 2463 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2464 try self.session.navigate(route); 2465 return; 2466 } 2467 2468 var id_bytes: [evt.event_id_size]u8 = undefined; 2469 io.random(&id_bytes); 2470 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2471 2472 const event = evt.EventWithId{ 2473 .id = event_id_hex, 2474 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2475 .author = author, 2476 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2477 .title = title, 2478 .description = description, 2479 .tags = tags, 2480 }), 2481 }; 2482 2483 switch (src.repo_kind) { 2484 inline else => |repo_kind| { 2485 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2486 defer any_repo.deinit(io, allocator); 2487 switch (any_repo) { 2488 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2489 } 2490 }, 2491 } 2492 2493 // wipe the form so a return visit starts fresh 2494 title_input.clear(allocator); 2495 tags_input.clear(allocator); 2496 description_input.clear(allocator); 2497 2498 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2499 try self.session.navigate(route); 2500 } 2501 2502 // re-emit the selected thread's event with the form's content (status 2503 // preserved), then reload the thread's page. this is the terminal path; 2504 // the web posts the form to the edit route. 2505 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2506 if (comptime wasm) return; 2507 const io = self.session.io orelse return; 2508 const entry = self.data.selectedThread() orelse return; 2509 2510 const form = self.threadForm() orelse return; 2511 const title_input = &form.children.values()[title_field_index].widget.text_input; 2512 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2513 const description_input = &form.children.values()[description_field_index].widget.text_input; 2514 2515 const title = try title_input.text(allocator); 2516 defer allocator.free(title); 2517 const tags = try tags_input.text(allocator); 2518 defer allocator.free(tags); 2519 const description = try description_input.text(allocator); 2520 defer allocator.free(description); 2521 2522 if (!Event.fieldsValid(title, tags)) return; 2523 2524 if (comptime supports_drafts) { 2525 if (entryDraft(entry.*)) { 2526 try self.data.editDraft(self.session, allocator, entry.id, title, tags, description); 2527 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2528 try self.session.navigate(route); 2529 return; 2530 } 2531 } 2532 2533 const id_bytes = try evt.parseEventId(entry.id); 2534 const src = self.data.repo_source orelse return; 2535 const author = (try self.session.eventAuthor()) orelse return; 2536 2537 switch (src.repo_kind) { 2538 inline else => |repo_kind| { 2539 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2540 defer any_repo.deinit(io, allocator); 2541 switch (any_repo) { 2542 inline else => |*repo| if (has_status) 2543 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2544 .title = title, 2545 .tags = tags, 2546 .description = description, 2547 } }, author) 2548 else 2549 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2550 } 2551 }, 2552 } 2553 2554 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2555 try self.session.navigate(route); 2556 } 2557 2558 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2559 switch (confirmation(self.data.view) orelse return) { 2560 .publish => if (comptime supports_drafts) { 2561 if (comptime wasm) return; 2562 const entry = self.data.selectedThread() orelse return; 2563 if (!entryDraft(entry.*)) return; 2564 try self.data.publishDraft(self.session, allocator, entry.id); 2565 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2566 try self.session.navigate(route); 2567 } else unreachable, 2568 .merge => if (comptime supports_merge) { 2569 if (comptime wasm) return; 2570 const entry = self.data.selectedThread() orelse return; 2571 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2572 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2573 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2574 try self.session.navigate(route); 2575 } else unreachable, 2576 .remove => try self.submitRemove(allocator), 2577 } 2578 } 2579 2580 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2581 const comment = self.data.comment_id.len != 0; 2582 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2583 const id = evt.parseEventId(event_id) catch return; 2584 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2585 } 2586 2587 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2588 if (comptime wasm) return; 2589 2590 if (comptime supports_drafts) { 2591 const selected = self.data.selectedThread() orelse return; 2592 if (event_kind == kind and entryDraft(selected.*)) { 2593 try Data.removeDraft(self.session, allocator, &id); 2594 const route = draftsRoute(self.data.identity) orelse return; 2595 try self.session.navigate(route); 2596 return; 2597 } 2598 } 2599 2600 const io = self.session.io orelse return; 2601 const src = self.data.repo_source orelse return; 2602 const author = (try self.session.eventAuthor()) orelse return; 2603 2604 switch (src.repo_kind) { 2605 inline else => |repo_kind| { 2606 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2607 defer any_repo.deinit(io, allocator); 2608 switch (any_repo) { 2609 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2610 } 2611 }, 2612 } 2613 2614 const id_hex = std.fmt.bytesToHex(id, .lower); 2615 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2616 try self.session.navigate(route); 2617 } 2618 2619 // return to the list. 2620 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2621 root_focus.setFocus(self.listScroll(index).getFocus().id); 2622 } 2623 2624 // cross to the header tabs above the stack. 2625 fn focusHeader(self: *This, root_focus: *Focus) void { 2626 root_focus.setFocus(self.box.children.keys()[header_index]); 2627 } 2628 2629 pub fn clearGrid(self: *This) void { 2630 self.box.clearGrid(); 2631 } 2632 2633 pub fn getGrid(self: This) ?Grid { 2634 return self.box.getGrid(); 2635 } 2636 2637 pub fn getFocus(self: *This) *Focus { 2638 return self.box.getFocus(); 2639 } 2640 2641 // for the parent's "scroll up at the top jumps to the header" check: at the 2642 // top only while the header tabs hold focus, so up from the split 2643 // crosses into the tabs first. 2644 pub fn getSelectedIndex(self: *This) ?usize { 2645 return if (self.headerActive()) 0 else 1; 2646 } 2647 }; 2648 } 2649