diff --git a/app/assets/javascripts/Components/Preferences/Panes/Account/Files.tsx b/app/assets/javascripts/Components/Preferences/Panes/Account/Files.tsx index 237e099de..7861fda8c 100644 --- a/app/assets/javascripts/Components/Preferences/Panes/Account/Files.tsx +++ b/app/assets/javascripts/Components/Preferences/Panes/Account/Files.tsx @@ -1,36 +1,71 @@ +import { WebApplication } from '@/UIModels/Application' import { AppState } from '@/UIModels/AppState' import { formatSizeToReadableString } from '@standardnotes/filepicker' +import { SubscriptionSettingName } from '@standardnotes/snjs' import { observer } from 'mobx-react-lite' import { FunctionComponent } from 'preact' +import { useEffect, useState } from 'preact/hooks' import { PreferencesGroup, PreferencesSegment, Subtitle, Title } from '../../PreferencesComponents' type Props = { + application: WebApplication appState: AppState } -export const FilesSection: FunctionComponent = observer(({ appState }) => { - if (!appState.features.isEntitledToFiles) { +export const FilesSection: FunctionComponent = observer(({ application, appState }) => { + if (!application.getUser() || !appState.features.isEntitledToFiles) { return null } - const filesQuotaUsed = appState.files.filesQuotaUsed - const filesQuotaTotal = appState.files.filesQuotaTotal + const [isLoading, setIsLoading] = useState(true) + const [filesQuotaUsed, setFilesQuotaUsed] = useState(0) + const [filesQuotaTotal, setFilesQuotaTotal] = useState(0) + + useEffect(() => { + const getFilesQuota = async () => { + const filesQuotaUsed = await application.settings.getSubscriptionSetting( + SubscriptionSettingName.FileUploadBytesUsed, + ) + const filesQuotaTotal = await application.settings.getSubscriptionSetting( + SubscriptionSettingName.FileUploadBytesLimit, + ) + + if (filesQuotaUsed) { + setFilesQuotaUsed(parseFloat(filesQuotaUsed)) + } + if (filesQuotaTotal) { + setFilesQuotaTotal(parseFloat(filesQuotaTotal)) + } + + setIsLoading(false) + } + + getFilesQuota().catch(console.error) + }) return ( Files Storage Quota -
- {formatSizeToReadableString(filesQuotaUsed)} of{' '} - {formatSizeToReadableString(filesQuotaTotal)} used -
- + {isLoading ? ( +
+
+
+ ) : ( + <> +
+ {formatSizeToReadableString(filesQuotaUsed)} of{' '} + {formatSizeToReadableString(filesQuotaTotal)} used +
+ + + )}
) diff --git a/app/assets/javascripts/Components/Preferences/Panes/Account/index.tsx b/app/assets/javascripts/Components/Preferences/Panes/Account/index.tsx index 302404789..4af12d87b 100644 --- a/app/assets/javascripts/Components/Preferences/Panes/Account/index.tsx +++ b/app/assets/javascripts/Components/Preferences/Panes/Account/index.tsx @@ -25,7 +25,7 @@ export const AccountPreferences = observer(({ application, appState }: Props) => )} - + )) diff --git a/app/assets/javascripts/UIModels/AppState/AppState.ts b/app/assets/javascripts/UIModels/AppState/AppState.ts index cd151266c..0697efaff 100644 --- a/app/assets/javascripts/UIModels/AppState/AppState.ts +++ b/app/assets/javascripts/UIModels/AppState/AppState.ts @@ -110,7 +110,7 @@ export class AppState { this.subscription = new SubscriptionState(application, this.appEventObserverRemovers) this.purchaseFlow = new PurchaseFlowState(application) this.notesView = new NotesViewState(application, this, this.appEventObserverRemovers) - this.files = new FilesState(application, this.appEventObserverRemovers) + this.files = new FilesState(application) this.addAppEventObserver() this.streamNotesAndTags() this.onVisibilityChange = () => { diff --git a/app/assets/javascripts/UIModels/AppState/FilesState.ts b/app/assets/javascripts/UIModels/AppState/FilesState.ts index 1903f5c58..1482de088 100644 --- a/app/assets/javascripts/UIModels/AppState/FilesState.ts +++ b/app/assets/javascripts/UIModels/AppState/FilesState.ts @@ -7,61 +7,12 @@ import { ClassicFileSaver, parseFileName, } from '@standardnotes/filepicker' -import { - ApplicationEvent, - ClientDisplayableError, - ContentType, - SNFile, - SubscriptionSettingName, -} from '@standardnotes/snjs' +import { ClientDisplayableError, SNFile } from '@standardnotes/snjs' import { addToast, dismissToast, ToastType } from '@standardnotes/stylekit' -import { action, makeObservable, observable } from 'mobx' - import { WebApplication } from '../Application' export class FilesState { - filesQuotaUsed: number - filesQuotaTotal: number - - constructor(private application: WebApplication, appObservers: (() => void)[]) { - this.filesQuotaUsed = 0 - this.filesQuotaTotal = 0 - - makeObservable(this, { - filesQuotaUsed: observable, - filesQuotaTotal: observable, - getFilesQuota: action, - }) - - appObservers.push( - application.addEventObserver(async (event) => { - switch (event) { - case ApplicationEvent.Launched: - case ApplicationEvent.SignedIn: - this.getFilesQuota().catch(console.error) - } - }), - application.streamItems(ContentType.File, () => { - this.getFilesQuota().catch(console.error) - }), - ) - } - - public async getFilesQuota() { - const filesQuotaUsed = await this.application.settings.getSubscriptionSetting( - SubscriptionSettingName.FileUploadBytesUsed, - ) - const filesQuotaTotal = await this.application.settings.getSubscriptionSetting( - SubscriptionSettingName.FileUploadBytesLimit, - ) - - if (filesQuotaUsed) { - this.filesQuotaUsed = parseFloat(filesQuotaUsed) - } - if (filesQuotaTotal) { - this.filesQuotaTotal = parseFloat(filesQuotaTotal) - } - } + constructor(private application: WebApplication) {} public async downloadFile(file: SNFile): Promise { let downloadingToastId = ''