1 import type { ConnectionCloseReason } from '@proton/docs-proto'
2 import { BasePropertiesState } from '@proton/docs-shared'
10 } from '@proton/docs-shared'
11 import type { DecryptedCommit } from '../Models/DecryptedCommit'
12 import type { DocumentEntitlements, PublicDocumentEntitlements } from '../Types/DocumentEntitlements'
13 import type { DecryptedNode } from '@proton/drive-store/lib'
15 export type DocumentEvent =
17 name: 'RealtimeConnectionClosed'
18 payload: ConnectionCloseReason
21 name: 'RealtimeFailedToConnect'
25 name: 'RealtimeAttemptingToSendUpdateThatIsTooLarge'
29 name: 'RealtimeReceivedDocumentUpdate'
30 payload: DecryptedMessage
33 name: 'RealtimeRequestingClientToBroadcastItsState'
37 name: 'RealtimeReceivedOtherClientPresenceState'
41 name: 'EditorRequestsPropagationOfUpdate'
43 message: RtsMessagePayload
44 debugSource: BroadcastSource
48 name: 'DriveFileConversionToDocBegan'
52 name: 'DriveFileConversionToDocSucceeded'
56 name: 'DebugMenuRequestingCommitWithRTS'
57 payload: DocumentEntitlements
60 name: 'EditorIsReadyToBeShown'
64 export interface DocumentStateValues {
66 currentCommitId: string | undefined
67 userRole: DocumentRole
68 documentTrashState: DocTrashState
69 baseCommit: DecryptedCommit | undefined
70 documentMeta: DocumentMetaInterface
71 decryptedNode: DecryptedNode
72 entitlements: DocumentEntitlements
75 editorHasRenderingIssue: boolean
77 /** Public documents may not support realtime depending on feature configuration */
78 realtimeEnabled: boolean
79 /** Is set to true after the connection connects AND the server informs us its ready to receive updates */
80 realtimeReadyToBroadcast: boolean
81 /** Is set to true if we are unable to form a connection with the RTS server in a reasonable time */
82 realtimeConnectionTimedOut: boolean
83 realtimeStatus: 'connected' | 'connecting' | 'disconnected'
84 realtimeIsExperiencingErroredSync: boolean
85 realtimeIsLockedDueToSizeContraint: boolean
86 realtimeIsParticipantLimitReached: boolean
89 const DefaultValues: Pick<
95 | 'editorHasRenderingIssue'
97 | 'realtimeReadyToBroadcast'
98 | 'realtimeConnectionTimedOut'
100 | 'realtimeIsExperiencingErroredSync'
101 | 'realtimeIsLockedDueToSizeContraint'
102 | 'realtimeIsParticipantLimitReached'
105 currentCommitId: undefined,
106 baseCommit: undefined,
109 editorHasRenderingIssue: false,
110 realtimeEnabled: true,
111 realtimeConnectionTimedOut: false,
112 realtimeReadyToBroadcast: false,
113 realtimeStatus: 'disconnected',
114 realtimeIsExperiencingErroredSync: false,
115 realtimeIsLockedDueToSizeContraint: false,
116 realtimeIsParticipantLimitReached: false,
119 export function isDocumentState(state: DocumentState | PublicDocumentState): state is DocumentState {
120 return state instanceof DocumentState
124 * Manages the state of a fully resolved document (has nodeMeta, documentMeta, etc)
126 export class DocumentState extends BasePropertiesState<DocumentStateValues, DocumentEvent> {
127 static readonly defaults: typeof DefaultValues = DefaultValues
131 * Same as DocumentState but entitlements are of type PublicDocumentEntitlements
133 export class PublicDocumentState extends BasePropertiesState<
134 Omit<DocumentStateValues, 'entitlements'> & { entitlements: PublicDocumentEntitlements },
137 static readonly defaults: typeof DefaultValues = DefaultValues