GeezerVison is showing github.com — fix(web): keep repository lookup dismissed after going back (#43427) · pingdotgg/t3code@1099705
File treeExpand file treeCollapse file tree Open diff view settings mobile/src/features/projects
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/projects/AddProjectScreen.test.ts +106-5Lines changed: 106 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,7 +13,7 @@ import { | 13 | 13 | } from "@t3tools/contracts"; | 14 | 14 | import * as Cause from "effect/Cause"; | 15 | 15 | import { AsyncResult } from "effect/reactivity"; | 16 | | -import { act, createElement, isValidElement, type ReactNode } from "react"; | | 16 | +import { act, Children, createElement, isValidElement, type ReactNode } from "react"; | 17 | 17 | import { createRoot } from "react-dom/client"; | 18 | 18 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; | 19 | 19 |
| @@ -32,6 +32,7 @@ const state = vi.hoisted(() => ({ | 32 | 32 | createResult: null as Promise<unknown> | null, | 33 | 33 | createNewResult: null as Promise<unknown> | null, | 34 | 34 | startCloneResult: null as Promise<unknown> | null, | | 35 | + lookupRepository: vi.fn(), | 35 | 36 | connectionStates: {} as Record<string, EnvironmentConnectionPhase>, | 36 | 37 | newProjectsRoot: null as string | null, | 37 | 38 | waitForProject: vi.fn(), | @@ -76,6 +77,7 @@ vi.mock("react-native", () => ({ | 76 | 77 | View: "View", | 77 | 78 | })); | 78 | 79 | vi.mock("@react-navigation/native", () => ({ | | 80 | + useRoute: () => state.navigationState?.routes[state.navigationState.index], | 79 | 81 | useNavigation: () => ({ | 80 | 82 | dispatch: state.dispatch, | 81 | 83 | getState: () => | @@ -144,10 +146,14 @@ vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: (command: unkno | 144 | 146 | vi.mock("../../state/use-atom-query-runner", () => ({ | 145 | 147 | useAtomQueryRunner: | 146 | 148 | () => | 147 | | - async ({ input }: { input?: { partialPath?: string } }) => | 148 | | - input?.partialPath === "~" | 149 | | - ? AsyncResult.success({ parentPath: "/home/tester", entries: [] }) | 150 | | - : undefined, | | 149 | + ({ input }: { input?: { partialPath?: string; repository?: string } }) => | | 150 | + input?.repository | | 151 | + ? state.lookupRepository(input.repository) | | 152 | + : Promise.resolve( | | 153 | + input?.partialPath === "~" | | 154 | + ? AsyncResult.success({ parentPath: "/home/tester", entries: [] }) | | 155 | + : undefined, | | 156 | + ), | 151 | 157 | })); | 152 | 158 | vi.mock("../../state/query", () => ({ useEnvironmentQuery: () => ({ data: null }) })); | 153 | 159 | vi.mock("../../state/presentation", () => ({ | @@ -190,6 +196,7 @@ import { | 190 | 196 | AddProjectNewScreen, | 191 | 197 | AddProjectDestinationScreen, | 192 | 198 | AddProjectLocalFolderScreen, | | 199 | + AddProjectRepositoryScreen, | 193 | 200 | AddProjectSourceScreen, | 194 | 201 | } from "./AddProjectScreen"; | 195 | 202 | import { AddProjectSourceRoute } from "./AddProjectSourceRoute"; | @@ -240,6 +247,99 @@ function renderScreen(render: () => ReactNode) { | 240 | 247 | return screen; | 241 | 248 | } | 242 | 249 |
| | 250 | +it.each([ | | 251 | + { name: "Back after two submissions in one render", repeatSubmit: true, unmount: true }, | | 252 | + { name: "Back before the screen unmounts" }, | | 253 | + { name: "a change to the repository route parameters", changeParams: true }, | | 254 | + { name: "submitting from a render with stale route parameters", replaceBeforeSubmit: true }, | | 255 | +])( | | 256 | + "ignores a pending repository reply after $name", | | 257 | + async ({ repeatSubmit, unmount, changeParams, replaceBeforeSubmit }) => { | | 258 | + const repository = AsyncResult.success({ | | 259 | + provider: "github", | | 260 | + nameWithOwner: "example/slow", | | 261 | + url: "https://github.com/example/slow", | | 262 | + | | 263 | + }); | | 264 | + const lookup = Promise.withResolvers<typeof repository>(); | | 265 | + const duplicateLookup = Promise.withResolvers<typeof repository>(); | | 266 | + state.lookupRepository | | 267 | + .mockReset() | | 268 | + .mockReturnValueOnce(lookup.promise) | | 269 | + .mockReturnValue(duplicateLookup.promise); | | 270 | + state.dispatch.mockReset(); | | 271 | + state.environments = [{ environmentId: "environment", environmentLabel: "Environment" }]; | | 272 | + state.connectionStates = {}; | | 273 | + const flow = mountProjectFlow(false, false, false, "AddProjectRepository"); | | 274 | + flow.setParams({ environmentId: "environment", source: "github" }); | | 275 | + let screen: ReactNode; | | 276 | + function RepositoryScreen() { | | 277 | + screen = AddProjectRepositoryScreen({ environmentId: "environment", source: "github" }); | | 278 | + return null; | | 279 | + } | | 280 | + function withRepositoryInput( | | 281 | + node: ReactNode, | | 282 | + action: (input: { | | 283 | + onChangeText: (value: string) => void; | | 284 | + onSubmitEditing: () => void; | | 285 | + }) => void, | | 286 | + ): void { | | 287 | + Children.forEach(node, (child) => { | | 288 | + if ( | | 289 | + !isValidElement<{ | | 290 | + children?: ReactNode; | | 291 | + onChangeText?: (value: string) => void; | | 292 | + onSubmitEditing?: () => void; | | 293 | + }>(child) | | 294 | + ) { | | 295 | + return; | | 296 | + } | | 297 | + const { onChangeText, onSubmitEditing } = child.props; | | 298 | + if (onChangeText && onSubmitEditing) { | | 299 | + action({ onChangeText, onSubmitEditing }); | | 300 | + } else { | | 301 | + withRepositoryInput(child.props.children, action); | | 302 | + } | | 303 | + }); | | 304 | + } | | 305 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; | | 306 | + const root = createRoot(document.createElement("div")); | | 307 | + mountedRoots.push(root); | | 308 | + await act(() => root.render(createElement(RepositoryScreen))); | | 309 | + await act(() => withRepositoryInput(screen, (input) => input.onChangeText("example/slow"))); | | 310 | + const submit = findAction(screen, "Lookup repository"); | | 311 | + if (!submit) throw new Error("Repository lookup action missing"); | | 312 | + try { | | 313 | + if (replaceBeforeSubmit) flow.setParams({ source: "gitlab" }); | | 314 | + await act(() => { | | 315 | + if (repeatSubmit) { | | 316 | + withRepositoryInput(screen, (input) => input.onSubmitEditing()); | | 317 | + } | | 318 | + void submit(); | | 319 | + }); | | 320 | + if (!replaceBeforeSubmit) { | | 321 | + expect(state.lookupRepository).toHaveBeenCalledWith("example/slow"); | | 322 | + if (changeParams) { | | 323 | + flow.setParams({ source: "gitlab" }); | | 324 | + } else { | | 325 | + flow.back(); | | 326 | + } | | 327 | + } | | 328 | + if (unmount) await act(() => root.render(null)); | | 329 | + await act(async () => { | | 330 | + lookup.resolve(repository); | | 331 | + duplicateLookup.resolve(repository); | | 332 | + await lookup.promise; | | 333 | + }); | | 334 | + expect(state.dispatch).not.toHaveBeenCalled(); | | 335 | + expect(state.lookupRepository).toHaveBeenCalledTimes(replaceBeforeSubmit ? 0 : 1); | | 336 | + } finally { | | 337 | + state.navigationState = undefined; | | 338 | + state.dispatch.mockReset(); | | 339 | + } | | 340 | + }, | | 341 | +); | | 342 | + | 243 | 343 | function cloneAction() { | 244 | 344 | const action = findAction( | 245 | 345 | renderScreen(() => | @@ -340,6 +440,7 @@ function mountProjectFlow( | 340 | 440 | "PullRequestFile", | 341 | 441 | "NewTaskProject", | 342 | 442 | "AddProject", | | 443 | + "AddProjectRepository", | 343 | 444 | "AddProjectLocal", | 344 | 445 | "AddProjectNew", | 345 | 446 | "AddProjectDestination", | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/projects/AddProjectScreen.tsx +39-20Lines changed: 39 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,6 +65,7 @@ import { | 65 | 65 | import { | 66 | 66 | StackActions, | 67 | 67 | useNavigation, | | 68 | + useRoute, | 68 | 69 | type NavigationState, | 69 | 70 | type ParamListBase, | 70 | 71 | } from "@react-navigation/native"; | @@ -755,6 +756,20 @@ function getActiveRoute(navigation: { | 755 | 756 | return state?.routes[state.index]; | 756 | 757 | } | 757 | 758 |
| | 759 | +function isActiveRoute( | | 760 | + navigation: Parameters<typeof getActiveRoute>[0], | | 761 | + originRoute: ReturnType<typeof getActiveRoute>, | | 762 | +): boolean { | | 763 | + const activeRoute = getActiveRoute(navigation); | | 764 | + // A warm link can change the task without replacing its native route key. | | 765 | + return ( | | 766 | + originRoute !== undefined && | | 767 | + activeRoute?.key === originRoute.key && | | 768 | + activeRoute.name === originRoute.name && | | 769 | + activeRoute.params === originRoute.params | | 770 | + ); | | 771 | +} | | 772 | + | 758 | 773 | function useOpenNewTaskDraft() { | 759 | 774 | const navigation = useNavigation<NativeStackNavigationProp<ReactNavigation.RootParamList>>(); | 760 | 775 | const selectDetail = useSelectWorkspaceDetail(); | @@ -768,17 +783,7 @@ function useOpenNewTaskDraft() { | 768 | 783 | }, | 769 | 784 | originRoute: NavigationState<ParamListBase>["routes"][number] | undefined, | 770 | 785 | ) => { | 771 | | - const isStillOnOrigin = () => { | 772 | | - const activeRoute = getActiveRoute(navigation); | 773 | | - // A warm link can change the task without replacing its native route key. | 774 | | - return ( | 775 | | - originRoute !== undefined && | 776 | | - activeRoute?.key === originRoute.key && | 777 | | - activeRoute.name === originRoute.name && | 778 | | - activeRoute.params === originRoute.params | 779 | | - ); | 780 | | - }; | 781 | | - if (!isStillOnOrigin()) return; | | 786 | + if (!isActiveRoute(navigation, originRoute)) return; | 782 | 787 | const chooserDraft = | 783 | 788 | params.draftId === undefined | 784 | 789 | ? chooserDraftKey( | @@ -792,7 +797,7 @@ function useOpenNewTaskDraft() { | 792 | 797 | environmentId: params.environmentId, | 793 | 798 | projectId: params.projectId, | 794 | 799 | }).catch(() => false)); | 795 | | - if (!isStillOnOrigin()) return; | | 800 | + if (!isActiveRoute(navigation, originRoute)) return; | 796 | 801 | selectDetail({ | 797 | 802 | name: "Thread", | 798 | 803 | params: { draft: carried ? { ...params, draftId: chooserDraft } : params }, | @@ -962,16 +967,22 @@ export function AddProjectRepositoryScreen(props: { | 962 | 967 | reportFailure: false, | 963 | 968 | }); | 964 | 969 | const navigation = useNavigation(); | | 970 | + const originRoute = useRoute(); | 965 | 971 | const environment = useEnvironmentFromParam(props.environmentId); | 966 | 972 | const source = sourceFromParam(props.source); | 967 | 973 | const [repositoryInput, setRepositoryInput] = useState(""); | 968 | 974 | const [isSubmitting, setIsSubmitting] = useState(false); | 969 | 975 | const [error, setError] = useState<string | null>(null); | | 976 | + const pendingLookup = useRef<AbortController | null>(null); | 970 | 977 | const gt = useGT(); | 971 | 978 | const messages = useMessages(); | 972 | 979 |
| | 980 | + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- cancels the repository lookup when its screen unmounts | | 981 | + useEffect(() => () => pendingLookup.current?.abort(), []); | | 982 | + | 973 | 983 | const lookupRepository = async () => { | 974 | | - if (!environment || repositoryInput.trim().length === 0 || isSubmitting) return; | | 984 | + if (!environment || repositoryInput.trim().length === 0 || pendingLookup.current) return; | | 985 | + if (!isActiveRoute(navigation, originRoute)) return; | 975 | 986 | const incompleteOwnerMessage = githubIncompleteOwnerRepositoryMessage( | 976 | 987 | source, | 977 | 988 | repositoryInput, | @@ -999,13 +1010,22 @@ export function AddProjectRepositoryScreen(props: { | 999 | 1010 | return; | 1000 | 1011 | } | 1001 | 1012 |
| 1002 | | - const result = await lookupRepositoryQuery({ | 1003 | | - environmentId: environment.environmentId, | 1004 | | - input: { | 1005 | | - provider, | 1006 | | - repository: repositoryInput.trim(), | | 1013 | + const lookup = new AbortController(); | | 1014 | + pendingLookup.current = lookup; | | 1015 | + const result = await lookupRepositoryQuery( | | 1016 | + { | | 1017 | + environmentId: environment.environmentId, | | 1018 | + input: { | | 1019 | + provider, | | 1020 | + repository: repositoryInput.trim(), | | 1021 | + }, | 1007 | 1022 | }, | 1008 | | - }); | | 1023 | + { signal: lookup.signal }, | | 1024 | + ); | | 1025 | + if (lookup.signal.aborted) return; | | 1026 | + pendingLookup.current = null; | | 1027 | + setIsSubmitting(false); | | 1028 | + if (!isActiveRoute(navigation, originRoute)) return; | 1009 | 1029 | if (AsyncResult.isFailure(result)) { | 1010 | 1030 | setError(errorMessage(Cause.squash(result.cause))); | 1011 | 1031 | } else { | @@ -1020,7 +1040,6 @@ export function AddProjectRepositoryScreen(props: { | 1020 | 1040 | }), | 1021 | 1041 | ); | 1022 | 1042 | } | 1023 | | - setIsSubmitting(false); | 1024 | 1043 | }; | 1025 | 1044 |
| 1026 | 1045 | return ( | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/CommandPalette.composition.browser.test.tsx +35-1Lines changed: 35 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,10 +7,13 @@ import { act, render } from "../../test/render"; | 7 | 7 | import { CommandPalette } from "./CommandPalette"; | 8 | 8 | import { openCommandPalette } from "../commandPaletteBus"; | 9 | 9 | import { primaryServerKeybindingsAtom } from "../state/server"; | | 10 | +import { sourceControlEnvironment } from "../state/sourceControl"; | | 11 | +import { AsyncResult } from "effect/reactivity"; | 10 | 12 |
| 11 | 13 | const scopes = vi.hoisted(() => ({ | 12 | 14 | denied: undefined as string | undefined, | 13 | 15 | sourceControl: undefined as unknown, | | 16 | + lookupRepository: vi.fn(), | 14 | 17 | })); | 15 | 18 | const setup = (deniedScope?: string, sourceControl?: unknown) => { | 16 | 19 | scopes.denied = deniedScope; | @@ -73,7 +76,10 @@ vi.mock("../state/use-atom-command", () => ({ | 73 | 76 | })); | 74 | 77 | vi.mock("../state/use-atom-query-runner", () => ({ | 75 | 78 | // Doubles as the add-project folder scan, which finds nothing to track. | 76 | | - useAtomQueryRunner: () => async () => ({ _tag: "Success", value: { folders: [] } }), | | 79 | + useAtomQueryRunner: (family: unknown) => | | 80 | + family === sourceControlEnvironment.repository | | 81 | + ? scopes.lookupRepository | | 82 | + : async () => ({ _tag: "Success", value: { folders: [] } }), | 77 | 83 | })); | 78 | 84 | vi.mock("../state/environments", async (original) => ({ | 79 | 85 | ...(await original<typeof import("../state/environments")>()), | @@ -244,3 +250,31 @@ it("tells screen readers whether a new project will be published", async () => { | 244 | 250 | await option.click(); | 245 | 251 | await expect.element(option).toHaveAttribute("aria-checked", "true"); | 246 | 252 | }); | | 253 | + | | 254 | +it("keeps the source list visible after a dismissed repository lookup succeeds", async () => { | | 255 | + setup(undefined, githubAvailable); | | 256 | + const repository = AsyncResult.success({ | | 257 | + provider: "github", | | 258 | + nameWithOwner: "example/slow", | | 259 | + url: "https://github.com/example/slow", | | 260 | + | | 261 | + }); | | 262 | + const lookup = Promise.withResolvers<typeof repository>(); | | 263 | + scopes.lookupRepository.mockReset().mockReturnValue(lookup.promise); | | 264 | + await using _screen = await render(<CommandPalette>{null}</CommandPalette>); | | 265 | + await act(() => openCommandPalette({ open: "add-project" })); | | 266 | + await page.getByRole("option", { name: /^GitHub repository/ }).click(); | | 267 | + await page.getByRole("combobox").fill("example/slow"); | | 268 | + await userEvent.keyboard("{Enter}"); | | 269 | + expect(scopes.lookupRepository).toHaveBeenCalledOnce(); | | 270 | + await page.getByRole("combobox").fill(""); | | 271 | + await userEvent.keyboard("{Backspace}"); | | 272 | + const sources = page.getByText("Git URL", { exact: true }); | | 273 | + await expect.element(sources).toBeVisible(); | | 274 | + | | 275 | + await act(async () => { | | 276 | + lookup.resolve(repository); | | 277 | + await lookup.promise; | | 278 | + }); | | 279 | + expect(sources.element()).toBeVisible(); | | 280 | +}); |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/CommandPalette.tsx +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1021,8 +1021,10 @@ function OpenCommandPaletteDialog(props: { | 1021 | 1021 | setHighlightedItemValue(null); | 1022 | 1022 | }); | 1023 | 1023 |
| 1024 | | - function popView(): void { | | 1024 | + const popView = useStableCallback((): void => { | 1025 | 1025 | browseNavigation.invalidate(); | | 1026 | + cloneLookupGeneration.current += 1; | | 1027 | + setIsRemoteProjectLookingUp(false); | 1026 | 1028 | setAddProjectCloneFlow(null); | 1027 | 1029 | setNewProjectFlow(null); | 1028 | 1030 | if (viewStack.length <= 1) { | @@ -1032,7 +1034,7 @@ function OpenCommandPaletteDialog(props: { | 1032 | 1034 | } | 1033 | 1035 | dispatchSearch({ type: "back" }); | 1034 | 1036 | setHighlightedItemValue(null); | 1035 | | - } | | 1037 | + }); | 1036 | 1038 |
| 1037 | 1039 | function handleQueryChange(nextQuery: string, source: EventTarget | null): void { | 1038 | 1040 | browseNavigation.invalidate(); | |
|