-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Simplify <Editor> with hooks
- Loading branch information
Showing
8 changed files
with
210 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useMemo } from 'react' | ||
import CollabProvider from 'lib/collab/provider' | ||
|
||
function useCollabProvider({ docVersion, docId, serviceClient }) { | ||
return useMemo( | ||
() => { | ||
if (serviceClient && docVersion !== undefined) { | ||
const provider = new CollabProvider( | ||
{ version: docVersion, docId }, | ||
serviceClient | ||
) | ||
// The following object is defined in an Atlassian API. | ||
// `provider` expects a Promise, even if we wouldn't | ||
// need it ourselves. | ||
return { | ||
useNativePlugin: true, | ||
provider: Promise.resolve(provider), | ||
inviteToEditHandler: () => undefined, | ||
isInviteToEditButtonSelected: false, | ||
userId: serviceClient.getSessionId() | ||
} | ||
} else { | ||
return null | ||
} | ||
}, | ||
[docId, docVersion, serviceClient] | ||
) | ||
} | ||
|
||
export default useCollabProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useCallback } from 'react' | ||
|
||
function useForceSync({ doc, docId, serviceClient, collabProvider }) { | ||
// Be sure to save everything before leaving | ||
// and force sync with the io.cozy.file | ||
const forceSync = useCallback( | ||
async function forceSync() { | ||
if (doc && collabProvider && serviceClient) { | ||
// wait for every event to finish | ||
const provider = await collabProvider.provider | ||
await provider.channel.ensureEmptyQueue() | ||
// then force a server sync | ||
await serviceClient.sync(docId) | ||
} | ||
}, | ||
[docId, doc, collabProvider] | ||
) | ||
|
||
// Sync on unload will probably be stopped by the browser, | ||
// as most async code on unload, but let's try anyway | ||
const emergencySync = useCallback( | ||
function() { | ||
if (doc && docId && serviceClient) { | ||
serviceClient.sync(docId) // force a server sync | ||
} | ||
}, | ||
[docId, doc, serviceClient] | ||
) | ||
|
||
return { forceSync, emergencySync } | ||
} | ||
|
||
export default useForceSync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useState, useEffect } from 'react' | ||
|
||
function useNote({ serviceClient, noteId }) { | ||
const [docId, setDocId] = useState(noteId) | ||
const [loading, setLoading] = useState(true) | ||
const [doc, setDoc] = useState(undefined) | ||
const [title, setTitle] = useState(undefined) | ||
|
||
// reload if ever noteId changes | ||
useEffect( | ||
() => { | ||
if (docId !== noteId) { | ||
setLoading(true) | ||
setTitle(undefined) | ||
setDoc(undefined) | ||
setDocId(noteId) | ||
} | ||
}, | ||
[noteId, docId, setLoading, setTitle, setDoc, setDocId] | ||
) | ||
|
||
// load the note | ||
useEffect( | ||
() => { | ||
const loadNote = async function() { | ||
try { | ||
if (!loading) setLoading(true) | ||
const doc = await serviceClient.getDoc(docId) | ||
setTitle(doc.title || '') | ||
setDoc(doc) | ||
} catch (e) { | ||
setTitle(false) | ||
setDoc(false) | ||
// eslint-disable-next-line no-console | ||
console.warn(`Could not load note ${docId}`) | ||
} | ||
setLoading(false) | ||
} | ||
if (serviceClient) { | ||
if (!doc || doc.file.id != docId) loadNote() | ||
} | ||
}, | ||
[docId, setLoading, serviceClient, setDoc, setTitle] | ||
) | ||
|
||
if (docId == noteId) return { loading, title, doc, setTitle } | ||
else return { loading: true, title: undefined, doc: undefined, setTitle } | ||
} | ||
|
||
export default useNote |
Oops, something went wrong.