Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat(core): add workspace quota panel for team workspace (#9085)

JimmFly216e09e1afd2
diff --git a/packages/frontend/core/src/desktop/dialogs/setting/general-setting/plans/plan-card.tsx b/packages/frontend/core/src/desktop/dialogs/setting/general-setting/plans/plan-card.tsx
index aef3731d13..f2325f29fb 100644
--- a/packages/frontend/core/src/desktop/dialogs/setting/general-setting/plans/plan-card.tsx
+++ b/packages/frontend/core/src/desktop/dialogs/setting/general-setting/plans/plan-card.tsx
@@ -103,7 +103,7 @@ const getSignUpText = (
case SubscriptionPlan.Free:
return t['com.affine.payment.sign-up-free']();
case SubscriptionPlan.Team:
- return t['com.affine.payment.start-free-trial']();
+ return t['com.affine.payment.upgrade']();
default:
return t['com.affine.payment.buy-pro']();
}
@@ -263,7 +263,7 @@ const UpgradeToTeam = () => {
variant="primary"
data-event-args-url={url}
>
- {t['com.affine.payment.start-free-trial']()}
+ {t['com.affine.payment.upgrade']()}
</Button>
</a>
);
diff --git a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/export.tsx b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/export.tsx
index 2eab27cd1c..4c730246e1 100644
--- a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/export.tsx
+++ b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/export.tsx
@@ -4,9 +4,11 @@ import { Button } from '@affine/component/ui/button';
import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks';
import { useSystemOnline } from '@affine/core/components/hooks/use-system-online';
import { DesktopApiService } from '@affine/core/modules/desktop-api';
+import { WorkspacePermissionService } from '@affine/core/modules/permissions';
import { useI18n } from '@affine/i18n';
import track from '@affine/track';
import {
+ useLiveData,
useService,
type Workspace,
type WorkspaceMetadata,
@@ -23,6 +25,13 @@ export const DesktopExportPanel = ({
workspace,
}: ExportPanelProps) => {
const workspaceId = workspaceMetadata.id;
+ const workspacePermissionService = useService(
+ WorkspacePermissionService
+ ).permission;
+ const isTeam = useLiveData(workspacePermissionService.isTeam$);
+ const isOwner = useLiveData(workspacePermissionService.isOwner$);
+ const isAdmin = useLiveData(workspacePermissionService.isAdmin$);
+
const t = useI18n();
const [saving, setSaving] = useState(false);
const isOnline = useSystemOnline();
@@ -55,6 +64,10 @@ export const DesktopExportPanel = ({
}
}, [desktopApi, isOnline, saving, t, workspace, workspaceId]);
+ if (isTeam && !isOwner && !isAdmin) {
+ return null;
+ }
+
return (
<SettingRow name={t['Export']()} desc={t['Export Description']()}>
<Button
diff --git a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/index.tsx b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/index.tsx
index 1f884a9216..c646de4367 100644
--- a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/index.tsx
+++ b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/index.tsx
@@ -20,6 +20,7 @@ import { MembersPanel } from './members';
import { ProfilePanel } from './profile';
import { SharingPanel } from './sharing';
import type { WorkspaceSettingDetailProps } from './types';
+import { WorkspaceQuotaPanel } from './workspace-quota';
export const WorkspaceSettingDetail = ({
workspaceMetadata,
@@ -69,6 +70,7 @@ export const WorkspaceSettingDetail = ({
</SettingWrapper>
<SettingWrapper title={t['com.affine.brand.affineCloud']()}>
<EnableCloudPanel onCloseSetting={onCloseSetting} />
+ <WorkspaceQuotaPanel />
<MembersPanel onChangeSettingState={onChangeSettingState} />
</SettingWrapper>
<AiSetting />
diff --git a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/members/cloud-members-panel.tsx b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/members/cloud-members-panel.tsx
index 05b4026d59..4b1bd8f569 100644
--- a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/members/cloud-members-panel.tsx
+++ b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/members/cloud-members-panel.tsx
@@ -146,7 +146,7 @@ export const CloudWorkspaceMembersPanel = ({
return (
<span>
{t['com.affine.payment.member.description2']()}
- {hasPaymentFeature ? (
+ {hasPaymentFeature && isOwner ? (
<div
className={styles.goUpgradeWrapper}
onClick={handleUpgradeConfirm}
@@ -158,7 +158,14 @@ export const CloudWorkspaceMembersPanel = ({
) : null}
</span>
);
- }, [handleUpgradeConfirm, hasPaymentFeature, isTeam, t, workspaceQuota]);
+ }, [
+ handleUpgradeConfirm,
+ hasPaymentFeature,
+ isOwner,
+ isTeam,
+ t,
+ workspaceQuota,
+ ]);
const title = useMemo(() => {
if (isTeam) {
diff --git a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/style.css.ts b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/style.css.ts
index 5288bf9a87..e0231f400f 100644
--- a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/style.css.ts
+++ b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/style.css.ts
@@ -1,6 +1,6 @@
import { cssVar } from '@toeverything/theme';
import { cssVarV2 } from '@toeverything/theme/v2';
-import { style } from '@vanilla-extract/css';
+import { globalStyle, style } from '@vanilla-extract/css';
export const profileWrapper = style({
display: 'flex',
alignItems: 'flex-end',
@@ -35,3 +35,31 @@ export const workspaceLabel = style({
lineHeight: '20px',
whiteSpace: 'nowrap',
});
+
+export const storageProgressContainer = style({
+ display: 'flex',
+ justifyContent: 'space-between',
+ alignItems: 'center',
+});
+export const storageProgressWrapper = style({
+ flexGrow: 1,
+ marginRight: '20px',
+});
+globalStyle(`${storageProgressWrapper} .storage-progress-desc`, {
+ fontSize: cssVar('fontXs'),
+ color: cssVarV2('text/secondary'),
+ height: '20px',
+ display: 'flex',
+ justifyContent: 'space-between',
+ alignItems: 'center',
+ marginBottom: 2,
+});
+globalStyle(`${storageProgressWrapper} .storage-progress-bar-wrapper`, {
+ height: '8px',
+ borderRadius: '4px',
+ backgroundColor: cssVarV2('layer/background/hoverOverlay'),
+ overflow: 'hidden',
+});
+export const storageProgressBar = style({
+ height: '100%',
+});
diff --git a/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/workspace-quota.tsx b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/workspace-quota.tsx
new file mode 100644
index 0000000000..db53628eaf
--- /dev/null
+++ b/packages/frontend/core/src/desktop/dialogs/setting/workspace-setting/new-workspace-setting-detail/workspace-quota.tsx
@@ -0,0 +1,77 @@
+import { ErrorMessage, Skeleton } from '@affine/component';
+import { SettingRow } from '@affine/component/setting-components';
+import { WorkspacePermissionService } from '@affine/core/modules/permissions';
+import { WorkspaceQuotaService } from '@affine/core/modules/quota';
+import { useI18n } from '@affine/i18n';
+import { useLiveData, useService } from '@toeverything/infra';
+import { cssVarV2 } from '@toeverything/theme/v2';
+import { useEffect } from 'react';
+
+import * as styles from './style.css';
+
+export const WorkspaceQuotaPanel = () => {
+ const t = useI18n();
+ return (
+ <SettingRow
+ name={t['com.affine.workspace.storage']()}
+ desc=""
+ spreadCol={false}
+ >
+ <StorageProgress />
+ </SettingRow>
+ );
+};
+
+export const StorageProgress = () => {
+ const t = useI18n();
+ const workspacePermissionService = useService(
+ WorkspacePermissionService
+ ).permission;
+ const workspaceQuotaService = useService(WorkspaceQuotaService).quota;
+ const isTeam = useLiveData(workspacePermissionService.isTeam$);
+ const isLoading = useLiveData(workspaceQuotaService.isLoading$);
+ const usedFormatted = useLiveData(workspaceQuotaService.usedFormatted$);
+ const maxFormatted = useLiveData(workspaceQuotaService.maxFormatted$);
+ const percent = useLiveData(workspaceQuotaService.percent$);
+ const color = useLiveData(workspaceQuotaService.color$);
+
+ useEffect(() => {
+ // revalidate quota to get the latest status
+ workspaceQuotaService.revalidate();
+ }, [workspaceQuotaService]);
+
+ const loadError = useLiveData(workspaceQuotaService.error$);
+
+ if (isLoading) {
+ if (loadError) {
+ return <ErrorMessage>Load error</ErrorMessage>;
+ }
+ return <Skeleton height={42} />;
+ }
+
+ if (!isTeam) {
+ return null;
+ }
+
+ return (
+ <div className={styles.storageProgressContainer}>
+ <div className={styles.storageProgressWrapper}>
+ <div className="storage-progress-desc">
+ <span>{t['com.affine.storage.used.hint']()}</span>
+ <span>
+ {usedFormatted}/{maxFormatted}
+ </span>
+ </div>
+ <div className="storage-progress-bar-wrapper">
+ <div
+ className={styles.storageProgressBar}
+ style={{
+ width: `${percent}%`,
+ backgroundColor: color ?? cssVarV2('toast/iconState/regular'),
+ }}
+ ></div>
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/packages/frontend/core/src/modules/quota/entities/quota.ts b/packages/frontend/core/src/modules/quota/entities/quota.ts
index 561f64022d..8b8d763afa 100644
--- a/packages/frontend/core/src/modules/quota/entities/quota.ts
+++ b/packages/frontend/core/src/modules/quota/entities/quota.ts
@@ -6,13 +6,15 @@ import {
catchErrorInto,
effect,
Entity,
+ exhaustMapSwitchUntilChanged,
fromPromise,
LiveData,
- mapInto,
onComplete,
onStart,
} from '@toeverything/infra';
-import { exhaustMap } from 'rxjs';
+import { cssVarV2 } from '@toeverything/theme/v2';
+import bytes from 'bytes';
+import { EMPTY, map, mergeMap } from 'rxjs';
import { isBackendError, isNetworkError } from '../../cloud';
import type { WorkspaceQuotaStore } from '../stores/quota';
@@ -26,6 +28,38 @@ export class WorkspaceQuota extends Entity {
isLoading$ = new LiveData(false);
error$ = new LiveData<any>(null);
+ /** Used storage in bytes */
+ used$ = new LiveData<number | null>(null);
+ /** Formatted used storage */
+ usedFormatted$ = this.used$.map(used =>
+ used !== null ? bytes.format(used) : null
+ );
+ /** Maximum storage limit in bytes */
+ max$ = this.quota$.map(quota => (quota ? quota.storageQuota : null));
+ /** Maximum storage limit formatted */
+ maxFormatted$ = this.max$.map(max => (max ? bytes.format(max) : null));
+
+ /** Percentage of storage used */
+ percent$ = LiveData.computed(get => {
+ const max = get(this.max$);
+ const used = get(this.used$);
+ if (max === null || used === null) {
+ return null;
+ }
+ return Math.min(
+ 100,
+ Math.max(0.5, Number(((used / max) * 100).toFixed(4)))
+ );
+ });
+
+ color$ = this.percent$.map(percent =>
+ percent !== null
+ ? percent > 80
+ ? cssVarV2('status/error')
+ : cssVarV2('toast/iconState/regular')
+ : null
+ );
+
constructor(
private readonly workspaceService: WorkspaceService,
private readonly store: WorkspaceQuotaStore
@@ -34,28 +68,59 @@ export class WorkspaceQuota extends Entity {
}
revalidate = effect(
- exhaustMap(() => {
- return fromPromise(signal =>
- this.store.fetchWorkspaceQuota(
- this.workspaceService.workspace.id,
- signal
- )
- ).pipe(
- backoffRetry({
- when: isNetworkError,
- count: Infinity,
- }),
- backoffRetry({
- when: isBackendError,
- count: 3,
- }),
- mapInto(this.quota$),
- catchErrorInto(this.error$, error => {
- logger.error('Failed to fetch isOwner', error);
- }),
- onStart(() => this.isLoading$.setValue(true)),
- onComplete(() => this.isLoading$.setValue(false))
- );
- })
+ map(() => ({
+ workspaceId: this.workspaceService.workspace.id,
+ })),
+ exhaustMapSwitchUntilChanged(
+ (a, b) => a.workspaceId === b.workspaceId,
+ ({ workspaceId }) => {
+ return fromPromise(async signal => {
+ if (!workspaceId) {
+ return; // no quota if no workspace
+ }
+ const data = await this.store.fetchWorkspaceQuota(
+ this.workspaceService.workspace.id,
+ signal
+ );
+ return { quota: data, used: data.usedSize };
+ }).pipe(
+ backoffRetry({
+ when: isNetworkError,
+ count: Infinity,
+ }),
+ backoffRetry({
+ when: isBackendError,
+ count: 3,
+ }),
+ mergeMap(data => {
+ if (data) {
+ const { quota, used } = data;
+ this.quota$.next(quota);
+ this.used$.next(used);
+ } else {
+ this.quota$.next(null);
+ this.used$.next(null);
+ }
+ return EMPTY;
+ }),
+ catchErrorInto(this.error$, error => {
+ logger.error('Failed to fetch workspace quota', error);
+ }),
+ onStart(() => this.isLoading$.setValue(true)),
+ onComplete(() => this.isLoading$.setValue(false))
+ );
+ }
+ )
);
+
+ reset() {
+ this.quota$.next(null);
+ this.used$.next(null);
+ this.error$.next(null);
+ this.isLoading$.next(false);
+ }
+
+ override dispose(): void {
+ this.revalidate.unsubscribe();
+ }
}
diff --git a/packages/frontend/i18n/src/resources/en.json b/packages/frontend/i18n/src/resources/en.json
index 256eda0c75..c0c0cb5e23 100644
--- a/packages/frontend/i18n/src/resources/en.json
+++ b/packages/frontend/i18n/src/resources/en.json
@@ -1590,5 +1590,6 @@
"com.affine.upgrade-to-team-page.create-and-upgrade-confirm.title": "Name Your Workspace",
"com.affine.upgrade-to-team-page.create-and-upgrade-confirm.description": "A workspace is your virtual space to capture, create and plan as just one person or together as a team.",
"com.affine.upgrade-to-team-page.create-and-upgrade-confirm.placeholder": "Set a workspace name",
- "com.affine.upgrade-to-team-page.create-and-upgrade-confirm.confirm": "Continue to Pricing"
+ "com.affine.upgrade-to-team-page.create-and-upgrade-confirm.confirm": "Continue to Pricing",
+ "com.affine.workspace.storage": "Workspace storage"
}