@@ -2,9 +2,12 @@ import { Hint } from 'codemirror'
2
2
import throttle from 'lodash.throttle'
3
3
import { useCallback , useEffect , useMemo , useRef , useState } from 'react'
4
4
import { YText , YEvent } from 'yjs/dist/src/internals'
5
+ import { useToast } from '../../../../design/lib/stores/toast'
5
6
import { buildIconUrl } from '../../../api/files'
7
+ import { buildTeamFileUrl , uploadFile } from '../../../api/teams/files'
6
8
import { getDocLinkHref } from '../../../components/Link/DocLink'
7
9
import { SerializedDocWithSupplemental } from '../../../interfaces/db/doc'
10
+ import { SerializedSubscription } from '../../../interfaces/db/subscription'
8
11
import { SerializedTeam } from '../../../interfaces/db/team'
9
12
import { SerializedTemplate } from '../../../interfaces/db/template'
10
13
import { SerializedUser } from '../../../interfaces/db/user'
@@ -20,20 +23,25 @@ import {
20
23
import { useLocalSnapshot } from '../../stores/localSnapshots'
21
24
import { useNav } from '../../stores/nav'
22
25
import { CodeMirrorKeyMap , useSettings } from '../../stores/settings'
26
+ import { freePlanUploadSizeMb , paidPlanUploadSizeMb } from '../../subscription'
27
+ import { nginxSizeLimitInMb } from '../../upload'
28
+ import { bytesToMegaBytes } from '../../utils/bytes'
23
29
import { getColorFromString , getRandomColor } from '../../utils/string'
24
30
25
31
interface DocEditorHookProps {
26
32
doc : SerializedDocWithSupplemental
27
33
collaborationToken : string
28
34
team ?: SerializedTeam
29
35
user ?: SerializedUser
36
+ subscription ?: SerializedSubscription
30
37
}
31
38
32
39
export function useDocEditor ( {
33
40
doc,
34
41
collaborationToken,
35
42
user,
36
43
team,
44
+ subscription,
37
45
} : DocEditorHookProps ) {
38
46
const { settings } = useSettings ( )
39
47
const [ editorContent , setEditorContent ] = useState ( '' )
@@ -45,6 +53,7 @@ export function useDocEditor({
45
53
} | null > ( null )
46
54
const suggestionsRef = useRef < Hint [ ] > ( [ ] )
47
55
const { loadDoc } = useNav ( )
56
+ const { pushApiErrorMessage, pushMessage } = useToast ( )
48
57
const [ selection , setSelection ] = useState < SelectionState > ( {
49
58
currentCursor : {
50
59
line : 0 ,
@@ -342,6 +351,37 @@ export function useDocEditor({
342
351
return undefined
343
352
} , [ realtime , updateContent ] )
344
353
354
+ useEffect ( ( ) => {
355
+ if ( team != null ) {
356
+ fileUploadHandlerRef . current = async ( file ) => {
357
+ if ( bytesToMegaBytes ( file . size ) > nginxSizeLimitInMb ) {
358
+ pushMessage ( {
359
+ title : '' ,
360
+ description : `File size exceeding limit. ${
361
+ subscription == null ? freePlanUploadSizeMb : paidPlanUploadSizeMb
362
+ } Mb limit per upload allowed.`,
363
+ } )
364
+ return null
365
+ }
366
+ try {
367
+ const { file : fileInfo } = await uploadFile ( team , file , doc )
368
+ const url = buildTeamFileUrl ( team , fileInfo . name )
369
+ if ( file . type . match ( / i m a g e \/ .* / ) ) {
370
+ return { type : 'img' , url, alt : file . name }
371
+ } else {
372
+ return { type : 'file' , url, title : file . name }
373
+ }
374
+ } catch ( err ) {
375
+ console . log ( err )
376
+ pushApiErrorMessage ( err )
377
+ return null
378
+ }
379
+ }
380
+ } else {
381
+ fileUploadHandlerRef . current = undefined
382
+ }
383
+ } , [ team , pushMessage , pushApiErrorMessage , subscription , doc ] )
384
+
345
385
return {
346
386
editorConfig,
347
387
editorContent,
0 commit comments