11import React , {
2- useState , useCallback , useEffect , useContext ,
2+ useState , useCallback , useContext ,
33} from 'react' ;
44import PropTypes from 'prop-types' ;
5+ import useEffect from 'use-deep-compare-effect' ;
56
67import {
78 getContentFromFile , saveFile , ensureFile , deleteFile ,
89} from './helpers' ;
910import {
10- FileCard , FileForm , useBlob , RepositoryContext , AuthenticationContext ,
11+ FileCard , FileForm , useBlob , RepositoryContext ,
1112} from '..' ;
1213
1314function useFile ( {
@@ -16,14 +17,14 @@ function useFile({
1617 filepath,
1718 onFilepath,
1819 defaultContent,
19- config,
20- create = false ,
20+ config : _config ,
21+ create= false ,
2122} ) {
2223 const [ file , setFile ] = useState ( ) ;
2324 const [ blob , setBlob ] = useState ( ) ;
24- const { actions : repositoryActions } = useContext ( RepositoryContext ) ;
25- const { state : contextAuthentication , config : contextConfig } = useContext ( AuthenticationContext ) ;
25+ const { actions : { updateBranch } , config : repositoryConfig } = useContext ( RepositoryContext ) ;
2626
27+ const config = _config || repositoryConfig ;
2728 const branch = repository && ( repository . branch || repository . default_branch ) ;
2829
2930 const [ deleted , setDeleted ] = useState ( ) ;
@@ -41,14 +42,18 @@ function useFile({
4142 } , [ ] ) ;
4243
4344 const read = useCallback ( async ( _filepath ) => {
44- onFilepath && await onFilepath ( _filepath ) ;
45+ if ( onFilepath ) {
46+ await onFilepath ( _filepath ) ;
47+ } ;
4548 } , [ onFilepath ] ) ;
4649
4750 const load = useCallback ( async ( ) => {
4851 if ( config && repository && filepath ) {
4952 const _file = await ensureFile ( {
5053 filepath, defaultContent, authentication, config, repository, branch,
5154 } ) ;
55+ // let content;
56+ // content = await repositoryActions.fileFromZip(filepath);
5257 const content = await getContentFromFile ( _file ) ;
5358
5459 update ( {
@@ -57,7 +62,9 @@ function useFile({
5762 }
5863 } , [ authentication , branch , config , defaultContent , filepath , repository , update ] ) ;
5964
60- const createFile = useCallback ( async ( { branch : _branch , filepath : _filepath , defaultContent : _defaultContent } ) => {
65+ const createFile = useCallback ( async ( {
66+ branch : _branch , filepath : _filepath , defaultContent : _defaultContent ,
67+ } ) => {
6168 if ( config && repository ) {
6269 const _file = await ensureFile ( {
6370 authentication, config, repository,
@@ -67,15 +74,20 @@ function useFile({
6774 } ) ;
6875
6976 if ( _file ) {
70- repositoryActions . updateBranch ( _branch ) ;
77+ updateBranch ( _branch ) ;
7178 onFilepath ( _filepath ) ;
7279 } ;
7380 }
74- } , [ authentication , config , repository ] ) ;
81+ } , [ authentication , config , repository , updateBranch , onFilepath ] ) ;
7582
7683 const close = useCallback ( ( ) => {
77- if ( blobActions && blobActions . close ) blobActions . close ( ) ;
78- if ( onFilepath ) onFilepath ( ) ;
84+ if ( blobActions && blobActions . close ) {
85+ blobActions . close ( ) ;
86+ } ;
87+
88+ if ( onFilepath ) {
89+ onFilepath ( ) ;
90+ } ;
7991 update ( ) ;
8092 } , [ update , blobActions , onFilepath ] ) ;
8193
@@ -110,14 +122,13 @@ function useFile({
110122 const blobFilepath = blobState && blobState . filepath ;
111123
112124 useEffect ( ( ) => {
113- if ( blobFilepath && onFilepath ) onFilepath ( blobFilepath ) ;
125+ if ( blobFilepath && onFilepath ) {
126+ onFilepath ( blobFilepath ) ;
127+ } ;
114128 } , [ blobFilepath , onFilepath ] ) ;
115129
116130 useEffect ( ( ) => { // if there is a file but no repository, close file.
117- if ( ! repository && file ) {
118- close ( ) ;
119- }
120- if ( ! contextAuthentication ) close ( ) ;
131+ if ( ! repository && file ) close ( ) ;
121132 } , [ repository , file , close ] ) ;
122133
123134 useEffect ( ( ) => {
0 commit comments