╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2360 switch (any_repo) { 2361 inline else => |*repo| { 2362 var resolution: Event.Resolve = .{ 2363 .title = title, 2364 .tags = tags, 2365 .hunks = hunks.items, 2366 }; 2367 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2368 const change = Event.Update{ .resolve = resolution }; 2369 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2370 // leave the form up for correction 2371 error.InvalidFields => return, 2372 else => |e| return e, 2373 }; 2374 }, 2375 } 2376 }, 2377 } 2378 2379 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2380 try self.session.navigate(route); 2381 } 2382 2383 // commit a new or edited comment and navigate to its permalink. this is the 2384 // terminal path; the web posts the form to its comment route. 2385 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2386 if (comptime wasm) return; 2387 const io = self.session.io orelse return; 2388 const src = self.data.repo_source orelse return; 2389 const author = (try self.session.eventAuthor()) orelse return; 2390 const form = self.threadForm() orelse return; 2391 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2392 const body = try body_input.text(allocator); 2393 defer allocator.free(body); 2394 if (!evt.Comment.fieldsValid(body)) return; 2395 2396 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2397 @memcpy(&thread_id, self.data.selected_id); 2398 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2399 var parent: [evt.event_id_size * 2]u8 = undefined; 2400 @memcpy(&parent, self.data.comment_id); 2401 break :blk parent; 2402 }; 2403 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2404 const editing = self.data.view == .edit_comment; 2405 switch (src.repo_kind) { 2406 inline else => |repo_kind| { 2407 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2408 defer any_repo.deinit(io, allocator); 2409 switch (any_repo) { 2410 inline else => |*repo| if (editing) { 2411 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2412 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2413 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2414 } else { 2415 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2416 }, 2417 } 2418 }, 2419 } 2420 2421 body_input.clear(allocator); 2422 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2423 try self.session.navigate(route); 2424 } 2425 2426 // the terminal submit paths: the new form commits a new thread event, the 2427 // edit form re-emits the selected thread's event with the form's content. 2428 // the web posts the forms to their routes instead. 2429 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2430 if (self.data.view == .edit) { 2431 try self.submitEditedThread(allocator); 2432 } else { 2433 try self.submitNewThread(allocator); 2434 } 2435 } 2436 2437 // commit the new thread to the repo's events branch and navigate to it. 2438 // this is the terminal path; the web posts the form to the thread route, 2439 // so the wasm side never runs (or compiles) the repo access below. 2440 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2441 if (comptime wasm) return; 2442 const io = self.session.io orelse return; 2443 const src = self.data.repo_source orelse return; 2444 const author = (try self.session.eventAuthor()) orelse return; 2445 2446 const form = self.threadForm() orelse return; 2447 const title_input = &form.children.values()[title_field_index].widget.text_input; 2448 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2449 const description_input = &form.children.values()[description_field_index].widget.text_input; 2450 2451 const title = try title_input.text(allocator); 2452 defer allocator.free(title); 2453 const tags = try tags_input.text(allocator); 2454 defer allocator.free(tags); 2455 const description = try description_input.text(allocator); 2456 defer allocator.free(description); 2457 2458 if (!Event.fieldsValid(title, tags)) return; 2459 2460 if (comptime supports_drafts) { 2461 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2462 title_input.clear(allocator); 2463 tags_input.clear(allocator); 2464 description_input.clear(allocator); 2465 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2466 try self.session.navigate(route); 2467 return; 2468 } 2469 2470 var id_bytes: [evt.event_id_size]u8 = undefined; 2471 io.random(&id_bytes); 2472 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2473 2474 const event = evt.EventWithId{ 2475 .id = event_id_hex, 2476 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2477 .author = author, 2478 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2479 .title = title, 2480 .description = description, 2481 .tags = tags, 2482 }), 2483 }; 2484 2485 switch (src.repo_kind) { 2486 inline else => |repo_kind| { 2487 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2488 defer any_repo.deinit(io, allocator); 2489 switch (any_repo) { 2490 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2491 } 2492 }, 2493 } 2494 2495 // wipe the form so a return visit starts fresh 2496 title_input.clear(allocator); 2497 tags_input.clear(allocator); 2498 description_input.clear(allocator); 2499 2500 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2501 try self.session.navigate(route); 2502 } 2503 2504 // re-emit the selected thread's event with the form's content (status 2505 // preserved), then reload the thread's page. this is the terminal path; 2506 // the web posts the form to the edit route. 2507 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2508 if (comptime wasm) return; 2509 const io = self.session.io orelse return; 2510 const entry = self.data.selectedThread() orelse return; 2511 2512 const form = self.threadForm() orelse return; 2513 const title_input = &form.children.values()[title_field_index].widget.text_input; 2514 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2515 const description_input = &form.children.values()[description_field_index].widget.text_input; 2516 2517 const title = try title_input.text(allocator); 2518 defer allocator.free(title); 2519 const tags = try tags_input.text(allocator); 2520 defer allocator.free(tags); 2521 const description = try description_input.text(allocator); 2522 defer allocator.free(description); 2523 2524 if (!Event.fieldsValid(title, tags)) return; 2525 2526 if (comptime supports_drafts) { 2527 if (entryDraft(entry.*)) { 2528 try self.data.editDraft(self.session, allocator, entry.id, title, tags, description); 2529 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2530 try self.session.navigate(route); 2531 return; 2532 } 2533 } 2534 2535 const id_bytes = try evt.parseEventId(entry.id); 2536 const src = self.data.repo_source orelse return; 2537 const author = (try self.session.eventAuthor()) orelse return; 2538 2539 switch (src.repo_kind) { 2540 inline else => |repo_kind| { 2541 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2542 defer any_repo.deinit(io, allocator); 2543 switch (any_repo) { 2544 inline else => |*repo| if (has_status) 2545 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2546 .title = title, 2547 .tags = tags, 2548 .description = description, 2549 } }, author) 2550 else 2551 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2552 } 2553 }, 2554 } 2555 2556 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2557 try self.session.navigate(route); 2558 } 2559 2560 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2561 switch (confirmation(self.data.view) orelse return) { 2562 .publish => if (comptime supports_drafts) { 2563 if (comptime wasm) return; 2564 const entry = self.data.selectedThread() orelse return; 2565 if (!entryDraft(entry.*)) return; 2566 try self.data.publishDraft(self.session, allocator, entry.id); 2567 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2568 try self.session.navigate(route); 2569 } else unreachable, 2570 .merge => if (comptime supports_merge) { 2571 if (comptime wasm) return; 2572 const entry = self.data.selectedThread() orelse return; 2573 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2574 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2575 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2576 try self.session.navigate(route); 2577 } else unreachable, 2578 .remove => try self.submitRemove(allocator), 2579 } 2580 } 2581 2582 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2583 const comment = self.data.comment_id.len != 0; 2584 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2585 const id = evt.parseEventId(event_id) catch return; 2586 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2587 } 2588 2589 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2590 if (comptime wasm) return; 2591 2592 if (comptime supports_drafts) { 2593 const selected = self.data.selectedThread() orelse return; 2594 if (event_kind == kind and entryDraft(selected.*)) { 2595 try Data.removeDraft(self.session, allocator, &id); 2596 const route = draftsRoute(self.data.identity) orelse return; 2597 try self.session.navigate(route); 2598 return; 2599 } 2600 } 2601 2602 const io = self.session.io orelse return; 2603 const src = self.data.repo_source orelse return; 2604 const author = (try self.session.eventAuthor()) orelse return; 2605 2606 switch (src.repo_kind) { 2607 inline else => |repo_kind| { 2608 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2609 defer any_repo.deinit(io, allocator); 2610 switch (any_repo) { 2611 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2612 } 2613 }, 2614 } 2615 2616 const id_hex = std.fmt.bytesToHex(id, .lower); 2617 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2618 try self.session.navigate(route); 2619 } 2620 2621 // return to the list. 2622 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2623 root_focus.setFocus(self.listScroll(index).getFocus().id); 2624 } 2625 2626 // cross to the header tabs above the stack. 2627 fn focusHeader(self: *This, root_focus: *Focus) void { 2628 root_focus.setFocus(self.box.children.keys()[header_index]); 2629 } 2630 2631 pub fn clearGrid(self: *This) void { 2632 self.box.clearGrid(); 2633 } 2634 2635 pub fn getGrid(self: This) ?Grid { 2636 return self.box.getGrid(); 2637 } 2638 2639 pub fn getFocus(self: *This) *Focus { 2640 return self.box.getFocus(); 2641 } 2642 2643 // for the parent's "scroll up at the top jumps to the header" check: at the 2644 // top only while the header tabs hold focus, so up from the split 2645 // crosses into the tabs first. 2646 pub fn getSelectedIndex(self: *This) ?usize { 2647 return if (self.headerActive()) 0 else 1; 2648 } 2649 }; 2650 } 2651