╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2367 } else false; 2368 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2369 } else if (key == .enter) { 2370 if (try self.header().submittedText(allocator)) |text| { 2371 defer allocator.free(text); 2372 try self.submitSearch(text); 2373 } 2374 } else { 2375 try self.header().input(allocator, key, root_focus); 2376 } 2377 return; 2378 } 2379 if (self.tagsViewActive()) { 2380 self.tagsViewInput(key, root_focus); 2381 return; 2382 } 2383 if (self.formViewActive()) { 2384 if (confirmation(self.data.view) != null) { 2385 try self.confirmationFormInput(allocator, key, root_focus); 2386 } else if (supports_conflicts and self.data.view == .resolve) { 2387 try self.resolveInput(allocator, key, root_focus); 2388 } else { 2389 try self.formInput(allocator, key, root_focus); 2390 } 2391 return; 2392 } 2393 const i = self.selectedSplitIndex() orelse return; 2394 if (self.detailActive(i)) { 2395 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2396 .none => {}, 2397 .list => self.focusList(i, root_focus), 2398 .header => self.focusHeader(root_focus), 2399 } 2400 } else { 2401 self.listInput(i, key, root_focus); 2402 } 2403 } 2404 2405 // navigate to the typed query's results, keeping the tag filter and the 2406 // status list the page shows. an empty box clears a filtered page and 2407 // does nothing on any other one. 2408 fn submitSearch(self: *This, text: []const u8) !void { 2409 if (text.len == 0 and self.data.search == null) return; 2410 const selected = self.header().getSelectedIndex() orelse 0; 2411 const status: Status = if (selected < status_count) splitStatus(selected) else @enumFromInt(0); 2412 const route = listRoute(self.data.identity, status, self.data.tag, "") orelse return; 2413 try self.session.navigate(route.withSearch(text) orelse return); 2414 } 2415 2416 // arrow keys move the tag selection; up from the top row crosses to the 2417 // header tabs. 2418 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2419 const tf = self.tagsView(); 2420 const cid = tf.focus.child_id orelse return; 2421 const cur = tf.indexOfFocusId(cid) orelse return; 2422 const count = tf.text_boxes.items.len; 2423 switch (key) { 2424 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2425 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2426 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2427 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2428 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2429 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2430 else => {}, 2431 } 2432 } 2433 2434 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2435 // up/down (and the scroll wheel) move the selection a row; page up/down 2436 // jump a fixed amount. right/Enter cross into the detail pane. up from 2437 // the top row crosses into the header tabs. 2438 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2439 const lb = self.listBox(index); 2440 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2441 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2442 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2443 return; 2444 } 2445 switch (key) { 2446 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2447 else => {}, 2448 } 2449 } 2450 2451 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2452 const form = self.formBox() orelse return; 2453 const cid = form.getFocus().child_id orelse return; 2454 const cur = form.children.getIndex(cid) orelse return; 2455 const child = &form.children.values()[cur]; 2456 2457 if (child.widget == .text_input) { 2458 const text_input = &child.widget.text_input; 2459 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2460 return child.widget.input(allocator, key, root_focus); 2461 } 2462 2463 const on_submit = child.widget == .submit_button; 2464 switch (key) { 2465 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2466 root_focus.setFocus(form.children.keys()[i]) 2467 else 2468 self.focusHeader(root_focus), 2469 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2470 .enter => if (on_submit) return self.submitForm(allocator), 2471 .mouse => |mouse| if (on_submit) { 2472 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2473 }, 2474 else => {}, 2475 } 2476 try child.widget.input(allocator, key, root_focus); 2477 } 2478 2479 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2480 const form = self.confirmationForm() orelse return; 2481 const child_id = form.getFocus().child_id orelse return; 2482 const index = form.children.getIndex(child_id) orelse return; 2483 switch (key) { 2484 .arrow_up, .back_tab => if (index > 1) 2485 root_focus.setFocus(form.children.keys()[index - 1]) 2486 else 2487 self.focusHeader(root_focus), 2488 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2489 .enter => try self.submitConfirmation(allocator, index), 2490 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2491 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2492 try self.submitConfirmation(allocator, button_index); 2493 break; 2494 } 2495 }, 2496 else => {}, 2497 } 2498 } 2499 2500 // the multiline hunk inputs keep enter and any up/down that has a row to 2501 // move to; enter on a "use this" link is a navigation the host follows 2502 // before this runs. 2503 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2504 const form = self.formBox() orelse return; 2505 const cid = form.getFocus().child_id orelse return; 2506 const cur = form.children.getIndex(cid) orelse return; 2507 const child = &form.children.values()[cur]; 2508 2509 if (child.widget == .text_input) { 2510 const ti = &child.widget.text_input; 2511 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2512 return child.widget.input(allocator, key, root_focus); 2513 } 2514 2515 const on_submit = child.widget == .submit_button; 2516 2517 switch (key) { 2518 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2519 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2520 .arrow_left, .arrow_right => switch (child.widget) { 2521 .box => |*row| { 2522 const row_cid = row.getFocus().child_id orelse return; 2523 const row_index = row.children.getIndex(row_cid) orelse return; 2524 if (key == .arrow_left) { 2525 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2526 } else if (row_index + 1 < row.children.count()) { 2527 root_focus.setFocus(row.children.keys()[row_index + 1]); 2528 } 2529 }, 2530 else => try child.widget.input(allocator, key, root_focus), 2531 }, 2532 .enter => if (on_submit) try self.submitResolution(allocator), 2533 .mouse => |mouse| if (on_submit) { 2534 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2535 }, 2536 else => try child.widget.input(allocator, key, root_focus), 2537 } 2538 } 2539 2540 // a multiline input keeps enter and any up/down that has a row to move to 2541 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2542 return switch (key) { 2543 .enter => true, 2544 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2545 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2546 else => false, 2547 }; 2548 } 2549 2550 // the neighboring form control, skipping spacers and blank rows 2551 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2552 var i = cur; 2553 while (true) { 2554 if (down) { 2555 i += 1; 2556 if (i >= form.children.count()) return null; 2557 } else { 2558 if (i == 0) return null; 2559 i -= 1; 2560 } 2561 const child = &form.children.values()[i]; 2562 if (child.hidden) continue; 2563 switch (child.widget) { 2564 .spacer, .text => continue, 2565 else => return i, 2566 } 2567 } 2568 } 2569 2570 // focus the resolve-form child at `i` (a version row focuses its selected 2571 // child) and keep it visible on the terminal 2572 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2573 const child = &form.children.values()[i]; 2574 switch (child.widget) { 2575 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2576 else => root_focus.setFocus(form.children.keys()[i]), 2577 } 2578 if (self.session.is_terminal) { 2579 if (self.resolveScroll()) |sc| { 2580 if (child.rect) |rect| sc.scrollToRect(rect); 2581 } 2582 } 2583 } 2584 2585 // re-emit the conflicted thread's event with the resolved content; any 2586 // event on the thread settles its conflict entry. this is the terminal 2587 // path; the web posts the form to the resolve route. 2588 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2589 if (comptime wasm) return; 2590 const io = self.session.io orelse return; 2591 const src = self.data.repo_source orelse return; 2592 const entry = self.data.selectedThread() orelse return; 2593 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2594 const form = self.formBox() orelse return; 2595 2596 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2597 var title: ?[]u8 = null; 2598 var tags: ?[]u8 = null; 2599 var hunks: std.ArrayList([]const u8) = .empty; 2600 defer { 2601 if (title) |t| allocator.free(t); 2602 if (tags) |t| allocator.free(t); 2603 for (hunks.items) |h| allocator.free(h); 2604 hunks.deinit(allocator); 2605 } 2606 for (form.children.values()) |*child| switch (child.widget) { 2607 .text_input => |*ti| { 2608 const text = try ti.text(allocator); 2609 errdefer allocator.free(text); 2610 if (std.mem.eql(u8, ti.options.name, "title")) { 2611 title = text; 2612 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2613 tags = text; 2614 } else { 2615 try hunks.append(allocator, text); 2616 } 2617 }, 2618 else => {}, 2619 }; 2620 2621 const id_bytes = try evt.parseEventId(entry.id); 2622 2623 switch (src.repo_kind) { 2624 inline else => |repo_kind| { 2625 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2626 defer any_repo.deinit(io, allocator); 2627 switch (any_repo) { 2628 inline else => |*repo| { 2629 var resolution: Event.Resolve = .{ 2630 .title = title, 2631 .tags = tags, 2632 .hunks = hunks.items, 2633 }; 2634 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2635 const change = Event.Update{ .resolve = resolution }; 2636 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2637 // leave the form up for correction 2638 error.InvalidFields => return, 2639 else => |e| return e, 2640 }; 2641 }, 2642 } 2643 }, 2644 } 2645 2646 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2647 try self.session.navigate(route); 2648 } 2649 2650 // commit a new or edited comment and navigate to its permalink. this is the 2651 // terminal path; the web posts the form to its comment route. 2652 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2653 if (comptime wasm) return; 2654 const io = self.session.io orelse return; 2655 const src = self.data.repo_source orelse return; 2656 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2657 const author = actor.author; 2658 const form = self.formBox() orelse return; 2659 const body_input = try formField(form, "body"); 2660 const body = try body_input.text(allocator); 2661 defer allocator.free(body); 2662 if (!evt.Comment.fieldsValid(body)) return; 2663 2664 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2665 @memcpy(&thread_id, self.data.selected_id); 2666 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2667 var parent: [evt.event_id_size * 2]u8 = undefined; 2668 @memcpy(&parent, self.data.comment_id); 2669 break :blk parent; 2670 }; 2671 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2672 const editing = self.data.view == .edit_comment; 2673 if (editing) { 2674 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2675 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2676 } 2677 switch (src.repo_kind) { 2678 inline else => |repo_kind| { 2679 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2680 defer any_repo.deinit(io, allocator); 2681 switch (any_repo) { 2682 inline else => |*repo| if (editing) { 2683 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2684 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2685 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2686 } else { 2687 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); 2688 }, 2689 } 2690 }, 2691 } 2692 2693 body_input.clear(allocator); 2694 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2695 try self.session.navigate(route); 2696 } 2697 2698 // terminal form submissions; the web posts to the forms' routes instead 2699 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2700 switch (self.data.view) { 2701 .new_comment, .edit_comment => try self.submitComment(allocator), 2702 .edit => try self.submitEditedThread(allocator), 2703 else => try self.submitNewThread(allocator), 2704 } 2705 } 2706 2707 // commit the new thread to the repo's events branch and navigate to it. 2708 // this is the terminal path; the web posts the form to the thread route, 2709 // so the wasm side never runs (or compiles) the repo access below. 2710 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2711 if (comptime wasm) return; 2712 const io = self.session.io orelse return; 2713 const src = self.data.repo_source orelse return; 2714 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2715 2716 const form = self.threadForm() orelse return; 2717 const title_input = try formField(form, "title"); 2718 const tags_input = try formField(form, "tags"); 2719 const description_input = try formField(form, "description"); 2720 2721 const title = try title_input.text(allocator); 2722 defer allocator.free(title); 2723 const tags = try tags_input.text(allocator); 2724 defer allocator.free(tags); 2725 const description = try description_input.text(allocator); 2726 defer allocator.free(description); 2727 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2728 defer if (comptime supports_drafts) allocator.free(target_branch); 2729 2730 if (!Event.fieldsValid(title, tags)) { 2731 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2732 return; 2733 } 2734 2735 if (comptime supports_drafts) { 2736 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2737 const source_branch = switch (source.selected) { 2738 .fork => null, 2739 .branch => try source.field().text(allocator), 2740 }; 2741 defer if (source_branch) |branch| allocator.free(branch); 2742 const event_id_hex = Data.create(self.data, self.session, allocator, author, title, tags, description, target_branch, source_branch) catch |err| { 2743 const failure = FeedbackFailure.fromError(err) orelse return err; 2744 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2745 return; 2746 }; 2747 title_input.clear(allocator); 2748 tags_input.clear(allocator); 2749 description_input.clear(allocator); 2750 (try formField(form, "target_branch")).clear(allocator); 2751 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2752 try self.session.navigate(route); 2753 return; 2754 } 2755 2756 var id_bytes: [evt.event_id_size]u8 = undefined; 2757 io.random(&id_bytes); 2758 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2759 2760 const event = evt.EventWithId{ 2761 .id = event_id_hex, 2762 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2763 .author = author, 2764 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2765 .title = title, 2766 .description = description, 2767 .tags = tags, 2768 }), 2769 }; 2770 2771 switch (src.repo_kind) { 2772 inline else => |repo_kind| { 2773 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2774 defer any_repo.deinit(io, allocator); 2775 switch (any_repo) { 2776 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}), 2777 } 2778 }, 2779 } 2780 2781 // wipe the form so a return visit starts fresh 2782 title_input.clear(allocator); 2783 tags_input.clear(allocator); 2784 description_input.clear(allocator); 2785 2786 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2787 try self.session.navigate(route); 2788 } 2789 2790 // re-emit the selected thread's event with the form's content (status 2791 // preserved), then reload the thread's page. this is the terminal path; 2792 // the web posts the form to the edit route. 2793 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2794 if (comptime wasm) return; 2795 const io = self.session.io orelse return; 2796 const entry = self.data.selectedThread() orelse return; 2797 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2798 const author = actor.author; 2799 2800 const form = self.threadForm() orelse return; 2801 const title_input = try formField(form, "title"); 2802 const tags_input = try formField(form, "tags"); 2803 const description_input = try formField(form, "description"); 2804 2805 const title = try title_input.text(allocator); 2806 defer allocator.free(title); 2807 const tags = try tags_input.text(allocator); 2808 defer allocator.free(tags); 2809 const description = try description_input.text(allocator); 2810 defer allocator.free(description); 2811 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2812 defer if (comptime supports_drafts) allocator.free(target_branch); 2813 2814 if (!Event.fieldsValid(title, tags)) { 2815 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2816 return; 2817 } 2818 const src = self.data.repo_source orelse return; 2819 2820 if (comptime supports_drafts) { 2821 if (entryDraft(entry.*)) { 2822 self.data.editDraft(self.session, allocator, author, entry.id, title, tags, description, target_branch) catch |err| { 2823 const failure = FeedbackFailure.fromError(err) orelse return err; 2824 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2825 return; 2826 }; 2827 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2828 try self.session.navigate(route); 2829 return; 2830 } 2831 } 2832 2833 const id_bytes = try evt.parseEventId(entry.id); 2834 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2835 2836 switch (src.repo_kind) { 2837 inline else => |repo_kind| { 2838 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2839 defer any_repo.deinit(io, allocator); 2840 switch (any_repo) { 2841 inline else => |*repo| if (supports_drafts) { 2842 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2843 .title = title, 2844 .tags = tags, 2845 .description = description, 2846 .target_branch = target_branch, 2847 } }, author) catch |err| { 2848 const failure = FeedbackFailure.fromError(err) orelse return err; 2849 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2850 return; 2851 }; 2852 } else if (has_status) 2853 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2854 .title = title, 2855 .tags = tags, 2856 .description = description, 2857 } }, author) 2858 else 2859 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2860 } 2861 }, 2862 } 2863 2864 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2865 try self.session.navigate(route); 2866 } 2867 2868 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2869 const aa = self.session.arena.allocator(); 2870 const source = if (supports_drafts) blk: { 2871 const form = self.threadForm() orelse break :blk null; 2872 break :blk formWidget(form, .patch_source); 2873 } else null; 2874 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2875 .failure = failure, 2876 .fields = if (comptime supports_drafts) .{ 2877 .title = try aa.dupe(u8, title), 2878 .tags = try aa.dupe(u8, tags), 2879 .description = try aa.dupe(u8, description), 2880 .target_branch = try aa.dupe(u8, target_branch), 2881 .source_branch = if (source) |value| try value.field().text(aa) else "", 2882 .source_kind = if (source) |value| value.selected else .fork, 2883 } else .{ 2884 .title = try aa.dupe(u8, title), 2885 .tags = try aa.dupe(u8, tags), 2886 .description = try aa.dupe(u8, description), 2887 }, 2888 }); 2889 } 2890 2891 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2892 switch (confirmation(self.data.view) orelse return) { 2893 .publish => if (comptime supports_drafts) { 2894 if (comptime wasm) return; 2895 const entry = self.data.selectedThread() orelse return; 2896 if (!entryDraft(entry.*)) return; 2897 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2898 try self.data.publishDraft(self.session, allocator, author, entry.id); 2899 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2900 try self.session.navigate(route); 2901 } else unreachable, 2902 .merge => if (comptime supports_merge) { 2903 if (comptime wasm) return; 2904 const entry = self.data.selectedThread() orelse return; 2905 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2906 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2907 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2908 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2909 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2910 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2911 try self.session.navigate(route); 2912 return; 2913 }, 2914 else => return err, 2915 }; 2916 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2917 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2918 try self.session.navigate(route); 2919 } else unreachable, 2920 .remove => try self.submitRemove(allocator), 2921 } 2922 } 2923 2924 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2925 const comment = self.data.comment_id.len != 0; 2926 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2927 const id = evt.parseEventId(event_id) catch return; 2928 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2929 } 2930 2931 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2932 if (comptime wasm) return; 2933 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2934 const author = actor.author; 2935 2936 if (comptime supports_drafts) { 2937 const selected = self.data.selectedThread() orelse return; 2938 if (event_kind == kind and entryDraft(selected.*)) { 2939 try Data.removeDraft(self.session, allocator, author, &id); 2940 const route = draftsRoute(self.data.identity) orelse return; 2941 try self.session.navigate(route); 2942 return; 2943 } 2944 } 2945 2946 const io = self.session.io orelse return; 2947 const src = self.data.repo_source orelse return; 2948 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2949 2950 switch (src.repo_kind) { 2951 inline else => |repo_kind| { 2952 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2953 defer any_repo.deinit(io, allocator); 2954 switch (any_repo) { 2955 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), 2956 } 2957 }, 2958 } 2959 2960 const id_hex = std.fmt.bytesToHex(id, .lower); 2961 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2962 try self.session.navigate(route); 2963 } 2964 2965 // return to the list. 2966 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2967 root_focus.setFocus(self.listScroll(index).getFocus().id); 2968 } 2969 2970 // cross to the header tabs above the stack. 2971 fn focusHeader(self: *This, root_focus: *Focus) void { 2972 root_focus.setFocus(self.box.children.keys()[header_index]); 2973 } 2974 2975 pub fn clearGrid(self: *This) void { 2976 self.box.clearGrid(); 2977 } 2978 2979 pub fn getGrid(self: This) ?Grid { 2980 return self.box.getGrid(); 2981 } 2982 2983 pub fn getFocus(self: *This) *Focus { 2984 return self.box.getFocus(); 2985 } 2986 2987 // for the parent's "scroll up at the top jumps to the header" check: at the 2988 // top only while the header tabs hold focus, so up from the split 2989 // crosses into the tabs first. 2990 pub fn atTop(self: *This) bool { 2991 return self.headerActive(); 2992 } 2993 }; 2994 } 2995