Skip to content

Commit

Permalink
replace "browser" usages, move subscription code to memex-common,
Browse files Browse the repository at this point in the history
Move subscription UI trigger to event. messages
  • Loading branch information
blackforestboi committed May 11, 2024
1 parent e673de2 commit dd08b22
Show file tree
Hide file tree
Showing 50 changed files with 216 additions and 504 deletions.
2 changes: 1 addition & 1 deletion external/@worldbrain/memex-common
3 changes: 3 additions & 0 deletions src/authentication/components/AccountInfo.tsx
Expand Up @@ -19,6 +19,7 @@ import { PopoutBox } from '@worldbrain/memex-common/lib/common-ui/components/pop
import TextField from '@worldbrain/memex-common/lib/common-ui/components/text-field'
import UpgradeModal from '../upgrade-modal'
import { RemoteBGScriptInterface } from 'src/background-script/types'
import { Browser } from 'webextension-polyfill'

const styles = require('./styles.css')

Expand All @@ -30,6 +31,7 @@ export interface Props extends Dependencies {
setAuthMode: (mode) => void
getRootElement: () => HTMLElement
bgScriptBG: RemoteBGScriptInterface
browserAPIs: Browser
}

export default class UserScreen extends StatefulUIElement<Props, State, Event> {
Expand Down Expand Up @@ -122,6 +124,7 @@ export default class UserScreen extends StatefulUIElement<Props, State, Event> {
}
componentVariant={'AccountPage'}
limitReachedNotif={null}
browserAPIs={this.props.browserAPIs}
/>
</SettingSection>
<SettingSection
Expand Down
8 changes: 8 additions & 0 deletions src/authentication/components/AuthDialog/logic.ts
Expand Up @@ -5,6 +5,7 @@ import {
} from '@worldbrain/memex-common/lib/main-ui/classes/logic'
import type { Dependencies, State, Event, AuthDialogMode } from './types'
import type { EmailPasswordCredentials } from 'src/authentication/background/types'
import { checkStripePlan } from '@worldbrain/memex-common/lib/subscriptions/storage'

type EventHandler<EventName extends keyof Event> = UIEventHandler<
State,
Expand Down Expand Up @@ -139,6 +140,10 @@ export default class Logic extends UILogic<State, Event> {
return
} else {
this.dependencies.onAuth?.({ reason: 'login' })
await checkStripePlan(
credentials.email,
this.dependencies.browserAPIs,
)
}
}
})
Expand All @@ -155,6 +160,9 @@ export default class Logic extends UILogic<State, Event> {
this.emitMutation({ error: { $set: result.reason } })
} else {
this.dependencies.onAuth?.({ reason: 'login' })
const currentUser = await this.dependencies.authBG.getCurrentUser()
const email = currentUser.email
await checkStripePlan(email, this.dependencies.browserAPIs)
}
}
}
2 changes: 2 additions & 0 deletions src/authentication/components/AuthDialog/types.ts
Expand Up @@ -2,6 +2,7 @@ import type { UIEvent } from 'ui-logic-core'
import type { UITaskState } from '@worldbrain/memex-common/lib/main-ui/types'
import type { AuthRemoteFunctionsInterface } from 'src/authentication/background/types'
import type { AuthProviderType } from '@worldbrain/memex-common/lib/authentication/types'
import { Browser } from 'webextension-polyfill'

export interface Dependencies {
authBG: AuthRemoteFunctionsInterface
Expand All @@ -10,6 +11,7 @@ export interface Dependencies {
setSaveState?: UITaskState
}): void
onAuth?(event: { reason: 'login' | 'register' }): void
browserAPIs: Browser
}

export type AuthDialogMode =
Expand Down
5 changes: 4 additions & 1 deletion src/authentication/components/UserScreen/index.tsx
Expand Up @@ -12,7 +12,7 @@ import LoadingIndicator from '@worldbrain/memex-common/lib/common-ui/components/
import { RemoteBGScriptInterface } from 'src/background-script/types'
import { LOGIN_URL } from 'src/constants'
import checkBrowser from 'src/util/check-browser'
import browser from 'webextension-polyfill'
import browser, { Browser } from 'webextension-polyfill'

// interface Props {
// initiallyShowSubscriptionModal?: boolean
Expand All @@ -24,6 +24,7 @@ export interface Props extends Dependencies {
refreshUser?: boolean
getRootElement: () => HTMLElement
bgScriptBG: RemoteBGScriptInterface
browserAPIs: Browser
}

export default class UserScreen extends StatefulUIElement<Props, State, Event> {
Expand Down Expand Up @@ -151,6 +152,7 @@ export default class UserScreen extends StatefulUIElement<Props, State, Event> {
mode,
})
}}
browserAPIs={browser}
/>
</SettingSection>
</div>
Expand All @@ -163,6 +165,7 @@ export default class UserScreen extends StatefulUIElement<Props, State, Event> {
}}
getRootElement={this.props.getRootElement}
bgScriptBG={this.props.bgScriptBG}
browserAPIs={browser}
/>
)}
</>
Expand Down
7 changes: 5 additions & 2 deletions src/authentication/components/UserScreen/logic.ts
Expand Up @@ -5,7 +5,7 @@ import {
UIEventHandler,
} from '@worldbrain/memex-common/lib/main-ui/classes/logic'
import type { Dependencies, State, Event } from './types'
import { checkStripePlan } from 'src/util/subscriptions/storage'
import { checkStripePlan } from '@worldbrain/memex-common/lib/subscriptions/storage'

type EventHandler<EventName extends keyof Event> = UIEventHandler<
State,
Expand Down Expand Up @@ -162,7 +162,10 @@ export default class Logic extends UILogic<State, Event> {
$set: 'running',
},
})
const subscriptionStatusInfo = await checkStripePlan(event.email)
const subscriptionStatusInfo = await checkStripePlan(
event.email,
this.dependencies.browserAPIs,
)

if (subscriptionStatusInfo != null) {
this.emitMutation({
Expand Down
2 changes: 2 additions & 0 deletions src/authentication/components/UserScreen/types.ts
Expand Up @@ -4,12 +4,14 @@ import type { AuthRemoteFunctionsInterface } from 'src/authentication/background
import type { PersonalCloudRemoteInterface } from 'src/personal-cloud/background/types'
import type { AuthDialogMode } from 'src/authentication/components/AuthDialog/types'
import type { AuthenticatedUser } from '@worldbrain/memex-common/lib/authentication/types'
import { Browser } from 'webextension-polyfill'

export interface Dependencies {
authBG: AuthRemoteFunctionsInterface
personalCloudBG: PersonalCloudRemoteInterface
navToDashboard: () => void
navToGuidedTutorial: () => void
browserAPIs: Browser
}

export interface State {
Expand Down
13 changes: 10 additions & 3 deletions src/authentication/upgrade-modal/logic.ts
@@ -1,4 +1,5 @@
import { checkStripePlan } from 'src/util/subscriptions/storage'
import { checkStripePlan } from '@worldbrain/memex-common/lib/subscriptions/storage'

import {
PromptTemplatesEvent,
PromptTemplatesDependencies,
Expand Down Expand Up @@ -31,7 +32,10 @@ export default class PromptTemplatesLogic extends UILogic<

const userEmail = (await this.dependencies.authBG.getCurrentUser())
?.email
const activatedPowerUps = await checkStripePlan(userEmail)
const activatedPowerUps = await checkStripePlan(
userEmail,
this.dependencies.browserAPIs,
)

this.emitMutation({
activatedPowerUps: { $set: activatedPowerUps },
Expand Down Expand Up @@ -121,7 +125,10 @@ export default class PromptTemplatesLogic extends UILogic<
})
return
} else if (upgradeResponse === 'success') {
const updatedPlans = await checkStripePlan(previousState.userEmail)
const updatedPlans = await checkStripePlan(
previousState.userEmail,
this.dependencies.browserAPIs,
)
this.emitMutation({
activatedPowerUps: { $set: updatedPlans },
checkoutLoading: { $set: 'success' },
Expand Down
2 changes: 2 additions & 0 deletions src/authentication/upgrade-modal/types.ts
Expand Up @@ -5,6 +5,7 @@ import {
} from '../../../external/@worldbrain/memex-common/ts/main-ui/classes/logic'
import { PremiumPlans } from '../../../external/@worldbrain/memex-common/ts/subscriptions/availablePowerups'
import { AuthRemoteFunctionsInterface } from '../background/types'
import { Browser } from 'webextension-polyfill'

export interface PromptTemplatesDependencies {
powerUpType: PowerUpModalVersion
Expand All @@ -18,6 +19,7 @@ export interface PromptTemplatesDependencies {
getRootElement?: () => HTMLElement
closeComponent?: () => void
authBG: AuthRemoteFunctionsInterface
browserAPIs: Browser
}

export interface PromptTemplatesState {
Expand Down
7 changes: 4 additions & 3 deletions src/background-script/index.ts
@@ -1,5 +1,5 @@
import type Storex from '@worldbrain/storex'
import type { Runtime, Storage, Tabs } from 'webextension-polyfill'
import type { Browser, Runtime, Storage, Tabs } from 'webextension-polyfill'
import type { URLNormalizer } from '@worldbrain/memex-common/lib/url-utils/normalize/types'

import * as utils from './utils'
Expand Down Expand Up @@ -34,7 +34,7 @@ import { MISSING_PDF_QUERY_PARAM } from 'src/dashboard-refactor/constants'
import type { BackgroundModules } from './setup'
import type { InPageUIContentScriptRemoteInterface } from 'src/in-page-ui/content_script/types'
import { captureException } from 'src/util/raven'
import { checkStripePlan } from 'src/util/subscriptions/storage'
import { checkStripePlan } from '@worldbrain/memex-common/lib/subscriptions/storage'
import type { AnalyticsCoreInterface } from '@worldbrain/memex-common/lib/analytics/types'
import { trackOnboardingPath } from '@worldbrain/memex-common/lib/analytics/events'
import { CLOUDFLARE_WORKER_URLS } from '@worldbrain/memex-common/lib/content-sharing/storage/constants'
Expand All @@ -50,6 +50,7 @@ interface Dependencies {
storageChangesMan: StorageChangesManager
storageAPI: Storage.Static
runtimeAPI: Runtime.Static
browserAPIs: Browser
tabsAPI: Tabs.Static
analyticsBG: AnalyticsCoreInterface
storageManager: Storex
Expand Down Expand Up @@ -252,7 +253,7 @@ class BackgroundScript {
let currentUser = await this.deps.bgModules.auth.authService.getCurrentUser()
if (currentUser) {
let emailAddresse = currentUser.email
checkStripePlan(emailAddresse)
checkStripePlan(emailAddresse, this.deps.browserAPIs)
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/background-script/setup.ts
Expand Up @@ -494,6 +494,7 @@ export function createBackgroundModules(options: {
})
const summarizeBG = new SummarizeBackground({
remoteEventEmitter: createRemoteEventEmitter('pageSummary'),
browserAPIs: options.browserAPIs,
analyticsBG,
})

Expand Down Expand Up @@ -584,6 +585,7 @@ export function createBackgroundModules(options: {
storageChangesMan: options.localStorageChangesManager,
urlNormalizer: normalizeUrl,
runtimeAPI: options.browserAPIs.runtime,
browserAPIs: options.browserAPIs,
storageAPI: options.browserAPIs.storage,
tabsAPI: options.browserAPIs.tabs,
localExtSettingStore,
Expand Down

0 comments on commit dd08b22

Please sign in to comment.