From 97bea3e257210d702b8824bbf82b029800922ac9 Mon Sep 17 00:00:00 2001 From: Evan Purkhiser Date: Wed, 28 Aug 2024 12:27:04 -0400 Subject: [PATCH] ref(rr6): Vendor legacy react-router 3 types (#76618) This will help us rip out react router 3. --- static/app/types/legacyReactRouter.tsx | 132 +++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 static/app/types/legacyReactRouter.tsx diff --git a/static/app/types/legacyReactRouter.tsx b/static/app/types/legacyReactRouter.tsx new file mode 100644 index 00000000000000..f71e6eaca24105 --- /dev/null +++ b/static/app/types/legacyReactRouter.tsx @@ -0,0 +1,132 @@ +/** + * These are vendored from react-router v3 + * + * Once we've fully migrated to react-router 6 we can drop these types + */ +import type { + Href, + Location, + LocationDescriptor, + LocationState, + Path, + Pathname, + Query, +} from 'history'; + +export interface Params { + [key: string]: string; +} + +export type RoutePattern = string; +export type RouteComponent = React.ComponentClass | React.FunctionComponent; + +export interface RouteComponents { + [name: string]: RouteComponent; +} + +export interface RouterState { + components: RouteComponent[]; + location: Location; + params: Params; + routes: PlainRoute[]; +} + +export interface RedirectFunction { + (location: LocationDescriptor): void; + (state: LocationState, pathname: Pathname | Path, query?: Query): void; +} + +type AnyFunction = (...args: any[]) => any; + +export type EnterHook = ( + nextState: RouterState, + replace: RedirectFunction, + callback?: AnyFunction +) => any; + +export type LeaveHook = (prevState: RouterState) => any; + +export type ChangeHook = ( + prevState: RouterState, + nextState: RouterState, + replace: RedirectFunction, + callback?: AnyFunction +) => any; + +export type RouteHook = (nextLocation?: Location) => any; + +type ComponentCallback = (err: any, component: RouteComponent) => any; +type ComponentsCallback = (err: any, components: RouteComponents) => any; + +export interface IndexRouteProps { + component?: RouteComponent | undefined; + components?: RouteComponents | undefined; + getComponent?(nextState: RouterState, callback: ComponentCallback): void; + getComponents?(nextState: RouterState, callback: ComponentsCallback): void; + onChange?: ChangeHook | undefined; + onEnter?: EnterHook | undefined; + onLeave?: LeaveHook | undefined; + props?: Props | undefined; +} + +export interface RouteProps extends IndexRouteProps { + children?: React.ReactNode; + path?: RoutePattern | undefined; +} + +type RouteCallback = (err: any, route: PlainRoute) => void; +type RoutesCallback = (err: any, routesArray: PlainRoute[]) => void; + +export interface PlainRoute extends RouteProps { + childRoutes?: PlainRoute[] | undefined; + getChildRoutes?(partialNextState: LocationState, callback: RoutesCallback): void; + getIndexRoute?(partialNextState: LocationState, callback: RouteCallback): void; + indexRoute?: PlainRoute | undefined; +} + +export interface RouteComponentProps { + location: Location; + params: P & R; + route: PlainRoute; + routeParams: R; + router: InjectedRouter; + routes: PlainRoute[]; +} + +type LocationFunction = (location: LocationDescriptor) => void; +type GoFunction = (n: number) => void; +type NavigateFunction = () => void; +type ActiveFunction = (location: LocationDescriptor, indexOnly?: boolean) => boolean; +type LeaveHookFunction = (route: any, callback: RouteHook) => () => void; +type CreatePartFunction = (pathOrLoc: LocationDescriptor, query?: any) => Part; + +export interface InjectedRouter

, Q = any> { + createHref: CreatePartFunction; + createPath: CreatePartFunction; + go: GoFunction; + goBack: NavigateFunction; + goForward: NavigateFunction; + isActive: ActiveFunction; + location: Location; + params: P; + push: LocationFunction; + replace: LocationFunction; + routes: PlainRoute[]; + setRouteLeaveHook: LeaveHookFunction; +} + +export interface WithRouterProps

, Q = any> { + location: Location; + params: P; + router: InjectedRouter; + routes: PlainRoute[]; +} + +export interface RouteContextInterface

, Q = any> { + location: Location; + params: P; + router: InjectedRouter; + routes: PlainRoute[]; +} + +export type Route = React.ComponentClass;