chore: show invite count bubble on preferences button (#2458)
This commit is contained in:
17
packages/services/src/Domain/Preferences/PreferenceId.ts
Normal file
17
packages/services/src/Domain/Preferences/PreferenceId.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
const PREFERENCE_PANE_IDS = [
|
||||
'general',
|
||||
'account',
|
||||
'security',
|
||||
'home-server',
|
||||
'vaults',
|
||||
'appearance',
|
||||
'backups',
|
||||
'listed',
|
||||
'shortcuts',
|
||||
'accessibility',
|
||||
'get-free-month',
|
||||
'help-feedback',
|
||||
'whats-new',
|
||||
] as const
|
||||
|
||||
export type PreferencePaneId = (typeof PREFERENCE_PANE_IDS)[number]
|
||||
@@ -1,10 +1,44 @@
|
||||
import { removeFromArray } from '@standardnotes/utils'
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
import { StatusServiceEvent, StatusServiceInterface, StatusMessageIdentifier } from './StatusServiceInterface'
|
||||
import { PreferencePaneId } from '../Preferences/PreferenceId'
|
||||
|
||||
/* istanbul ignore file */
|
||||
|
||||
export class StatusService extends AbstractService<StatusServiceEvent, string> implements StatusServiceInterface {
|
||||
private preferencesBubbleCounts: Record<PreferencePaneId, number> = {
|
||||
general: 0,
|
||||
account: 0,
|
||||
security: 0,
|
||||
'home-server': 0,
|
||||
vaults: 0,
|
||||
appearance: 0,
|
||||
backups: 0,
|
||||
listed: 0,
|
||||
shortcuts: 0,
|
||||
accessibility: 0,
|
||||
'get-free-month': 0,
|
||||
'help-feedback': 0,
|
||||
'whats-new': 0,
|
||||
}
|
||||
|
||||
getPreferencesBubbleCount(preferencePaneId: PreferencePaneId): number {
|
||||
return this.preferencesBubbleCounts[preferencePaneId]
|
||||
}
|
||||
|
||||
setPreferencesBubbleCount(preferencePaneId: PreferencePaneId, count: number): void {
|
||||
this.preferencesBubbleCounts[preferencePaneId] = count
|
||||
const totalCount = this.totalPreferencesBubbleCount
|
||||
void this.notifyEvent(
|
||||
StatusServiceEvent.PreferencesBubbleCountChanged,
|
||||
totalCount > 0 ? totalCount.toString() : undefined,
|
||||
)
|
||||
}
|
||||
|
||||
get totalPreferencesBubbleCount(): number {
|
||||
return Object.values(this.preferencesBubbleCounts).reduce((total, count) => total + count, 0)
|
||||
}
|
||||
|
||||
private _message = ''
|
||||
private directSetMessage?: string
|
||||
private dynamicMessages: string[] = []
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
import { PreferencePaneId } from '../Preferences/PreferenceId'
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
|
||||
/* istanbul ignore file */
|
||||
|
||||
export enum StatusServiceEvent {
|
||||
MessageChanged = 'MessageChanged',
|
||||
PreferencesBubbleCountChanged = 'PreferencesBubbleCountChanged',
|
||||
}
|
||||
|
||||
export type StatusMessageIdentifier = string
|
||||
|
||||
export interface StatusServiceInterface extends AbstractService<StatusServiceEvent, string> {
|
||||
getPreferencesBubbleCount(preferencePaneId: PreferencePaneId): number
|
||||
setPreferencesBubbleCount(preferencePaneId: PreferencePaneId, count: number): void
|
||||
get totalPreferencesBubbleCount(): number
|
||||
|
||||
get message(): string
|
||||
setMessage(message: string | undefined): void
|
||||
addMessage(message: string): StatusMessageIdentifier
|
||||
|
||||
@@ -37,6 +37,8 @@ import { AbstractService } from './../Service/AbstractService'
|
||||
import { VaultInviteServiceEvent } from './VaultInviteServiceEvent'
|
||||
import { GetKeyPairs } from '../Encryption/UseCase/GetKeyPairs'
|
||||
import { DecryptErroredPayloads } from '../Encryption/UseCase/DecryptErroredPayloads'
|
||||
import { StatusServiceInterface } from '../Status/StatusServiceInterface'
|
||||
import { ApplicationEvent } from '../Event/ApplicationEvent'
|
||||
import { WebSocketsServiceEvent } from '../Api/WebSocketsServiceEvent'
|
||||
|
||||
export class VaultInviteService
|
||||
@@ -51,6 +53,7 @@ export class VaultInviteService
|
||||
private vaultUsers: VaultUserServiceInterface,
|
||||
private sync: SyncServiceInterface,
|
||||
private invitesServer: SharedVaultInvitesServer,
|
||||
private status: StatusServiceInterface,
|
||||
private _getAllContacts: GetAllContacts,
|
||||
private _getVault: GetVault,
|
||||
private _getVaultContacts: GetVaultContacts,
|
||||
@@ -96,11 +99,28 @@ export class VaultInviteService
|
||||
this.pendingInvites = {}
|
||||
}
|
||||
|
||||
updatePendingInviteCount() {
|
||||
this.status.setPreferencesBubbleCount('vaults', Object.keys(this.pendingInvites).length)
|
||||
}
|
||||
|
||||
addPendingInvite(invite: InviteRecord): void {
|
||||
this.pendingInvites[invite.invite.uuid] = invite
|
||||
this.updatePendingInviteCount()
|
||||
}
|
||||
|
||||
removePendingInvite(uuid: string): void {
|
||||
delete this.pendingInvites[uuid]
|
||||
this.updatePendingInviteCount()
|
||||
}
|
||||
|
||||
async handleEvent(event: InternalEventInterface): Promise<void> {
|
||||
switch (event.type) {
|
||||
case SyncEvent.ReceivedSharedVaultInvites:
|
||||
await this.processInboundInvites(event.payload as SyncEventReceivedSharedVaultInvitesData)
|
||||
break
|
||||
case ApplicationEvent.Launched:
|
||||
void this.downloadInboundInvites()
|
||||
break
|
||||
case WebSocketsServiceEvent.UserInvitedToSharedVault:
|
||||
await this.processInboundInvites([(event as UserInvitedToSharedVaultEvent).payload.invite])
|
||||
break
|
||||
@@ -154,7 +174,7 @@ export class VaultInviteService
|
||||
return Result.fail(acceptResult.getError())
|
||||
}
|
||||
|
||||
delete this.pendingInvites[pendingInvite.invite.uuid]
|
||||
this.removePendingInvite(pendingInvite.invite.uuid)
|
||||
|
||||
void this.sync.sync()
|
||||
|
||||
@@ -242,7 +262,7 @@ export class VaultInviteService
|
||||
return ClientDisplayableError.FromString(`Failed to delete invite ${JSON.stringify(response)}`)
|
||||
}
|
||||
|
||||
delete this.pendingInvites[invite.uuid]
|
||||
this.removePendingInvite(invite.uuid)
|
||||
}
|
||||
|
||||
private async reprocessCachedInvitesTrustStatusAfterTrustedContactsChange(): Promise<void> {
|
||||
@@ -253,6 +273,7 @@ export class VaultInviteService
|
||||
|
||||
private async processInboundInvites(invites: SharedVaultInviteServerHash[]): Promise<void> {
|
||||
if (invites.length === 0) {
|
||||
this.updatePendingInviteCount()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -274,11 +295,11 @@ export class VaultInviteService
|
||||
})
|
||||
|
||||
if (!trustedMessage.isFailed()) {
|
||||
this.pendingInvites[invite.uuid] = {
|
||||
this.addPendingInvite({
|
||||
invite,
|
||||
message: trustedMessage.getValue(),
|
||||
trusted: true,
|
||||
}
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
@@ -290,11 +311,11 @@ export class VaultInviteService
|
||||
})
|
||||
|
||||
if (!untrustedMessage.isFailed()) {
|
||||
this.pendingInvites[invite.uuid] = {
|
||||
this.addPendingInvite({
|
||||
invite,
|
||||
message: untrustedMessage.getValue(),
|
||||
trusted: false,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -134,6 +134,7 @@ export * from './KeySystem/KeySystemKeyManager'
|
||||
export * from './Mfa/MfaServiceInterface'
|
||||
export * from './Mutator/MutatorClientInterface'
|
||||
export * from './Payloads/PayloadManagerInterface'
|
||||
export * from './Preferences/PreferenceId'
|
||||
export * from './Preferences/PreferenceServiceInterface'
|
||||
export * from './Protection/MobileUnlockTiming'
|
||||
export * from './Protection/ProtectionClientInterface'
|
||||
|
||||
Reference in New Issue
Block a user