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