The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ThreadPullRequestKey, ThreadPullRequestLink } from "@t3tools/contracts"; |
| 2 | |
| 3 | import { visibleThreadPullRequests } from "./threadPullRequests.ts"; |
| 4 | |
| 5 | export interface ComposerPullRequestMatch extends ThreadPullRequestKey { |
| 6 | readonly projectId: string; |
| 7 | readonly updatedAt: string; |
| 8 | } |
| 9 | |
| 10 | export function composerPullRequestsFromLinks( |
| 11 | links: ReadonlyArray<ThreadPullRequestLink>, |
| 12 | projectId: string, |
| 13 | ) { |
| 14 | return visibleThreadPullRequests(links).flatMap((link) => |
| 15 | link.snapshot === null |
| 16 | ? [] |
| 17 | : [ |
| 18 | { |
| 19 | projectId, |
| 20 | host: link.host, |
| 21 | repository: link.repository, |
| 22 | number: link.number, |
| 23 | url: link.url, |
| 24 | title: link.snapshot.title, |
| 25 | headBranch: link.snapshot.headBranch, |
| 26 | baseBranch: link.snapshot.baseBranch, |
| 27 | state: link.snapshot.state, |
| 28 | isDraft: link.snapshot.isDraft, |
| 29 | updatedAt: link.snapshot.updatedAt ?? link.linkedAt, |
| 30 | }, |
| 31 | ], |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | export function filterComposerPullRequestMatches< |
| 36 | Entry extends ComposerPullRequestMatch, |
| 37 | Linked extends ComposerPullRequestMatch = never, |
| 38 | >(input: { |
| 39 | readonly entries: ReadonlyArray<Entry>; |
| 40 | readonly linked?: ReadonlyArray<Linked>; |
| 41 | readonly projectId: string; |
| 42 | readonly repository: string; |
| 43 | readonly query: string; |
| 44 | readonly limit: number; |
| 45 | readonly keyOf: (entry: Entry | Linked) => string; |
| 46 | }): ReadonlyArray<Entry | Linked> { |
| 47 | const repository = input.repository.trim().toLowerCase(); |
| 48 | const { keyOf } = input; |
| 49 | const linked = (input.linked ?? []).filter((entry) => String(entry.number).includes(input.query)); |
| 50 | const linkedKeys = new Set(linked.map(keyOf)); |
| 51 | const matchingEntries = input.entries.filter( |
| 52 | (entry) => |
| 53 | entry.projectId === input.projectId && |
| 54 | entry.repository.trim().toLowerCase() === repository && |
| 55 | String(entry.number).includes(input.query), |
| 56 | ); |
| 57 | const uniqueEntries = new Map<string, Entry | Linked>(); |
| 58 | for (const entry of [...matchingEntries, ...linked]) { |
| 59 | if (!uniqueEntries.has(keyOf(entry))) { |
| 60 | uniqueEntries.set(keyOf(entry), entry); |
| 61 | } |
| 62 | } |
| 63 | const isExactMatch = (entry: ComposerPullRequestMatch) => String(entry.number) === input.query; |
| 64 | const isLinked = (entry: Entry | Linked) => linkedKeys.has(keyOf(entry)); |
| 65 | const updatedAtMs = (entry: ComposerPullRequestMatch) => Date.parse(entry.updatedAt) || 0; |
| 66 | // Hermes has no ES2023 array methods, so sort a fresh array. |
| 67 | return [...uniqueEntries.values()] |
| 68 | .sort( |
| 69 | (left, right) => |
| 70 | Number(isExactMatch(right)) - Number(isExactMatch(left)) || |
| 71 | Number(isLinked(right)) - Number(isLinked(left)) || |
| 72 | updatedAtMs(right) - updatedAtMs(left), |
| 73 | ) |
| 74 | .slice(0, input.limit); |
| 75 | } |
| 76 | |
| 77 | /** Puts the thread's linked pull requests that match a text query ahead of the searched ones. */ |
| 78 | export function withLinkedComposerPullRequestsFirst< |
| 79 | Entry extends ComposerPullRequestMatch, |
| 80 | Linked extends ComposerPullRequestMatch & { |
| 81 | readonly title: string; |
| 82 | readonly headBranch: string; |
| 83 | }, |
| 84 | >(input: { |
| 85 | readonly matches: ReadonlyArray<Entry>; |
| 86 | readonly linked: ReadonlyArray<Linked>; |
| 87 | readonly query: string; |
| 88 | readonly limit: number; |
| 89 | readonly keyOf: (entry: Entry | Linked) => string; |
| 90 | }): ReadonlyArray<Entry | Linked> { |
| 91 | const { keyOf } = input; |
| 92 | const matchesByKey = new Map(input.matches.map((entry) => [keyOf(entry), entry])); |
| 93 | const words = input.query.toLowerCase().split(/\s+/u).filter(Boolean); |
| 94 | const linked = input.linked.flatMap((entry): Array<Entry | Linked> => { |
| 95 | const match = matchesByKey.get(keyOf(entry)); |
| 96 | if (match !== undefined) return [match]; |
| 97 | const text = |
| 98 | `#${entry.number} ${entry.title} ${entry.repository} ${entry.headBranch}`.toLowerCase(); |
| 99 | return words.every((word) => text.includes(word)) ? [entry] : []; |
| 100 | }); |
| 101 | const linkedKeys = new Set(linked.map(keyOf)); |
| 102 | return [...linked, ...input.matches.filter((entry) => !linkedKeys.has(keyOf(entry)))].slice( |
| 103 | 0, |
| 104 | input.limit, |
| 105 | ); |
| 106 | } |