Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add self host team plan #9569

Draft
wants to merge 2 commits into
base: canary
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/backend/server/src/plugins/license/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class LicenseService {
throw new WorkspaceLicenseAlreadyExists();
}

const data = await this.fetch<License>(
const data = await this.fetchAffinePro<License>(
`/api/team/licenses/${licenseKey}/activate`,
{
method: 'POST',
Expand Down Expand Up @@ -105,7 +105,7 @@ export class LicenseService {
throw new LicenseNotFound();
}

await this.fetch(`/api/team/licenses/${license.key}/deactivate`, {
await this.fetchAffinePro(`/api/team/licenses/${license.key}/deactivate`, {
method: 'POST',
});

Expand All @@ -120,10 +120,11 @@ export class LicenseService {
plan: SubscriptionPlan.SelfHostedTeam,
recurring: SubscriptionRecurring.Monthly,
});
return true;
}

async updateTeamRecurring(key: string, recurring: SubscriptionRecurring) {
await this.fetch(`/api/team/licenses/${key}/recurring`, {
await this.fetchAffinePro(`/api/team/licenses/${key}/recurring`, {
method: 'POST',
body: JSON.stringify({
recurring,
Expand All @@ -142,7 +143,7 @@ export class LicenseService {
throw new LicenseNotFound();
}

return this.fetch<{ url: string }>(
return this.fetchAffinePro<{ url: string }>(
`/api/team/licenses/${license.key}/create-customer-portal`,
{
method: 'POST',
Expand All @@ -164,7 +165,7 @@ export class LicenseService {
return;
}

await this.fetch(`/api/team/licenses/${license.key}/seats`, {
await this.fetchAffinePro(`/api/team/licenses/${license.key}/seats`, {
method: 'POST',
body: JSON.stringify({
quantity: count,
Expand Down Expand Up @@ -218,7 +219,7 @@ export class LicenseService {

private async revalidateLicense(license: InstalledLicense) {
try {
const res = await this.fetch<License>(
const res = await this.fetchAffinePro<License>(
`/api/team/licenses/${license.key}/health`
);

Expand Down Expand Up @@ -262,7 +263,7 @@ export class LicenseService {
}
}

private async fetch<T = any>(
private async fetchAffinePro<T = any>(
path: string,
init?: RequestInit
): Promise<T & { res: Response }> {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/server/src/plugins/payment/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class SubscriptionResolver {
if (input.plan === SubscriptionPlan.SelfHostedTeam) {
session = await this.service.checkout(input, {
plan: input.plan as any,
quantity: input.args.quantity ?? 10,
quantity: input.args?.quantity ?? 10,
});
} else {
if (!user) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ConfirmModal } from '@affine/component/ui/modal';
import { openQuotaModalAtom } from '@affine/core/components/atoms';
import { UserQuotaService } from '@affine/core/modules/cloud';
import { WorkspaceDialogService } from '@affine/core/modules/dialogs';
import { WorkspacePermissionService } from '@affine/core/modules/permissions';
import { WorkspaceQuotaService } from '@affine/core/modules/quota';
Expand Down Expand Up @@ -30,18 +29,6 @@ export const CloudQuotaModal = () => {
permissionService.permission.revalidate();
}, [permissionService]);

const quotaService = useService(UserQuotaService);
const userQuota = useLiveData(
quotaService.quota.quota$.map(q =>
q
? {
name: q.humanReadable.name,
blobLimit: q.humanReadable.blobLimit,
}
: null
)
);

const workspaceDialogService = useService(WorkspaceDialogService);
const handleUpgradeConfirm = useCallback(() => {
workspaceDialogService.open('setting', {
Expand All @@ -54,18 +41,19 @@ export const CloudQuotaModal = () => {
}, [workspaceDialogService, setOpen]);

const description = useMemo(() => {
if (userQuota && isOwner) {
return <OwnerDescription quota={userQuota.blobLimit} />;
}
if (workspaceQuota) {
return t['com.affine.payment.blob-limit.description.member']({
quota: workspaceQuota.humanReadable.blobLimit,
});
} else {
// loading
if (!workspaceQuota) {
return null;
}
}, [userQuota, isOwner, workspaceQuota, t]);
if (isOwner) {
return (
<OwnerDescription quota={workspaceQuota.humanReadable.blobLimit} />
);
}

return t['com.affine.payment.blob-limit.description.member']({
quota: workspaceQuota.humanReadable.blobLimit,
});
}, [isOwner, workspaceQuota, t]);

const onAbortLargeBlob = useAsyncCallback(
async (byteSize: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,21 @@ export const generateSubscriptionCallbackLink = (
recurring: SubscriptionRecurring,
workspaceId?: string
) => {
if (account === null) {
throw new Error('Account is required');
}
const baseUrl =
plan === SubscriptionPlan.AI
? '/ai-upgrade-success'
: plan === SubscriptionPlan.Team
? '/upgrade-success/team'
: '/upgrade-success';
: plan === SubscriptionPlan.SelfHostedTeam
? '/upgrade-success/self-hosted-team'
: '/upgrade-success';

if (plan === SubscriptionPlan.SelfHostedTeam) {
return baseUrl;
}
if (account === null) {
throw new Error('Account is required');
}

let name = account?.info?.name ?? '';
if (name.includes(separator)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,40 @@ export function getPlanDetail(t: T) {
benefits: teamBenefits(t),
},
],
[
SubscriptionPlan.SelfHostedTeam,
{
type: 'fixed',
plan: SubscriptionPlan.SelfHostedTeam,
price: '12',
yearlyPrice: '10',
name: 'Self-hosted Team',
description: 'Self-hosted Team description',
titleRenderer: (recurring, detail) => {
const price =
recurring === SubscriptionRecurring.Yearly
? detail.yearlyPrice
: detail.price;
return (
<>
{t['com.affine.payment.cloud.team-workspace.title.price-monthly'](
{
price: '$' + price,
}
)}
{recurring === SubscriptionRecurring.Yearly ? (
<span className={planTitleTitleCaption}>
{t[
'com.affine.payment.cloud.team-workspace.title.billed-yearly'
]()}
</span>
) : null}
</>
);
},
benefits: teamBenefits(t),
},
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { GlobalDialogService } from '@affine/core/modules/dialogs';
import {
type CreateCheckoutSessionInput,
ServerDeploymentType,
SubscriptionPlan,
SubscriptionRecurring,
SubscriptionStatus,
Expand Down Expand Up @@ -121,15 +122,24 @@ const ActionButton = ({ detail, recurring }: PlanCardProps) => {
const isOnetime = useLiveData(subscriptionService.subscription.isOnetimePro$);
const isFree = detail.plan === SubscriptionPlan.Free;

const serverService = useService(ServerService);
const isSelfHosted = useLiveData(
serverService.server.config$.selector(
c => c.type === ServerDeploymentType.Selfhosted
)
);

const signUpText = useMemo(
() => getSignUpText(detail.plan, t),
[detail.plan, t]
);

// branches:
// if contact => 'Contact Sales'
// if self-hosted team => 'Upgrade'
// if not signed in:
// if free => 'Sign up free'
// if team => 'Upgrade'
// else => 'Buy Pro'
// else
// if team => 'Start 14-day free trial'
Expand All @@ -144,6 +154,13 @@ const ActionButton = ({ detail, recurring }: PlanCardProps) => {
// if currentRecurring !== recurring => 'Change to {recurring} Billing'
// else => 'Upgrade'

// self-hosted team
if (isSelfHosted || detail.plan === SubscriptionPlan.SelfHostedTeam) {
return (
<UpgradeToSelfHostTeam recurring={recurring as SubscriptionRecurring} />
);
}

// not signed in
if (!loggedIn) {
return <SignUpAction>{signUpText}</SignUpAction>;
Expand Down Expand Up @@ -267,6 +284,51 @@ const UpgradeToTeam = ({ recurring }: { recurring: SubscriptionRecurring }) => {
</a>
);
};
const UpgradeToSelfHostTeam = ({
recurring,
}: {
recurring: SubscriptionRecurring;
}) => {
const t = useI18n();

const handleBeforeCheckout = useCallback(() => {
track.$.settingsPanel.plans.checkout({
plan: SubscriptionPlan.SelfHostedTeam,
recurring: recurring,
});
}, [recurring]);

const checkoutOptions = useMemo(
() => ({
recurring,
plan: SubscriptionPlan.SelfHostedTeam,
variant: null,
coupon: null,
successCallbackLink: generateSubscriptionCallbackLink(
null,
SubscriptionPlan.SelfHostedTeam,
recurring
),
}),
[recurring]
);

return (
<CheckoutSlot
onBeforeCheckout={handleBeforeCheckout}
checkoutOptions={checkoutOptions}
renderer={props => (
<Button
className={clsx(styles.planAction)}
variant="primary"
{...props}
>
{t['com.affine.payment.upgrade']()}
</Button>
)}
/>
);
};

export const Upgrade = ({
className,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useWorkspaceInfo } from '@affine/core/components/hooks/use-workspace-info';
import { ServerService } from '@affine/core/modules/cloud';
import type { SettingTab } from '@affine/core/modules/dialogs/constant';
import { WorkspaceService } from '@affine/core/modules/workspace';
import { ServerDeploymentType } from '@affine/graphql';
import { useI18n } from '@affine/i18n';
import {
CollaborationIcon,
Expand All @@ -9,11 +11,12 @@ import {
SaveIcon,
SettingsIcon,
} from '@blocksuite/icons/rc';
import { useService } from '@toeverything/infra';
import { useLiveData, useService } from '@toeverything/infra';
import { useMemo } from 'react';

import type { SettingSidebarItem, SettingState } from '../types';
import { WorkspaceSettingBilling } from './billing';
import { WorkspaceSettingLicense } from './license';
import { MembersPanel } from './members';
import { WorkspaceSettingDetail } from './preference';
import { WorkspaceSettingProperties } from './properties';
Expand Down Expand Up @@ -44,6 +47,10 @@ export const WorkspaceSetting = ({
return <WorkspaceSettingBilling />;
case 'workspace:storage':
return <WorkspaceSettingStorage onCloseSetting={onCloseSetting} />;
case 'workspace:license':
return (
<WorkspaceSettingLicense onChangeSettingState={onChangeSettingState} />
);
default:
return null;
}
Expand All @@ -52,10 +59,18 @@ export const WorkspaceSetting = ({
export const useWorkspaceSettingList = (): SettingSidebarItem[] => {
const workspaceService = useService(WorkspaceService);
const information = useWorkspaceInfo(workspaceService.workspace);
const serverService = useService(ServerService);

const isSelfhosted = useLiveData(
serverService.server.config$.selector(
c => c.type === ServerDeploymentType.Selfhosted
)
);

const t = useI18n();

const showBilling = information?.isTeam && information?.isOwner;
const showLicense = information?.isOwner && isSelfhosted;
const items = useMemo<SettingSidebarItem[]>(() => {
return [
{
Expand Down Expand Up @@ -88,10 +103,14 @@ export const useWorkspaceSettingList = (): SettingSidebarItem[] => {
icon: <PaymentIcon />,
testId: 'workspace-setting:billing',
},

// todo(@pengx17): add selfhost's team license
showLicense && {
key: 'workspace:license' as SettingTab,
title: t['com.affine.settings.workspace.license'](),
icon: <PaymentIcon />,
testId: 'workspace-setting:license',
},
].filter((item): item is SettingSidebarItem => !!item);
}, [showBilling, t]);
}, [showBilling, showLicense, t]);

return items;
};
Loading
Loading