╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2220 else 2221 self.focusHeader(root_focus), 2222 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2223 .enter => try self.submitConfirmation(allocator, index), 2224 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2225 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2226 try self.submitConfirmation(allocator, button_index); 2227 break; 2228 } 2229 }, 2230 else => {}, 2231 } 2232 } 2233 2234 // the multiline hunk inputs keep enter and any up/down that has a row to 2235 // move to; enter on a "use this" link is a navigation the host follows 2236 // before this runs. 2237 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2238 const form = self.threadForm() orelse return; 2239 const cid = form.getFocus().child_id orelse return; 2240 const cur = form.children.getIndex(cid) orelse return; 2241 const child = &form.children.values()[cur]; 2242 2243 if (child.widget == .text_input) { 2244 const ti = &child.widget.text_input; 2245 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2246 return child.widget.input(allocator, key, root_focus); 2247 } 2248 2249 const on_submit = child.widget == .submit_button; 2250 2251 switch (key) { 2252 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2253 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2254 .arrow_left, .arrow_right => switch (child.widget) { 2255 .box => |*row| { 2256 const row_cid = row.getFocus().child_id orelse return; 2257 const row_index = row.children.getIndex(row_cid) orelse return; 2258 if (key == .arrow_left) { 2259 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2260 } else if (row_index + 1 < row.children.count()) { 2261 root_focus.setFocus(row.children.keys()[row_index + 1]); 2262 } 2263 }, 2264 else => try child.widget.input(allocator, key, root_focus), 2265 }, 2266 .enter => if (on_submit) try self.submitResolution(allocator), 2267 .mouse => |mouse| if (on_submit) { 2268 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2269 }, 2270 else => try child.widget.input(allocator, key, root_focus), 2271 } 2272 } 2273 2274 // a multiline input keeps enter and any up/down that has a row to move to 2275 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2276 return switch (key) { 2277 .enter => true, 2278 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2279 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2280 else => false, 2281 }; 2282 } 2283 2284 // the neighboring focusable resolve-form child, skipping the spacer and 2285 // the blank gap rows 2286 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2287 var i = cur; 2288 while (true) { 2289 if (down) { 2290 i += 1; 2291 if (i >= form.children.count()) return null; 2292 } else { 2293 if (i == 0) return null; 2294 i -= 1; 2295 } 2296 switch (form.children.values()[i].widget) { 2297 .spacer, .text => continue, 2298 else => return i, 2299 } 2300 } 2301 } 2302 2303 // focus the resolve-form child at `i` (a version row focuses its selected 2304 // child) and keep it visible on the terminal 2305 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2306 const child = &form.children.values()[i]; 2307 switch (child.widget) { 2308 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2309 else => root_focus.setFocus(form.children.keys()[i]), 2310 } 2311 if (self.session.is_terminal) { 2312 if (self.resolveScroll()) |sc| { 2313 if (child.rect) |rect| sc.scrollToRect(rect); 2314 } 2315 } 2316 } 2317 2318 // re-emit the conflicted thread's event with the resolved content; any 2319 // event on the thread settles its conflict entry. this is the terminal 2320 // path; the web posts the form to the resolve route. 2321 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2322 if (comptime wasm) return; 2323 const io = self.session.io orelse return; 2324 const src = self.data.repo_source orelse return; 2325 const entry = self.data.selectedThread() orelse return; 2326 const author = (try self.session.eventAuthor()) orelse return; 2327 const form = self.threadForm() orelse return; 2328 2329 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2330 var title: ?[]u8 = null; 2331 var tags: ?[]u8 = null; 2332 var hunks: std.ArrayList([]const u8) = .empty; 2333 defer { 2334 if (title) |t| allocator.free(t); 2335 if (tags) |t| allocator.free(t); 2336 for (hunks.items) |h| allocator.free(h); 2337 hunks.deinit(allocator); 2338 } 2339 for (form.children.values()) |*child| switch (child.widget) { 2340 .text_input => |*ti| { 2341 const text = try ti.text(allocator); 2342 errdefer allocator.free(text); 2343 if (std.mem.eql(u8, ti.options.name, "title")) { 2344 title = text; 2345 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2346 tags = text; 2347 } else { 2348 try hunks.append(allocator, text); 2349 } 2350 }, 2351 else => {}, 2352 }; 2353 2354 const id_bytes = try evt.parseEventId(entry.id); 2355 2356 switch (src.repo_kind) { 2357 inline else => |repo_kind| { 2358 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2359 defer any_repo.deinit(io, allocator); 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