╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2454 break :blk parent; 2455 }; 2456 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2457 const editing = self.data.view == .edit_comment; 2458 switch (src.repo_kind) { 2459 inline else => |repo_kind| { 2460 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2461 defer any_repo.deinit(io, allocator); 2462 switch (any_repo) { 2463 inline else => |*repo| if (editing) { 2464 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2465 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2466 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2467 } else { 2468 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2469 }, 2470 } 2471 }, 2472 } 2473 2474 body_input.clear(allocator); 2475 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2476 try self.session.navigate(route); 2477 } 2478 2479 // the terminal submit paths: the new form commits a new thread event, the 2480 // edit form re-emits the selected thread's event with the form's content. 2481 // the web posts the forms to their routes instead. 2482 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2483 if (self.data.view == .edit) { 2484 try self.submitEditedThread(allocator); 2485 } else { 2486 try self.submitNewThread(allocator); 2487 } 2488 } 2489 2490 // commit the new thread to the repo's events branch and navigate to it. 2491 // this is the terminal path; the web posts the form to the thread route, 2492 // so the wasm side never runs (or compiles) the repo access below. 2493 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2494 if (comptime wasm) return; 2495 const io = self.session.io orelse return; 2496 const src = self.data.repo_source orelse return; 2497 const author = (try self.session.eventAuthor()) orelse return; 2498 2499 const form = self.threadForm() orelse return; 2500 const title_input = &form.children.values()[title_field_index].widget.text_input; 2501 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2502 const description_input = &form.children.values()[description_field_index].widget.text_input; 2503 2504 const title = try title_input.text(allocator); 2505 defer allocator.free(title); 2506 const tags = try tags_input.text(allocator); 2507 defer allocator.free(tags); 2508 const description = try description_input.text(allocator); 2509 defer allocator.free(description); 2510 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2511 defer if (comptime supports_drafts) allocator.free(target_branch); 2512 2513 if (!Event.fieldsValid(title, tags)) { 2514 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2515 return; 2516 } 2517 2518 if (comptime supports_drafts) { 2519 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2520 error.InvalidFields => { 2521 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2522 return; 2523 }, 2524 else => |other| return other, 2525 }; 2526 title_input.clear(allocator); 2527 tags_input.clear(allocator); 2528 description_input.clear(allocator); 2529 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2530 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2531 try self.session.navigate(route); 2532 return; 2533 } 2534 2535 var id_bytes: [evt.event_id_size]u8 = undefined; 2536 io.random(&id_bytes); 2537 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2538 2539 const event = evt.EventWithId{ 2540 .id = event_id_hex, 2541 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2542 .author = author, 2543 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2544 .title = title, 2545 .description = description, 2546 .tags = tags, 2547 }), 2548 }; 2549 2550 switch (src.repo_kind) { 2551 inline else => |repo_kind| { 2552 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2553 defer any_repo.deinit(io, allocator); 2554 switch (any_repo) { 2555 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2556 } 2557 }, 2558 } 2559 2560 // wipe the form so a return visit starts fresh 2561 title_input.clear(allocator); 2562 tags_input.clear(allocator); 2563 description_input.clear(allocator); 2564 2565 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2566 try self.session.navigate(route); 2567 } 2568 2569 // re-emit the selected thread's event with the form's content (status 2570 // preserved), then reload the thread's page. this is the terminal path; 2571 // the web posts the form to the edit route. 2572 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2573 if (comptime wasm) return; 2574 const io = self.session.io orelse return; 2575 const entry = self.data.selectedThread() orelse return; 2576 2577 const form = self.threadForm() orelse return; 2578 const title_input = &form.children.values()[title_field_index].widget.text_input; 2579 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2580 const description_input = &form.children.values()[description_field_index].widget.text_input; 2581 2582 const title = try title_input.text(allocator); 2583 defer allocator.free(title); 2584 const tags = try tags_input.text(allocator); 2585 defer allocator.free(tags); 2586 const description = try description_input.text(allocator); 2587 defer allocator.free(description); 2588 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2589 defer if (comptime supports_drafts) allocator.free(target_branch); 2590 2591 if (!Event.fieldsValid(title, tags)) { 2592 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2593 return; 2594 } 2595 const src = self.data.repo_source orelse return; 2596 2597 if (comptime supports_drafts) { 2598 if (entryDraft(entry.*)) { 2599 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2600 error.InvalidFields => { 2601 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2602 return; 2603 }, 2604 else => |other| return other, 2605 }; 2606 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2607 try self.session.navigate(route); 2608 return; 2609 } 2610 } 2611 2612 const id_bytes = try evt.parseEventId(entry.id); 2613 const author = (try self.session.eventAuthor()) orelse return; 2614 2615 switch (src.repo_kind) { 2616 inline else => |repo_kind| { 2617 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2618 defer any_repo.deinit(io, allocator); 2619 switch (any_repo) { 2620 inline else => |*repo| if (supports_drafts) { 2621 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2622 .title = title, 2623 .tags = tags, 2624 .description = description, 2625 .target_branch = target_branch, 2626 } }, author) catch |err| switch (err) { 2627 error.InvalidFields => { 2628 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2629 return; 2630 }, 2631 else => |other| return other, 2632 }; 2633 } else if (has_status) 2634 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2635 .title = title, 2636 .tags = tags, 2637 .description = description, 2638 } }, author) 2639 else 2640 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2641 } 2642 }, 2643 } 2644 2645 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2646 try self.session.navigate(route); 2647 } 2648 2649 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2650 const aa = self.session.arena.allocator(); 2651 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2652 .failure = failure, 2653 .fields = if (comptime supports_drafts) .{ 2654 .title = try aa.dupe(u8, title), 2655 .tags = try aa.dupe(u8, tags), 2656 .description = try aa.dupe(u8, description), 2657 .target_branch = try aa.dupe(u8, target_branch), 2658 } else .{ 2659 .title = try aa.dupe(u8, title), 2660 .tags = try aa.dupe(u8, tags), 2661 .description = try aa.dupe(u8, description), 2662 }, 2663 }); 2664 } 2665 2666 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2667 switch (confirmation(self.data.view) orelse return) { 2668 .publish => if (comptime supports_drafts) { 2669 if (comptime wasm) return; 2670 const entry = self.data.selectedThread() orelse return; 2671 if (!entryDraft(entry.*)) return; 2672 try self.data.publishDraft(self.session, allocator, entry.id); 2673 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2674 try self.session.navigate(route); 2675 } else unreachable, 2676 .merge => if (comptime supports_merge) { 2677 if (comptime wasm) return; 2678 const entry = self.data.selectedThread() orelse return; 2679 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2680 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2681 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2682 try self.session.navigate(route); 2683 } else unreachable, 2684 .remove => try self.submitRemove(allocator), 2685 } 2686 } 2687 2688 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2689 const comment = self.data.comment_id.len != 0; 2690 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2691 const id = evt.parseEventId(event_id) catch return; 2692 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2693 } 2694 2695 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2696 if (comptime wasm) return; 2697 2698 if (comptime supports_drafts) { 2699 const selected = self.data.selectedThread() orelse return; 2700 if (event_kind == kind and entryDraft(selected.*)) { 2701 try Data.removeDraft(self.session, allocator, &id); 2702 const route = draftsRoute(self.data.identity) orelse return; 2703 try self.session.navigate(route); 2704 return; 2705 } 2706 } 2707 2708 const io = self.session.io orelse return; 2709 const src = self.data.repo_source orelse return; 2710 const author = (try self.session.eventAuthor()) orelse return; 2711 2712 switch (src.repo_kind) { 2713 inline else => |repo_kind| { 2714 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2715 defer any_repo.deinit(io, allocator); 2716 switch (any_repo) { 2717 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2718 } 2719 }, 2720 } 2721 2722 const id_hex = std.fmt.bytesToHex(id, .lower); 2723 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2724 try self.session.navigate(route); 2725 } 2726 2727 // return to the list. 2728 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2729 root_focus.setFocus(self.listScroll(index).getFocus().id); 2730 } 2731 2732 // cross to the header tabs above the stack. 2733 fn focusHeader(self: *This, root_focus: *Focus) void { 2734 root_focus.setFocus(self.box.children.keys()[header_index]); 2735 } 2736 2737 pub fn clearGrid(self: *This) void { 2738 self.box.clearGrid(); 2739 } 2740 2741 pub fn getGrid(self: This) ?Grid { 2742 return self.box.getGrid(); 2743 } 2744 2745 pub fn getFocus(self: *This) *Focus { 2746 return self.box.getFocus(); 2747 } 2748 2749 // for the parent's "scroll up at the top jumps to the header" check: at the 2750 // top only while the header tabs hold focus, so up from the split 2751 // crosses into the tabs first. 2752 pub fn atTop(self: *This) bool { 2753 return self.headerActive(); 2754 } 2755 }; 2756 } 2757