╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2666 2667 body_input.clear(allocator); 2668 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2669 try self.session.navigate(route); 2670 } 2671 2672 // terminal form submissions; the web posts to the forms' routes instead 2673 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2674 switch (self.data.view) { 2675 .new_comment, .edit_comment => try self.submitComment(allocator), 2676 .edit => try self.submitEditedThread(allocator), 2677 else => try self.submitNewThread(allocator), 2678 } 2679 } 2680 2681 // commit the new thread to the repo's events branch and navigate to it. 2682 // this is the terminal path; the web posts the form to the thread route, 2683 // so the wasm side never runs (or compiles) the repo access below. 2684 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2685 if (comptime wasm) return; 2686 const io = self.session.io orelse return; 2687 const src = self.data.repo_source orelse return; 2688 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2689 2690 const form = self.threadForm() orelse return; 2691 const title_input = try formField(form, "title"); 2692 const labels_input = try formField(form, "labels"); 2693 const description_input = try formField(form, "description"); 2694 2695 const title = try title_input.text(allocator); 2696 defer allocator.free(title); 2697 const labels = try labels_input.text(allocator); 2698 defer allocator.free(labels); 2699 const description = try description_input.text(allocator); 2700 defer allocator.free(description); 2701 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2702 defer if (comptime supports_drafts) allocator.free(target_branch); 2703 2704 if (!Event.fieldsValid(title, labels)) { 2705 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2706 return; 2707 } 2708 2709 if (comptime supports_drafts) { 2710 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2711 const source_branch = switch (source.selectedKind()) { 2712 .fork => null, 2713 .branch => try source.field().text(allocator), 2714 }; 2715 defer if (source_branch) |branch| allocator.free(branch); 2716 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2717 const failure = FeedbackFailure.fromError(err) orelse return err; 2718 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2719 return; 2720 }; 2721 title_input.clear(allocator); 2722 labels_input.clear(allocator); 2723 description_input.clear(allocator); 2724 (try formField(form, "target_branch")).clear(allocator); 2725 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2726 try self.session.navigate(route); 2727 return; 2728 } 2729 2730 var id_bytes: [evt.event_id_size]u8 = undefined; 2731 io.random(&id_bytes); 2732 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2733 2734 const event = evt.EventWithId{ 2735 .id = event_id_hex, 2736 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2737 .author = actor.author, 2738 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2739 .title = title, 2740 .description = description, 2741 .labels = labels, 2742 }), 2743 }; 2744 2745 switch (src.repo_kind) { 2746 inline else => |repo_kind| { 2747 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2748 defer any_repo.deinit(io, allocator); 2749 switch (any_repo) { 2750 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2751 } 2752 }, 2753 } 2754 2755 // wipe the form so a return visit starts fresh 2756 title_input.clear(allocator); 2757 labels_input.clear(allocator); 2758 description_input.clear(allocator); 2759 2760 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2761 try self.session.navigate(route); 2762 } 2763 2764 // re-emit the selected thread's event with the form's content (status 2765 // preserved), then reload the thread's page. this is the terminal path; 2766 // the web posts the form to the edit route. 2767 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2768 if (comptime wasm) return; 2769 const io = self.session.io orelse return; 2770 const entry = self.data.selectedThread() orelse return; 2771 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2772 const author = actor.author; 2773 2774 const form = self.threadForm() orelse return; 2775 const title_input = try formField(form, "title"); 2776 const labels_input = try formField(form, "labels"); 2777 const description_input = try formField(form, "description"); 2778 2779 const title = try title_input.text(allocator); 2780 defer allocator.free(title); 2781 const labels = try labels_input.text(allocator); 2782 defer allocator.free(labels); 2783 const description = try description_input.text(allocator); 2784 defer allocator.free(description); 2785 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2786 defer if (comptime supports_drafts) allocator.free(target_branch); 2787 2788 if (!Event.fieldsValid(title, labels)) { 2789 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2790 return; 2791 } 2792 const src = self.data.repo_source orelse return; 2793 2794 if (comptime supports_drafts) { 2795 if (entryDraft(entry.*)) { 2796 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2797 const failure = FeedbackFailure.fromError(err) orelse return err; 2798 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2799 return; 2800 }; 2801 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2802 try self.session.navigate(route); 2803 return; 2804 } 2805 } 2806 2807 const id_bytes = try evt.parseEventId(entry.id); 2808 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2809 2810 switch (src.repo_kind) { 2811 inline else => |repo_kind| { 2812 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2813 defer any_repo.deinit(io, allocator); 2814 switch (any_repo) { 2815 inline else => |*repo| if (supports_drafts) { 2816 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2817 .title = title, 2818 .labels = labels, 2819 .description = description, 2820 .target_branch = target_branch, 2821 } }, author) catch |err| { 2822 const failure = FeedbackFailure.fromError(err) orelse return err; 2823 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2824 return; 2825 }; 2826 } else if (has_status) 2827 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2828 .title = title, 2829 .labels = labels, 2830 .description = description, 2831 } }, author) 2832 else 2833 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2834 } 2835 }, 2836 } 2837 2838 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2839 try self.session.navigate(route); 2840 } 2841 2842 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2843 const aa = self.session.arena.allocator(); 2844 const source = if (supports_drafts) blk: { 2845 const form = self.threadForm() orelse break :blk null; 2846 break :blk formWidget(form, .patch_source); 2847 } else null; 2848 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2849 .failure = failure, 2850 .fields = if (comptime supports_drafts) .{ 2851 .title = try aa.dupe(u8, title), 2852 .labels = try aa.dupe(u8, labels), 2853 .description = try aa.dupe(u8, description), 2854 .target_branch = try aa.dupe(u8, target_branch), 2855 .source_branch = if (source) |value| try value.field().text(aa) else "", 2856 .source_kind = if (source) |value| value.selectedKind() else .fork, 2857 } else .{ 2858 .title = try aa.dupe(u8, title), 2859 .labels = try aa.dupe(u8, labels), 2860 .description = try aa.dupe(u8, description), 2861 }, 2862 }); 2863 } 2864 2865 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2866 switch (confirmation(self.data.view) orelse return) { 2867 .publish => if (comptime supports_drafts) { 2868 if (comptime wasm) return; 2869 const entry = self.data.selectedThread() orelse return; 2870 if (!entryDraft(entry.*)) return; 2871 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2872 try self.data.publishDraft(self.session, allocator, author, entry.id); 2873 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2874 try self.session.navigate(route); 2875 } else unreachable, 2876 .merge => if (comptime supports_merge) { 2877 if (comptime wasm) return; 2878 const entry = self.data.selectedThread() orelse return; 2879 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2880 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2881 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2882 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2883 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2884 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2885 try self.session.navigate(route); 2886 return; 2887 }, 2888 else => return err, 2889 }; 2890 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2891 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2892 try self.session.navigate(route); 2893 } else unreachable, 2894 .remove => try self.submitRemove(allocator), 2895 } 2896 } 2897 2898 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2899 const comment = self.data.comment_id.len != 0; 2900 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2901 const id = evt.parseEventId(event_id) catch return; 2902 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2903 } 2904 2905 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2906 if (comptime wasm) return; 2907 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2908 const author = actor.author; 2909 2910 if (comptime supports_drafts) { 2911 const selected = self.data.selectedThread() orelse return; 2912 if (event_kind == kind and entryDraft(selected.*)) { 2913 try Data.removeDraft(self.session, allocator, author, &id); 2914 const route = draftsRoute(self.data.identity) orelse return; 2915 try self.session.navigate(route); 2916 return; 2917 } 2918 } 2919 2920 const io = self.session.io orelse return; 2921 const src = self.data.repo_source orelse return; 2922 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2923 2924 switch (src.repo_kind) { 2925 inline else => |repo_kind| { 2926 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2927 defer any_repo.deinit(io, allocator); 2928 switch (any_repo) { 2929 inline else => |*repo| try evt.remove(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2930 } 2931 }, 2932 } 2933 2934 const id_hex = std.fmt.bytesToHex(id, .lower); 2935 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2936 try self.session.navigate(route); 2937 } 2938 2939 // return to the list. 2940 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2941 root_focus.setFocus(self.listScroll(index).getFocus().id); 2942 } 2943 2944 // cross to the header tabs above the stack. 2945 fn focusHeader(self: *This, root_focus: *Focus) void { 2946 root_focus.setFocus(self.box.children.keys()[header_index]); 2947 } 2948 2949 pub fn clearGrid(self: *This) void { 2950 self.box.clearGrid(); 2951 } 2952 2953 pub fn getGrid(self: This) ?Grid { 2954 return self.box.getGrid(); 2955 } 2956 2957 pub fn getFocus(self: *This) *Focus { 2958 return self.box.getFocus(); 2959 } 2960 2961 // for the parent's "scroll up at the top jumps to the header" check: at the 2962 // top only while the header tabs hold focus, so up from the split 2963 // crosses into the tabs first. 2964 pub fn atTop(self: *This) bool { 2965 return self.headerActive(); 2966 } 2967 }; 2968 } 2969