feat: only fetch files quota when prefs are opened (#999)

This commit is contained in:
Aman Harwara
2022-04-26 00:12:17 +05:30
committed by GitHub
parent e8ed38bcb2
commit 50d047a903
4 changed files with 53 additions and 67 deletions

View File

@@ -1,36 +1,71 @@
import { WebApplication } from '@/UIModels/Application'
import { AppState } from '@/UIModels/AppState' import { AppState } from '@/UIModels/AppState'
import { formatSizeToReadableString } from '@standardnotes/filepicker' import { formatSizeToReadableString } from '@standardnotes/filepicker'
import { SubscriptionSettingName } from '@standardnotes/snjs'
import { observer } from 'mobx-react-lite' import { observer } from 'mobx-react-lite'
import { FunctionComponent } from 'preact' import { FunctionComponent } from 'preact'
import { useEffect, useState } from 'preact/hooks'
import { PreferencesGroup, PreferencesSegment, Subtitle, Title } from '../../PreferencesComponents' import { PreferencesGroup, PreferencesSegment, Subtitle, Title } from '../../PreferencesComponents'
type Props = { type Props = {
application: WebApplication
appState: AppState appState: AppState
} }
export const FilesSection: FunctionComponent<Props> = observer(({ appState }) => { export const FilesSection: FunctionComponent<Props> = observer(({ application, appState }) => {
if (!appState.features.isEntitledToFiles) { if (!application.getUser() || !appState.features.isEntitledToFiles) {
return null return null
} }
const filesQuotaUsed = appState.files.filesQuotaUsed const [isLoading, setIsLoading] = useState(true)
const filesQuotaTotal = appState.files.filesQuotaTotal const [filesQuotaUsed, setFilesQuotaUsed] = useState<number>(0)
const [filesQuotaTotal, setFilesQuotaTotal] = useState<number>(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 ( return (
<PreferencesGroup> <PreferencesGroup>
<PreferencesSegment> <PreferencesSegment>
<Title>Files</Title> <Title>Files</Title>
<Subtitle>Storage Quota</Subtitle> <Subtitle>Storage Quota</Subtitle>
<div className="mt-1 mb-1"> {isLoading ? (
<span className="font-semibold">{formatSizeToReadableString(filesQuotaUsed)}</span> of{' '} <div className="mt-2">
<span>{formatSizeToReadableString(filesQuotaTotal)}</span> used <div className="sk-spinner spinner-info w-3 h-3"></div>
</div> </div>
<progress ) : (
className="w-full progress-bar" <>
aria-label="Files storage used" <div className="mt-1 mb-1">
value={appState.files.filesQuotaUsed} <span className="font-semibold">{formatSizeToReadableString(filesQuotaUsed)}</span> of{' '}
max={filesQuotaTotal} <span>{formatSizeToReadableString(filesQuotaTotal)}</span> used
/> </div>
<progress
className="w-full progress-bar"
aria-label="Files storage used"
value={filesQuotaUsed}
max={filesQuotaTotal}
/>
</>
)}
</PreferencesSegment> </PreferencesSegment>
</PreferencesGroup> </PreferencesGroup>
) )

View File

@@ -25,7 +25,7 @@ export const AccountPreferences = observer(({ application, appState }: Props) =>
</> </>
)} )}
<Subscription application={application} appState={appState} /> <Subscription application={application} appState={appState} />
<FilesSection appState={appState} /> <FilesSection application={application} appState={appState} />
<SignOutWrapper application={application} appState={appState} /> <SignOutWrapper application={application} appState={appState} />
</PreferencesPane> </PreferencesPane>
)) ))

View File

@@ -110,7 +110,7 @@ export class AppState {
this.subscription = new SubscriptionState(application, this.appEventObserverRemovers) this.subscription = new SubscriptionState(application, this.appEventObserverRemovers)
this.purchaseFlow = new PurchaseFlowState(application) this.purchaseFlow = new PurchaseFlowState(application)
this.notesView = new NotesViewState(application, this, this.appEventObserverRemovers) this.notesView = new NotesViewState(application, this, this.appEventObserverRemovers)
this.files = new FilesState(application, this.appEventObserverRemovers) this.files = new FilesState(application)
this.addAppEventObserver() this.addAppEventObserver()
this.streamNotesAndTags() this.streamNotesAndTags()
this.onVisibilityChange = () => { this.onVisibilityChange = () => {

View File

@@ -7,61 +7,12 @@ import {
ClassicFileSaver, ClassicFileSaver,
parseFileName, parseFileName,
} from '@standardnotes/filepicker' } from '@standardnotes/filepicker'
import { import { ClientDisplayableError, SNFile } from '@standardnotes/snjs'
ApplicationEvent,
ClientDisplayableError,
ContentType,
SNFile,
SubscriptionSettingName,
} from '@standardnotes/snjs'
import { addToast, dismissToast, ToastType } from '@standardnotes/stylekit' import { addToast, dismissToast, ToastType } from '@standardnotes/stylekit'
import { action, makeObservable, observable } from 'mobx'
import { WebApplication } from '../Application' import { WebApplication } from '../Application'
export class FilesState { export class FilesState {
filesQuotaUsed: number constructor(private application: WebApplication) {}
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)
}
}
public async downloadFile(file: SNFile): Promise<void> { public async downloadFile(file: SNFile): Promise<void> {
let downloadingToastId = '' let downloadingToastId = ''