feat(core): simplify subscribe page param (#8518)

AF-1481
This commit is contained in:
forehalo
2024-10-17 07:05:00 +00:00
parent b7fac5acb8
commit 7dae5c5dd5
4 changed files with 116 additions and 70 deletions
@@ -1,4 +1,5 @@
import { type CreateCheckoutSessionInput } from '@affine/graphql';
import { mixpanel } from '@affine/track';
import { OnEvent, Service } from '@toeverything/infra';
import { Subscription } from '../entities/subscription';
@@ -13,6 +14,22 @@ export class SubscriptionService extends Service {
constructor(private readonly store: SubscriptionStore) {
super();
this.subscription.ai$
.map(sub => !!sub)
.distinctUntilChanged()
.subscribe(ai => {
mixpanel.people.set({
ai,
});
});
this.subscription.pro$
.map(sub => !!sub)
.distinctUntilChanged()
.subscribe(pro => {
mixpanel.people.set({
pro,
});
});
}
async createCheckoutSession(input: CreateCheckoutSessionInput) {
@@ -1,21 +1,22 @@
import type { QuotaQuery } from '@affine/graphql';
import { createEvent, OnEvent, Service } from '@toeverything/infra';
import { mixpanel } from '@affine/track';
import { OnEvent, Service } from '@toeverything/infra';
import { UserQuota } from '../entities/user-quota';
import { AccountChanged } from './auth';
type UserQuotaInfo = NonNullable<QuotaQuery['currentUser']>['quota'];
export const UserQuotaChanged = createEvent<UserQuotaInfo>('UserQuotaChanged');
@OnEvent(AccountChanged, e => e.onAccountChanged)
export class UserQuotaService extends Service {
constructor() {
super();
this.quota.quota$.distinctUntilChanged().subscribe(q => {
this.eventBus.emit(UserQuotaChanged, q);
});
this.quota.quota$
.map(q => q?.humanReadable.name)
.distinctUntilChanged()
.subscribe(quota => {
mixpanel.people.set({
quota,
});
});
}
quota = this.framework.createEntity(UserQuota);
@@ -1,4 +1,3 @@
import type { QuotaQuery } from '@affine/graphql';
import { mixpanel } from '@affine/track';
import type { GlobalContextService } from '@toeverything/infra';
import { ApplicationStarted, OnEvent, Service } from '@toeverything/infra';
@@ -9,16 +8,11 @@ import {
type AuthService,
} from '../../cloud';
import { AccountLoggedOut } from '../../cloud/services/auth';
import { UserQuotaChanged } from '../../cloud/services/user-quota';
@OnEvent(ApplicationStarted, e => e.onApplicationStart)
@OnEvent(AccountChanged, e => e.updateIdentity)
@OnEvent(AccountLoggedOut, e => e.onAccountLoggedOut)
@OnEvent(UserQuotaChanged, e => e.onUserQuotaChanged)
export class TelemetryService extends Service {
private prevQuota: NonNullable<QuotaQuery['currentUser']>['quota'] | null =
null;
constructor(
private readonly auth: AuthService,
private readonly globalContextService: GlobalContextService
@@ -48,17 +42,6 @@ export class TelemetryService extends Service {
mixpanel.reset();
}
onUserQuotaChanged(quota: NonNullable<QuotaQuery['currentUser']>['quota']) {
const plan = quota?.humanReadable.name;
// only set when plan is not empty and changed
if (plan !== this.prevQuota?.humanReadable.name && plan) {
mixpanel.people.set({
plan: quota?.humanReadable.name,
});
}
this.prevQuota = quota;
}
registerMiddlewares() {
this.disposables.push(
mixpanel.middleware((_event, parameters) => {