1
1
import randomWords from "random-words" ;
2
2
import { v4 as uuid } from "uuid" ;
3
- import * as React from "react" ;
3
+ import React , { useMemo , useState , useEffect , useCallback } from "react" ;
4
4
import { connectToDB , getConnection } from "./sharedb" ;
5
+ import { useTransition } from "./react-experimental" ;
5
6
import {
6
7
Link ,
7
8
BrowserRouter as Router ,
8
9
useHistory ,
9
10
useLocation ,
10
11
} from "react-router-dom" ;
11
12
12
- const useTransition : ( ) => [ ( fn : Function ) => void , boolean ] = ( React as any )
13
- . unstable_useTransition ;
14
-
15
13
interface Node {
16
14
text : string ;
17
15
}
@@ -35,11 +33,11 @@ function useFlow(config: {
35
33
36
34
const [ startTransition , isPending ] = useTransition ( ) ;
37
35
38
- const [ state , setState ] = React . useState < Flow | null > ( null ) ;
36
+ const [ state , setState ] = useState < Flow | null > ( null ) ;
39
37
40
- const doc = React . useMemo ( ( ) => getConnection ( config . id ) , [ config . id ] ) ;
38
+ const doc = useMemo ( ( ) => getConnection ( config . id ) , [ config . id ] ) ;
41
39
42
- React . useEffect ( ( ) => {
40
+ useEffect ( ( ) => {
43
41
const cloneStateFromShareDB = ( ) =>
44
42
startTransition ( ( ) => {
45
43
setState ( JSON . parse ( JSON . stringify ( doc . data ) ) ) ;
@@ -58,18 +56,18 @@ function useFlow(config: {
58
56
59
57
// Methods
60
58
61
- const addNode = React . useCallback ( ( ) => {
59
+ const addNode = useCallback ( ( ) => {
62
60
doc . submitOp ( [ { p : [ "nodes" , uuid ( ) ] , oi : { text : randomWords ( ) } } ] ) ;
63
61
} , [ doc ] ) ;
64
62
65
- const removeNode = React . useCallback (
63
+ const removeNode = useCallback (
66
64
( id ) => {
67
65
doc . submitOp ( [ { p : [ "nodes" , id ] , od : { } } ] ) ;
68
66
} ,
69
67
[ doc ]
70
68
) ;
71
69
72
- const reset = React . useCallback (
70
+ const reset = useCallback (
73
71
( flow ) => {
74
72
doc . submitOp ( [ { p : [ ] , od : doc . data , oi : flow } ] ) ;
75
73
} ,
@@ -180,15 +178,15 @@ const App = () => {
180
178
const history = useHistory ( ) ;
181
179
const location = useLocation ( ) ;
182
180
183
- const id = React . useMemo ( ( ) => {
181
+ const id = useMemo ( ( ) => {
184
182
if ( location . hash . length < 2 ) {
185
183
return null ;
186
184
}
187
185
return location . hash . slice ( 1 ) ;
188
186
} , [ location ] ) ;
189
187
190
188
// If there is no ID readable from the hash, redirect to a freshly created one
191
- React . useEffect ( ( ) => {
189
+ useEffect ( ( ) => {
192
190
if ( id === null ) {
193
191
history . push ( `#${ randomWords ( ) } ` ) ;
194
192
}
0 commit comments