refactor(web): dependency management (#2386)
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { isDesktopApplication } from '@/Utils'
|
||||
import { ApplicationInterface, LegacyApiServiceInterface, Result, UseCaseInterface } from '@standardnotes/snjs'
|
||||
|
||||
export class GetPurchaseFlowUrl implements UseCaseInterface<string> {
|
||||
constructor(
|
||||
private application: ApplicationInterface,
|
||||
private legacyApi: LegacyApiServiceInterface,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<string>> {
|
||||
const currentUrl = window.location.origin
|
||||
const successUrl = isDesktopApplication() ? 'standardnotes://' : currentUrl
|
||||
|
||||
if (this.application.sessions.isSignedOut() || this.application.isThirdPartyHostUsed()) {
|
||||
return Result.ok(`${window.purchaseUrl}/offline?&success_url=${successUrl}`)
|
||||
}
|
||||
|
||||
const token = await this.legacyApi.getNewSubscriptionToken()
|
||||
if (token) {
|
||||
return Result.ok(`${window.purchaseUrl}?subscription_token=${token}&success_url=${successUrl}`)
|
||||
}
|
||||
|
||||
return Result.fail('Could not get purchase flow URL.')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { isMobileScreen, isTabletOrMobileScreen, isTabletScreen } from '@/Utils'
|
||||
import { Environment, Result, SyncUseCaseInterface } from '@standardnotes/snjs'
|
||||
import { IsNativeMobileWeb } from '@standardnotes/ui-services'
|
||||
|
||||
type ReturnType = {
|
||||
isTabletOrMobile: boolean
|
||||
isTablet: boolean
|
||||
isMobile: boolean
|
||||
}
|
||||
|
||||
export class IsTabletOrMobileScreen implements SyncUseCaseInterface<ReturnType> {
|
||||
private _isNativeMobileWeb = new IsNativeMobileWeb(this.environment)
|
||||
|
||||
constructor(private environment: Environment) {}
|
||||
|
||||
execute(): Result<ReturnType> {
|
||||
const isNativeMobile = this._isNativeMobileWeb.execute().getValue()
|
||||
const isTabletOrMobile = isTabletOrMobileScreen() || isNativeMobile
|
||||
const isTablet = isTabletScreen() || (isNativeMobile && !isMobileScreen())
|
||||
const isMobile = isMobileScreen() || (isNativeMobile && !isTablet)
|
||||
|
||||
if (isTablet && isMobile) {
|
||||
throw Error('isTablet and isMobile cannot both be true')
|
||||
}
|
||||
|
||||
return Result.ok({
|
||||
isTabletOrMobile,
|
||||
isTablet,
|
||||
isMobile,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Environment, Result, UseCaseInterface } from '@standardnotes/snjs'
|
||||
import { GetPurchaseFlowUrl } from './GetPurchaseFlowUrl'
|
||||
import { RouteType, WebApplicationInterface } from '@standardnotes/ui-services'
|
||||
|
||||
export class LoadPurchaseFlowUrl implements UseCaseInterface<void> {
|
||||
constructor(
|
||||
private application: WebApplicationInterface,
|
||||
private _getPurchaseFlowUrl: GetPurchaseFlowUrl,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<void>> {
|
||||
const urlResult = await this._getPurchaseFlowUrl.execute()
|
||||
if (urlResult.isFailed()) {
|
||||
return urlResult
|
||||
}
|
||||
|
||||
const url = urlResult.getValue()
|
||||
const route = this.application.routeService.getRoute()
|
||||
const params = route.type === RouteType.Purchase ? route.purchaseParams : { period: null, plan: null }
|
||||
const period = params.period ? `&period=${params.period}` : ''
|
||||
const plan = params.plan ? `&plan=${params.plan}` : ''
|
||||
|
||||
if (url) {
|
||||
const finalUrl = `${url}${period}${plan}`
|
||||
|
||||
if (this.application.isNativeMobileWeb()) {
|
||||
this.application.mobileDevice.openUrl(finalUrl)
|
||||
} else if (this.application.environment === Environment.Desktop) {
|
||||
this.application.desktopDevice?.openUrl(finalUrl)
|
||||
} else {
|
||||
const windowProxy = window.open('', '_blank')
|
||||
;(windowProxy as WindowProxy).location = finalUrl
|
||||
}
|
||||
|
||||
return Result.ok()
|
||||
}
|
||||
|
||||
return Result.fail('Could not load purchase flow URL.')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Environment, LegacyApiServiceInterface, Result, UseCaseInterface } from '@standardnotes/snjs'
|
||||
import { WebApplicationInterface } from '@standardnotes/ui-services'
|
||||
|
||||
export class OpenSubscriptionDashboard implements UseCaseInterface<void> {
|
||||
constructor(
|
||||
private application: WebApplicationInterface,
|
||||
private legacyApi: LegacyApiServiceInterface,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<void>> {
|
||||
const token = await this.legacyApi.getNewSubscriptionToken()
|
||||
if (!token) {
|
||||
return Result.fail('Could not get subscription token.')
|
||||
}
|
||||
|
||||
const url = `${window.dashboardUrl}?subscription_token=${token}`
|
||||
|
||||
if (this.application.device.environment === Environment.Mobile) {
|
||||
this.application.device.openUrl(url)
|
||||
return Result.ok()
|
||||
}
|
||||
|
||||
if (this.application.device.environment === Environment.Desktop) {
|
||||
window.open(url, '_blank')
|
||||
return Result.ok()
|
||||
}
|
||||
|
||||
const windowProxy = window.open('', '_blank')
|
||||
;(windowProxy as WindowProxy).location = url
|
||||
|
||||
return Result.ok()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { AppPaneId } from './../../Components/Panes/AppPaneMetadata'
|
||||
import { PaneLayout } from './../../Controllers/PaneController/PaneLayout'
|
||||
import { IsTabletOrMobileScreen } from './IsTabletOrMobileScreen'
|
||||
import { Result, SyncUseCaseInterface } from '@standardnotes/snjs'
|
||||
|
||||
export class PanesForLayout implements SyncUseCaseInterface<AppPaneId[]> {
|
||||
constructor(private _isTabletOrMobileScreen: IsTabletOrMobileScreen) {}
|
||||
|
||||
execute(layout: PaneLayout): Result<AppPaneId[]> {
|
||||
const screen = this._isTabletOrMobileScreen.execute().getValue()
|
||||
if (screen.isTablet) {
|
||||
if (layout === PaneLayout.TagSelection || layout === PaneLayout.TableView) {
|
||||
return Result.ok([AppPaneId.Navigation, AppPaneId.Items])
|
||||
} else if (layout === PaneLayout.ItemSelection || layout === PaneLayout.Editing) {
|
||||
return Result.ok([AppPaneId.Items, AppPaneId.Editor])
|
||||
}
|
||||
} else if (screen.isMobile) {
|
||||
if (layout === PaneLayout.TagSelection) {
|
||||
return Result.ok([AppPaneId.Navigation])
|
||||
} else if (layout === PaneLayout.ItemSelection || layout === PaneLayout.TableView) {
|
||||
return Result.ok([AppPaneId.Navigation, AppPaneId.Items])
|
||||
} else if (layout === PaneLayout.Editing) {
|
||||
return Result.ok([AppPaneId.Navigation, AppPaneId.Items, AppPaneId.Editor])
|
||||
}
|
||||
} else {
|
||||
if (layout === PaneLayout.TableView) {
|
||||
return Result.ok([AppPaneId.Navigation, AppPaneId.Items])
|
||||
} else {
|
||||
return Result.ok([AppPaneId.Navigation, AppPaneId.Items, AppPaneId.Editor])
|
||||
}
|
||||
}
|
||||
|
||||
throw Error('Unhandled pane layout')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user