fix(core): fix error when server not support ai (#6796)

This commit is contained in:
EYHN
2024-05-07 08:25:27 +00:00
parent a0e0b6b53b
commit 35ce4adffe
13 changed files with 226 additions and 97 deletions
@@ -4,7 +4,7 @@ import { openSettingModalAtom } from '@affine/core/atoms';
import {
ServerConfigService,
SubscriptionService,
UserQuotaService,
UserCopilotQuotaService,
} from '@affine/core/modules/cloud';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { useLiveData, useService } from '@toeverything/infra';
@@ -28,14 +28,18 @@ export const AIUsagePanel = () => {
// revalidate latest subscription status
subscriptionService.subscription.revalidate();
}, [subscriptionService]);
const quotaService = useService(UserQuotaService);
const copilotQuotaService = useService(UserCopilotQuotaService);
useEffect(() => {
quotaService.quota.revalidate();
}, [quotaService]);
const aiActionLimit = useLiveData(quotaService.quota.aiActionLimit$);
const aiActionUsed = useLiveData(quotaService.quota.aiActionUsed$);
const loading = aiActionLimit === null || aiActionUsed === null;
const loadError = useLiveData(quotaService.quota.error$);
copilotQuotaService.copilotQuota.revalidate();
}, [copilotQuotaService]);
const copilotActionLimit = useLiveData(
copilotQuotaService.copilotQuota.copilotActionLimit$
);
const copilotActionUsed = useLiveData(
copilotQuotaService.copilotQuota.copilotActionUsed$
);
const loading = copilotActionLimit === null || copilotActionUsed === null;
const loadError = useLiveData(copilotQuotaService.copilotQuota.error$);
const openBilling = useCallback(() => {
setOpenSettingModal({
@@ -69,13 +73,13 @@ export const AIUsagePanel = () => {
}
const percent =
aiActionLimit === 'unlimited'
copilotActionLimit === 'unlimited'
? 0
: Math.min(
100,
Math.max(
0.5,
Number(((aiActionUsed / aiActionLimit) * 100).toFixed(4))
Number(((copilotActionUsed / copilotActionLimit) * 100).toFixed(4))
)
);
@@ -91,7 +95,7 @@ export const AIUsagePanel = () => {
}
name={t['com.affine.payment.ai.usage-title']()}
>
{aiActionLimit === 'unlimited' ? (
{copilotActionLimit === 'unlimited' ? (
hasPaymentFeature && aiSubscription?.canceledAt ? (
<AIResume />
) : (
@@ -106,8 +110,8 @@ export const AIUsagePanel = () => {
<span>{t['com.affine.payment.ai.usage.used-caption']()}</span>
<span>
{t['com.affine.payment.ai.usage.used-detail']({
used: aiActionUsed.toString(),
limit: aiActionLimit.toString(),
used: copilotActionUsed.toString(),
limit: copilotActionLimit.toString(),
})}
</span>
</div>
@@ -8,7 +8,12 @@ import { Button } from '@affine/component/ui/button';
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { ArrowRightSmallIcon, CameraIcon } from '@blocksuite/icons';
import { useEnsureLiveData, useService } from '@toeverything/infra';
import {
useEnsureLiveData,
useLiveData,
useService,
useServices,
} from '@toeverything/infra';
import { useSetAtom } from 'jotai';
import type { FC, MouseEvent } from 'react';
import { useCallback, useEffect, useState } from 'react';
@@ -18,7 +23,7 @@ import {
openSettingModalAtom,
openSignOutModalAtom,
} from '../../../../atoms';
import { AuthService } from '../../../../modules/cloud';
import { AuthService, ServerConfigService } from '../../../../modules/cloud';
import { mixpanel } from '../../../../utils';
import { Upload } from '../../../pure/file-upload';
import { AIUsagePanel } from './ai-usage-panel';
@@ -178,8 +183,15 @@ const StoragePanel = () => {
};
export const AccountSetting: FC = () => {
const { authService, serverConfigService } = useServices({
AuthService,
ServerConfigService,
});
const serverFeatures = useLiveData(
serverConfigService.serverConfig.features$
);
const t = useAFFiNEI18N();
const session = useService(AuthService).session;
const session = authService.session;
useEffect(() => {
session.revalidate();
}, [session]);
@@ -235,7 +247,7 @@ export const AccountSetting: FC = () => {
</Button>
</SettingRow>
<StoragePanel />
<AIUsagePanel />
{serverFeatures?.copilot && <AIUsagePanel />}
<SettingRow
name={t[`Sign out`]()}
desc={t['com.affine.setting.sign.out.message']()}