Skip to content

Commit

Permalink
Wipe more FlowRouter direct references
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Jul 4, 2023
1 parent 559e83b commit 0ebfc0e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 51 deletions.
22 changes: 18 additions & 4 deletions apps/meteor/client/lib/createRouteGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import type { IRouterPaths, RouterPathPattern } from '@rocket.chat/ui-contexts';
import type { GroupName, GroupPrefix, RouteNamesOf, TrimPrefix } from 'meteor/kadira:flow-router';
import type { ElementType, ReactNode } from 'react';
import React from 'react';
import { type IRouterPaths, type RouteName, type RouterPathPattern } from '@rocket.chat/ui-contexts';
import React, { type ElementType, type ReactNode } from 'react';

import { router } from '../providers/RouterProvider';
import MainLayout from '../views/root/MainLayout';
import { appLayout } from './appLayout';

type GroupName = 'omnichannel' | 'marketplace' | 'account' | 'admin';

type GroupPrefix<TGroupName extends GroupName> = IRouterPaths[`${TGroupName}-index`]['pattern'];

type RouteNamesOf<TGroupName extends GroupName> = Extract<
| keyof {
[TRouteName in RouteName as IRouterPaths[TRouteName]['pattern'] extends `${GroupPrefix<TGroupName>}/${string}`
? TRouteName
: never]: never;
}
| `${GroupName}-index`,
RouteName
>;

type TrimPrefix<T extends string, P extends string> = T extends `${P}${infer U}` ? U : T;

export const createRouteGroup = <TGroupName extends GroupName>(
name: TGroupName,
prefix: GroupPrefix<TGroupName>,
Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/client/providers/RouterProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { queueMicrotask } from '../lib/utils/queueMicrotask';

FlowRouter.wait();

FlowRouter.notFound = {
action: () => undefined,
};

const subscribers = new Set<() => void>();

const listenToRouteChange = () => {
Expand Down
3 changes: 1 addition & 2 deletions apps/meteor/client/startup/reloadRoomAfterLogin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';

Expand All @@ -15,7 +14,7 @@ Meteor.startup(() => {
currentUsername = user?.username;
LegacyRoomManager.closeAllRooms();
// Reload only if the current route is a channel route
const routeName = FlowRouter.current().route?.name;
const routeName = router.getRouteName();
if (!routeName) {
return;
}
Expand Down
5 changes: 0 additions & 5 deletions apps/meteor/client/views/root/IndexRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { RouteName } from '@rocket.chat/ui-contexts';
import { useRouter, useUser, useUserId } from '@rocket.chat/ui-contexts';
import { FlowRouter } from 'meteor/kadira:flow-router';
import React, { useEffect } from 'react';

import PageLoading from './PageLoading';
Expand All @@ -17,10 +16,6 @@ const IndexRoute = () => {
}

const computation = Tracker.autorun((c) => {
if (!FlowRouter.subsReady()) {
return;
}

setTimeout(async () => {
if (user?.defaultRoom) {
const room = user.defaultRoom.split('/') as [routeName: RouteName, routeParam: string];
Expand Down
59 changes: 19 additions & 40 deletions apps/meteor/definition/externals/meteor/kadira-flow-router.d.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
declare module 'meteor/kadira:flow-router' {
import type { Subscription } from 'meteor/meteor';
import type { IRouterPaths, RouteName, RouterPathPattern } from '@rocket.chat/ui-contexts';

type GroupName = RouteName extends infer U ? (U extends `${infer TGroupName}-index` ? TGroupName : never) : never;
type GroupPrefix<TGroupName extends GroupName> = IRouterPaths[`${TGroupName}-index`]['pattern'];
type RouteNamesOf<TGroupName extends GroupName> = Extract<
| keyof {
[TRouteName in RouteName as IRouterPaths[TRouteName]['pattern'] extends `${GroupPrefix<TGroupName>}/${string}`
? TRouteName
: never]: Route<TRouteName>;
}
| `${GroupName}-index`,
RouteName
>;
type TrimPrefix<T extends string, P extends string> = T extends `${P}${infer U}` ? U : T;

type Context = {
params: Record<string, string>;
queryParams: Record<string, string>;
pathname: string;
oldRoute?: Route<RouteName, any>;
route: Route<RouteName, any>;
oldRoute?: Route<string, any>;
route: Route<string, any>;
};

type RouteOptions<TRouteName extends RouteName> = {
type RouteOptions<TRouteName extends string> = {
name?: TRouteName;
action?: (this: Route, params?: Record<string, string>, queryParams?: Record<string, string | string[]>) => void;
subscriptions?: (this: Route, params?: Record<string, string>, queryParams?: Record<string, string | string[]>) => void;
triggersEnter?: ((context: Context, redirect: (path: string) => void, stop: () => void) => void)[];
triggersExit?: ((context: Context) => void)[];
};

class Route<TRouteName extends RouteName, TGroup extends Group<GroupName> | undefined = any> {
constructor(router: Router, pathDef: IRouterPaths[TRouteName]['pattern'], options?: RouteOptions<TRouteName>, group?: TGroup);
class Route<TRouteName extends string, TGroup extends Group<string> | undefined = any> {
constructor(router: Router, pathDef: string, options?: RouteOptions<TRouteName>, group?: TGroup);

options: RouteOptions<TRouteName>;

pathDef: IRouterPaths[TRouteName]['pattern'];
pathDef: string;

path: string;

Expand Down Expand Up @@ -69,24 +55,24 @@ declare module 'meteor/kadira:flow-router' {
registerRouteChange(currentContext: Context, routeChanging?: boolean): void;
}

type GroupOptions<TGroupName extends RouteName> = {
type GroupOptions<TGroupName extends string> = {
name: TGroupName;
prefix?: GroupPrefix<TGroupName>;
prefix?: string;
triggersEnter?: unknown[];
triggersExit?: unknown[];
subscriptions?: (this: Route, params?: Record<string, string>, queryParams?: Record<string, string | string[]>) => void;
};

/** @deprecated */
class Group<TGroupName extends GroupName, TParentGroup extends Group<GroupName> | undefined = any> {
class Group<TGroupName extends string, TParentGroup extends Group<string> | undefined = any> {
/** @deprecated */
constructor(router: Router, options?: GroupOptions<TGroupName>, parent?: TParentGroup);

/** @deprecated */
name: TGroupName;

/** @deprecated */
prefix: GroupPrefix<TGroupName>;
prefix: string;

/** @deprecated */
options: GroupOptions<TGroupName>;
Expand All @@ -95,11 +81,7 @@ declare module 'meteor/kadira:flow-router' {
parent: TParentGroup;

/** @deprecated */
route<TRouteName extends RouteNamesOf<TGroupName>>(
pathDef: TrimPrefix<IRouterPaths[TRouteName]['pattern'], GroupPrefix<TGroupName>>,
options: RouteOptions<TRouteName>,
group?: TParentGroup,
): Route<TRouteName, TGroup>;
route<TRouteName extends string>(pathDef: string, options: RouteOptions<TRouteName>, group?: TParentGroup): Route<TRouteName, TGroup>;

/** @deprecated */
group(options?: GroupOptions<TGroupName>): Group;
Expand All @@ -113,8 +95,8 @@ declare module 'meteor/kadira:flow-router' {
context: Context;
params: Record<string, string>;
queryParams?: Record<string, string>;
route?: Route<RouteName> | undefined;
oldRoute?: Route<RouteName> | undefined;
route?: Route<string> | undefined;
oldRoute?: Route<string> | undefined;
};

type RouterOptions = {
Expand All @@ -124,21 +106,18 @@ declare module 'meteor/kadira:flow-router' {
class Router {
constructor();

route<TRouteName extends RouteName>(
pathDef: IRouterPaths[TRouteName]['pattern'],
options: RouteOptions<TRouteName>,
): Route<TRouteName, undefined>;
route<TRouteName extends string>(pathDef: string, options: RouteOptions<TRouteName>): Route<TRouteName, undefined>;

route<TRouteName extends RouteName, TGroup extends Group<RouteName>>(
pathDef: IRouterPaths[TRouteName]['pattern'],
route<TRouteName extends string, TGroup extends Group<string>>(
pathDef: string,
options: RouteOptions<TRouteName>,
group: TGroup,
): Route<TRouteName, TGroup>;

/** @deprecated */
group<TGroupName extends GroupName>(options: GroupOptions<TGroupName>): Group<TGroupName>;
group<TGroupName extends string>(options: GroupOptions<TGroupName>): Group<TGroupName>;

path(pathDef: RouteName | RouterPathPattern, fields?: Record<string, string>, queryParams?: Record<string, string>): string;
path(pathDef: string, fields?: Record<string, string>, queryParams?: Record<string, string>): string;

url(pathDef: string, fields?: Record<string, string>, queryParams?: Record<string, string>): string;

Expand Down Expand Up @@ -166,7 +145,7 @@ declare module 'meteor/kadira:flow-router' {

notFound: Omit<RouteOptions<any>, 'name'>;

getRouteName(): RouteName;
getRouteName(): string;

getParam(key: string): string;

Expand Down

0 comments on commit 0ebfc0e

Please sign in to comment.