|
| 1 | +/** |
| 2 | + * Fork: the repository owner filter and filter chip parts of the pull request page |
| 3 | + * (`routes/_chat.pull-requests.tsx`). The route calls this module and keeps one line per hook. |
| 4 | + */ |
| 5 | +import type { ClientSettings, EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 6 | +import { useMemo } from "react"; |
| 7 | + |
| 8 | +import type { FilterOperator } from "../filters/FilterChip"; |
| 9 | +import type { PullRequestFilterChip } from "./pullRequestFilterChips.logic"; |
| 10 | +import { |
| 11 | + MAX_PULL_REQUEST_LIST_OWNERS, |
| 12 | + type PullRequestListPreferencePatch, |
| 13 | + type PullRequestListPreferences, |
| 14 | +} from "./pullRequestListPreferences"; |
| 15 | +import { |
| 16 | + type OwnedProject, |
| 17 | + type PullRequestOwnerFilter, |
| 18 | + projectsOfOwners, |
| 19 | + pullRequestMenuOwnerFilter, |
| 20 | + pullRequestOwnerRowFilter, |
| 21 | +} from "./pullRequestOwnerFilter.logic"; |
| 22 | + |
| 23 | +/** Owners are read like labels, then lowercased to match `repositoryOwnerOf`. */ |
| 24 | +export function pullRequestSearchOwners(labels: { |
| 25 | + readonly labels?: ReadonlyArray<string>; |
| 26 | +}): Partial<Pick<PullRequestListPreferences, "owners">> { |
| 27 | + const owners = labels.labels |
| 28 | + ?.map((owner) => owner.toLowerCase()) |
| 29 | + .slice(0, MAX_PULL_REQUEST_LIST_OWNERS); |
| 30 | + return owners === undefined ? {} : { owners }; |
| 31 | +} |
| 32 | + |
| 33 | +export const selectRepositoryOwnerFilters = (settings: ClientSettings) => |
| 34 | + settings.repositoryOwnerFilters; |
| 35 | +const NO_OWNERS: ReadonlyArray<string> = []; |
| 36 | +export const selectPullRequestFilterChips = (settings: ClientSettings) => |
| 37 | + settings.pullRequestFilterChips; |
| 38 | +export const NO_FILTER_CHIPS: ReadonlyArray<PullRequestFilterChip> = []; |
| 39 | + |
| 40 | +/** The search patch of an owner menu change. */ |
| 41 | +export const ownerMenuPatch = ( |
| 42 | + owners: ReadonlyArray<string>, |
| 43 | + operator: FilterOperator, |
| 44 | +): PullRequestListPreferencePatch => ({ |
| 45 | + owners: owners.length === 0 ? undefined : owners, |
| 46 | + ownerOperator: operator === "is_not" ? operator : undefined, |
| 47 | +}); |
| 48 | + |
| 49 | +export function usePullRequestOwnerFilter<Project extends OwnedProject>(input: { |
| 50 | + /** The `repositoryOwnerFilters` setting. */ |
| 51 | + readonly enabled: boolean; |
| 52 | + readonly search: Pick<PullRequestListPreferences, "owners" | "ownerOperator"> & { |
| 53 | + readonly projectId?: ProjectId | undefined; |
| 54 | + readonly environmentId?: EnvironmentId | undefined; |
| 55 | + }; |
| 56 | + readonly projects: ReadonlyArray<Project>; |
| 57 | + /** The owners of the typed and of the sent search text. */ |
| 58 | + readonly typedOwners: PullRequestOwnerFilter | undefined; |
| 59 | + readonly sentOwners: PullRequestOwnerFilter | undefined; |
| 60 | +}) { |
| 61 | + const { enabled, search, projects, typedOwners, sentOwners } = input; |
| 62 | + // The repository owners that narrow the list. A typed `org:` or `-org:` wins over the menu, |
| 63 | + // the same way the other typed qualifiers win. The request reads the sent text, and the rows |
| 64 | + // on screen read the typed text at once. |
| 65 | + const menuOwners = enabled ? (search.owners ?? NO_OWNERS) : NO_OWNERS; |
| 66 | + const menuOwnerOperator: FilterOperator = search.ownerOperator ?? "is"; |
| 67 | + const menuOwnerFilter = useMemo( |
| 68 | + () => pullRequestMenuOwnerFilter(menuOwners, menuOwnerOperator), |
| 69 | + [menuOwnerOperator, menuOwners], |
| 70 | + ); |
| 71 | + const requestOwners = sentOwners ?? menuOwnerFilter; |
| 72 | + const localOwners = typedOwners ?? menuOwnerFilter; |
| 73 | + // The projects that the list asks about. The project scope, the project menu and the |
| 74 | + // per-server split read these. The selection reads every project, because a link can open a |
| 75 | + // pull request from any owner. |
| 76 | + const listProjects = useMemo( |
| 77 | + () => projectsOfOwners(projects, requestOwners), |
| 78 | + [projects, requestOwners], |
| 79 | + ); |
| 80 | + const keepRequestOwnerRow = useMemo( |
| 81 | + () => pullRequestOwnerRowFilter(projects, requestOwners), |
| 82 | + [projects, requestOwners], |
| 83 | + ); |
| 84 | + const keepLocalOwnerRow = useMemo( |
| 85 | + () => |
| 86 | + localOwners === requestOwners |
| 87 | + ? keepRequestOwnerRow |
| 88 | + : pullRequestOwnerRowFilter(projects, localOwners), |
| 89 | + [keepRequestOwnerRow, localOwners, projects, requestOwners], |
| 90 | + ); |
| 91 | + // An owner change that leaves the scoped project out drops the project scope too, the same |
| 92 | + // way picking another server does. The owner menu and the owner chip both pass here. |
| 93 | + const withOwnerScope = ( |
| 94 | + patch: PullRequestListPreferencePatch, |
| 95 | + ): PullRequestListPreferencePatch => { |
| 96 | + if (!("owners" in patch) && !("ownerOperator" in patch)) return patch; |
| 97 | + const owners = "owners" in patch ? (patch.owners ?? NO_OWNERS) : menuOwners; |
| 98 | + const operator = "ownerOperator" in patch ? (patch.ownerOperator ?? "is") : menuOwnerOperator; |
| 99 | + const kept = projectsOfOwners(projects, pullRequestMenuOwnerFilter(owners, operator)); |
| 100 | + const keepsScope = |
| 101 | + search.projectId === undefined || |
| 102 | + kept.some( |
| 103 | + (project) => |
| 104 | + project.id === search.projectId && |
| 105 | + (search.environmentId === undefined || project.environmentId === search.environmentId), |
| 106 | + ); |
| 107 | + return keepsScope ? patch : { ...patch, projectId: undefined }; |
| 108 | + }; |
| 109 | + return { |
| 110 | + menuOwners, |
| 111 | + menuOwnerOperator, |
| 112 | + listProjects, |
| 113 | + keepRequestOwnerRow, |
| 114 | + keepLocalOwnerRow, |
| 115 | + withOwnerScope, |
| 116 | + }; |
| 117 | +} |
0 commit comments