╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2234 const submit = &form.children.values()[cur].widget.submit_button; 2235 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2236 }, 2237 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2238 } 2239 } 2240 2241 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2242 const form = self.formBox() orelse return; 2243 const cid = form.getFocus().child_id orelse return; 2244 const cur = form.children.getIndex(cid) orelse return; 2245 const child = &form.children.values()[cur]; 2246 2247 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2248 return child.widget.input(allocator, key, root_focus); 2249 2250 switch (key) { 2251 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2252 root_focus.setFocus(form.children.keys()[cur - 1]) 2253 else 2254 self.focusHeader(root_focus), 2255 .arrow_down, .tab => if (cur < comment_submit_field_index) 2256 root_focus.setFocus(form.children.keys()[cur + 1]), 2257 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2258 .mouse => |mouse| if (cur == comment_submit_field_index) { 2259 const submit = &child.widget.submit_button; 2260 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2261 }, 2262 else => try child.widget.input(allocator, key, root_focus), 2263 } 2264 } 2265 2266 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2267 const form = self.confirmationForm() orelse return; 2268 const child_id = form.getFocus().child_id orelse return; 2269 const index = form.children.getIndex(child_id) orelse return; 2270 switch (key) { 2271 .arrow_up, .back_tab => if (index > 1) 2272 root_focus.setFocus(form.children.keys()[index - 1]) 2273 else 2274 self.focusHeader(root_focus), 2275 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2276 .enter => try self.submitConfirmation(allocator, index), 2277 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2278 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2279 try self.submitConfirmation(allocator, button_index); 2280 break; 2281 } 2282 }, 2283 else => {}, 2284 } 2285 } 2286 2287 // the multiline hunk inputs keep enter and any up/down that has a row to 2288 // move to; enter on a "use this" link is a navigation the host follows 2289 // before this runs. 2290 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2291 const form = self.formBox() orelse return; 2292 const cid = form.getFocus().child_id orelse return; 2293 const cur = form.children.getIndex(cid) orelse return; 2294 const child = &form.children.values()[cur]; 2295 2296 if (child.widget == .text_input) { 2297 const ti = &child.widget.text_input; 2298 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2299 return child.widget.input(allocator, key, root_focus); 2300 } 2301 2302 const on_submit = child.widget == .submit_button; 2303 2304 switch (key) { 2305 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2306 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2307 .arrow_left, .arrow_right => switch (child.widget) { 2308 .box => |*row| { 2309 const row_cid = row.getFocus().child_id orelse return; 2310 const row_index = row.children.getIndex(row_cid) orelse return; 2311 if (key == .arrow_left) { 2312 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2313 } else if (row_index + 1 < row.children.count()) { 2314 root_focus.setFocus(row.children.keys()[row_index + 1]); 2315 } 2316 }, 2317 else => try child.widget.input(allocator, key, root_focus), 2318 }, 2319 .enter => if (on_submit) try self.submitResolution(allocator), 2320 .mouse => |mouse| if (on_submit) { 2321 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2322 }, 2323 else => try child.widget.input(allocator, key, root_focus), 2324 } 2325 } 2326 2327 // a multiline input keeps enter and any up/down that has a row to move to 2328 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2329 return switch (key) { 2330 .enter => true, 2331 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2332 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2333 else => false, 2334 }; 2335 } 2336 2337 // the neighboring focusable resolve-form child, skipping the spacer and 2338 // the blank gap rows 2339 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2340 var i = cur; 2341 while (true) { 2342 if (down) { 2343 i += 1; 2344 if (i >= form.children.count()) return null; 2345 } else { 2346 if (i == 0) return null; 2347 i -= 1; 2348 } 2349 switch (form.children.values()[i].widget) { 2350 .spacer, .text => continue, 2351 else => return i, 2352 } 2353 } 2354 } 2355 2356 // focus the resolve-form child at `i` (a version row focuses its selected 2357 // child) and keep it visible on the terminal 2358 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2359 const child = &form.children.values()[i]; 2360 switch (child.widget) { 2361 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2362 else => root_focus.setFocus(form.children.keys()[i]), 2363 } 2364 if (self.session.is_terminal) { 2365 if (self.resolveScroll()) |sc| { 2366 if (child.rect) |rect| sc.scrollToRect(rect); 2367 } 2368 } 2369 } 2370 2371 // re-emit the conflicted thread's event with the resolved content; any 2372 // event on the thread settles its conflict entry. this is the terminal 2373 // path; the web posts the form to the resolve route. 2374 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2375 if (comptime wasm) return; 2376 const io = self.session.io orelse return; 2377 const src = self.data.repo_source orelse return; 2378 const entry = self.data.selectedThread() orelse return; 2379 const author = (try self.session.eventAuthor()) orelse return; 2380 const form = self.formBox() orelse return; 2381 2382 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2383 var title: ?[]u8 = null; 2384 var tags: ?[]u8 = null; 2385 var hunks: std.ArrayList([]const u8) = .empty; 2386 defer { 2387 if (title) |t| allocator.free(t); 2388 if (tags) |t| allocator.free(t); 2389 for (hunks.items) |h| allocator.free(h); 2390 hunks.deinit(allocator); 2391 } 2392 for (form.children.values()) |*child| switch (child.widget) { 2393 .text_input => |*ti| { 2394 const text = try ti.text(allocator); 2395 errdefer allocator.free(text); 2396 if (std.mem.eql(u8, ti.options.name, "title")) { 2397 title = text; 2398 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2399 tags = text; 2400 } else { 2401 try hunks.append(allocator, text); 2402 } 2403 }, 2404 else => {}, 2405 }; 2406 2407 const id_bytes = try evt.parseEventId(entry.id); 2408 2409 switch (src.repo_kind) { 2410 inline else => |repo_kind| { 2411 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2412 defer any_repo.deinit(io, allocator); 2413 switch (any_repo) { 2414 inline else => |*repo| { 2415 var resolution: Event.Resolve = .{ 2416 .title = title, 2417 .tags = tags, 2418 .hunks = hunks.items, 2419 }; 2420 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2421 const change = Event.Update{ .resolve = resolution }; 2422 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2423 // leave the form up for correction 2424 error.InvalidFields => return, 2425 else => |e| return e, 2426 }; 2427 }, 2428 } 2429 }, 2430 } 2431 2432 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2433 try self.session.navigate(route); 2434 } 2435 2436 // commit a new or edited comment and navigate to its permalink. this is the 2437 // terminal path; the web posts the form to its comment route. 2438 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2439 if (comptime wasm) return; 2440 const io = self.session.io orelse return; 2441 const src = self.data.repo_source orelse return; 2442 const author = (try self.session.eventAuthor()) orelse return; 2443 const form = self.formBox() orelse return; 2444 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2445 const body = try body_input.text(allocator); 2446 defer allocator.free(body); 2447 if (!evt.Comment.fieldsValid(body)) return; 2448 2449 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2450 @memcpy(&thread_id, self.data.selected_id); 2451 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2452 var parent: [evt.event_id_size * 2]u8 = undefined; 2453 @memcpy(&parent, self.data.comment_id); 2454 break :blk parent; 2455 }; 2456 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2457 const editing = self.data.view == .edit_comment; 2458 switch (src.repo_kind) { 2459 inline else => |repo_kind| { 2460 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2461 defer any_repo.deinit(io, allocator); 2462 switch (any_repo) { 2463 inline else => |*repo| if (editing) { 2464 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2465 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2466 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2467 } else { 2468 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2469 }, 2470 } 2471 }, 2472 } 2473 2474 body_input.clear(allocator); 2475 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2476 try self.session.navigate(route); 2477 } 2478 2479 // the terminal submit paths: the new form commits a new thread event, the 2480 // edit form re-emits the selected thread's event with the form's content. 2481 // the web posts the forms to their routes instead. 2482 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2483 if (self.data.view == .edit) { 2484 try self.submitEditedThread(allocator); 2485 } else { 2486 try self.submitNewThread(allocator); 2487 } 2488 } 2489 2490 // commit the new thread to the repo's events branch and navigate to it. 2491 // this is the terminal path; the web posts the form to the thread route, 2492 // so the wasm side never runs (or compiles) the repo access below. 2493 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2494 if (comptime wasm) return; 2495 const io = self.session.io orelse return; 2496 const src = self.data.repo_source orelse return; 2497 const author = (try self.session.eventAuthor()) orelse return; 2498 2499 const form = self.threadForm() orelse return; 2500 const title_input = &form.children.values()[title_field_index].widget.text_input; 2501 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2502 const description_input = &form.children.values()[description_field_index].widget.text_input; 2503 2504 const title = try title_input.text(allocator); 2505 defer allocator.free(title); 2506 const tags = try tags_input.text(allocator); 2507 defer allocator.free(tags); 2508 const description = try description_input.text(allocator); 2509 defer allocator.free(description); 2510 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2511 defer if (comptime supports_drafts) allocator.free(target_branch); 2512 2513 if (!Event.fieldsValid(title, tags)) { 2514 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2515 return; 2516 } 2517 2518 if (comptime supports_drafts) { 2519 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2520 error.InvalidFields => { 2521 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2522 return; 2523 }, 2524 else => |other| return other, 2525 }; 2526 title_input.clear(allocator); 2527 tags_input.clear(allocator); 2528 description_input.clear(allocator); 2529 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2530 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2531 try self.session.navigate(route); 2532 return; 2533 } 2534 2535 var id_bytes: [evt.event_id_size]u8 = undefined; 2536 io.random(&id_bytes); 2537 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2538 2539 const event = evt.EventWithId{ 2540 .id = event_id_hex, 2541 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2542 .author = author, 2543 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2544 .title = title, 2545 .description = description, 2546 .tags = tags, 2547 }), 2548 }; 2549 2550 switch (src.repo_kind) { 2551 inline else => |repo_kind| { 2552 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2553 defer any_repo.deinit(io, allocator); 2554 switch (any_repo) { 2555 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2556 } 2557 }, 2558 } 2559 2560 // wipe the form so a return visit starts fresh 2561 title_input.clear(allocator); 2562 tags_input.clear(allocator); 2563 description_input.clear(allocator); 2564 2565 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2566 try self.session.navigate(route); 2567 } 2568 2569 // re-emit the selected thread's event with the form's content (status 2570 // preserved), then reload the thread's page. this is the terminal path; 2571 // the web posts the form to the edit route. 2572 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2573 if (comptime wasm) return; 2574 const io = self.session.io orelse return; 2575 const entry = self.data.selectedThread() orelse return; 2576 2577 const form = self.threadForm() orelse return; 2578 const title_input = &form.children.values()[title_field_index].widget.text_input; 2579 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2580 const description_input = &form.children.values()[description_field_index].widget.text_input; 2581 2582 const title = try title_input.text(allocator); 2583 defer allocator.free(title); 2584 const tags = try tags_input.text(allocator); 2585 defer allocator.free(tags); 2586 const description = try description_input.text(allocator); 2587 defer allocator.free(description); 2588 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2589 defer if (comptime supports_drafts) allocator.free(target_branch); 2590 2591 if (!Event.fieldsValid(title, tags)) { 2592 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2593 return; 2594 } 2595 const src = self.data.repo_source orelse return; 2596 2597 if (comptime supports_drafts) { 2598 if (entryDraft(entry.*)) { 2599 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2600 error.InvalidFields => { 2601 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2602 return; 2603 }, 2604 else => |other| return other, 2605 }; 2606 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2607 try self.session.navigate(route); 2608 return; 2609 } 2610 } 2611 2612 const id_bytes = try evt.parseEventId(entry.id); 2613 const author = (try self.session.eventAuthor()) orelse return; 2614 2615 switch (src.repo_kind) { 2616 inline else => |repo_kind| { 2617 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2618 defer any_repo.deinit(io, allocator); 2619 switch (any_repo) { 2620 inline else => |*repo| if (supports_drafts) { 2621 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2622 .title = title, 2623 .tags = tags, 2624 .description = description, 2625 .target_branch = target_branch, 2626 } }, author) catch |err| switch (err) { 2627 error.InvalidFields => { 2628 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2629 return; 2630 }, 2631 else => |other| return other, 2632 }; 2633 } else if (has_status) 2634 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2635 .title = title, 2636 .tags = tags, 2637 .description = description, 2638 } }, author) 2639 else 2640 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2641 } 2642 }, 2643 } 2644 2645 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2646 try self.session.navigate(route); 2647 } 2648 2649 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2650 const aa = self.session.arena.allocator(); 2651 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2652 .failure = failure, 2653 .fields = if (comptime supports_drafts) .{ 2654 .title = try aa.dupe(u8, title), 2655 .tags = try aa.dupe(u8, tags), 2656 .description = try aa.dupe(u8, description), 2657 .target_branch = try aa.dupe(u8, target_branch), 2658 } else .{ 2659 .title = try aa.dupe(u8, title), 2660 .tags = try aa.dupe(u8, tags), 2661 .description = try aa.dupe(u8, description), 2662 }, 2663 }); 2664 } 2665 2666 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2667 switch (confirmation(self.data.view) orelse return) { 2668 .publish => if (comptime supports_drafts) { 2669 if (comptime wasm) return; 2670 const entry = self.data.selectedThread() orelse return; 2671 if (!entryDraft(entry.*)) return; 2672 try self.data.publishDraft(self.session, allocator, entry.id); 2673 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2674 try self.session.navigate(route); 2675 } else unreachable, 2676 .merge => if (comptime supports_merge) { 2677 if (comptime wasm) return; 2678 const entry = self.data.selectedThread() orelse return; 2679 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2680 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2681 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2682 try self.session.navigate(route); 2683 } else unreachable, 2684 .remove => try self.submitRemove(allocator), 2685 } 2686 } 2687 2688 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2689 const comment = self.data.comment_id.len != 0; 2690 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2691 const id = evt.parseEventId(event_id) catch return; 2692 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2693 } 2694 2695 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2696 if (comptime wasm) return; 2697 2698 if (comptime supports_drafts) { 2699 const selected = self.data.selectedThread() orelse return; 2700 if (event_kind == kind and entryDraft(selected.*)) { 2701 try Data.removeDraft(self.session, allocator, &id); 2702 const route = draftsRoute(self.data.identity) orelse return; 2703 try self.session.navigate(route); 2704 return; 2705 } 2706 } 2707 2708 const io = self.session.io orelse return; 2709 const src = self.data.repo_source orelse return; 2710 const author = (try self.session.eventAuthor()) orelse return; 2711 2712 switch (src.repo_kind) { 2713 inline else => |repo_kind| { 2714 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2715 defer any_repo.deinit(io, allocator); 2716 switch (any_repo) { 2717 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2718 } 2719 }, 2720 } 2721 2722 const id_hex = std.fmt.bytesToHex(id, .lower); 2723 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2724 try self.session.navigate(route); 2725 } 2726 2727 // return to the list. 2728 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2729 root_focus.setFocus(self.listScroll(index).getFocus().id); 2730 } 2731 2732 // cross to the header tabs above the stack. 2733 fn focusHeader(self: *This, root_focus: *Focus) void { 2734 root_focus.setFocus(self.box.children.keys()[header_index]); 2735 } 2736 2737 pub fn clearGrid(self: *This) void { 2738 self.box.clearGrid(); 2739 } 2740 2741 pub fn getGrid(self: This) ?Grid { 2742 return self.box.getGrid(); 2743 } 2744 2745 pub fn getFocus(self: *This) *Focus { 2746 return self.box.getFocus(); 2747 } 2748 2749 // for the parent's "scroll up at the top jumps to the header" check: at the 2750 // top only while the header tabs hold focus, so up from the split 2751 // crosses into the tabs first. 2752 pub fn getSelectedIndex(self: *This) ?usize { 2753 return if (self.headerActive()) 0 else 1; 2754 } 2755 }; 2756 } 2757