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