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