-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-6827] Browser Extension Refresh - Tabs Routing (#9004)
* [PM-6827] Add componentRouteSwap util function * [PM-6827] Add extension-refresh feature flag * [PM-6827] Add extension-refresh route swap utils * [PM-6827] Add the TabsV2 component * [PM-6827] Add the TabsV2 to routing module * [PM-6827] Fix route prefixes in popup-tab-navigation component
- Loading branch information
1 parent
09ff12f
commit ff30211
Showing
7 changed files
with
125 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { inject, Type } from "@angular/core"; | ||
import { Route, Router, Routes, UrlTree } from "@angular/router"; | ||
|
||
import { componentRouteSwap } from "@bitwarden/angular/utils/component-route-swap"; | ||
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum"; | ||
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service"; | ||
|
||
/** | ||
* Helper function to swap between two components based on the ExtensionRefresh feature flag. | ||
* @param defaultComponent - The current non-refreshed component to render. | ||
* @param refreshedComponent - The new refreshed component to render. | ||
* @param options - The shared route options to apply to both components. | ||
*/ | ||
export function extensionRefreshSwap( | ||
defaultComponent: Type<any>, | ||
refreshedComponent: Type<any>, | ||
options: Route, | ||
): Routes { | ||
return componentRouteSwap( | ||
defaultComponent, | ||
refreshedComponent, | ||
async () => { | ||
const configService = inject(ConfigService); | ||
return configService.getFeatureFlag(FeatureFlag.ExtensionRefresh); | ||
}, | ||
options, | ||
); | ||
} | ||
|
||
/** | ||
* Helper function to redirect to a new URL based on the ExtensionRefresh feature flag. | ||
* @param redirectUrl - The URL to redirect to if the ExtensionRefresh flag is enabled. | ||
*/ | ||
export function extensionRefreshRedirect(redirectUrl: string): () => Promise<boolean | UrlTree> { | ||
return async () => { | ||
const configService = inject(ConfigService); | ||
const router = inject(Router); | ||
const shouldRedirect = await configService.getFeatureFlag(FeatureFlag.ExtensionRefresh); | ||
if (shouldRedirect) { | ||
return router.parseUrl(redirectUrl); | ||
} else { | ||
return true; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Component } from "@angular/core"; | ||
|
||
@Component({ | ||
selector: "app-tabs-v2", | ||
template: ` | ||
<popup-tab-navigation> | ||
<router-outlet></router-outlet> | ||
</popup-tab-navigation> | ||
`, | ||
}) | ||
export class TabsV2Component {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Type } from "@angular/core"; | ||
import { Route, Routes } from "@angular/router"; | ||
|
||
/** | ||
* Helper function to swap between two components based on an async condition. The async condition is evaluated | ||
* as an `CanMatchFn` and supports Angular dependency injection via `inject()`. | ||
* | ||
* @example | ||
* ```ts | ||
* const routes = [ | ||
* ...componentRouteSwap( | ||
* defaultComponent, | ||
* altComponent, | ||
* async () => { | ||
* const configService = inject(ConfigService); | ||
* return configService.getFeatureFlag(FeatureFlag.SomeFlag); | ||
* }, | ||
* { | ||
* path: 'some-path' | ||
* } | ||
* ), | ||
* // Other routes... | ||
* ]; | ||
* ``` | ||
* | ||
* @param defaultComponent - The default component to render. | ||
* @param altComponent - The alternate component to render when the condition is met. | ||
* @param shouldSwapFn - The async function to determine if the alternate component should be rendered. | ||
* @param options - The shared route options to apply to both components. | ||
*/ | ||
export function componentRouteSwap( | ||
defaultComponent: Type<any>, | ||
altComponent: Type<any>, | ||
shouldSwapFn: () => Promise<boolean>, | ||
options: Route, | ||
): Routes { | ||
const defaultRoute = { | ||
...options, | ||
component: defaultComponent, | ||
}; | ||
|
||
const altRoute: Route = { | ||
...options, | ||
component: altComponent, | ||
canMatch: [ | ||
async () => { | ||
return await shouldSwapFn(); | ||
}, | ||
...(options.canMatch ?? []), | ||
], | ||
}; | ||
|
||
// Return the alternate route first, so it is evaluated first. | ||
return [altRoute, defaultRoute]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters