2024-04-19 14:51:41 +08:00
import React , { Fragment } from 'react' ;
import PropTypes from 'prop-types' ;
import cookie from 'react-cookies' ;
import { seafileAPI } from '../../utils/seafile-api' ;
import { gettext , canAddPublicRepo } from '../../utils/constants' ;
import { Utils } from '../../utils/utils' ;
import Repo from '../../models/repo' ;
import toaster from '../../components/toast' ;
import Loading from '../../components/loading' ;
import EmptyTip from '../../components/empty-tip' ;
import CommonToolbar from '../../components/toolbar/common-toolbar' ;
import SharedRepoListView from '../../components/shared-repo-list-view/shared-repo-list-view' ;
import SortOptionsDialog from '../../components/dialog/sort-options' ;
import TopToolbar from './top-toolbar' ;
const propTypes = {
onShowSidePanel : PropTypes . func ,
onSearchedClick : PropTypes . func ,
inAllLibs : PropTypes . bool
} ;
class PublicSharedView extends React . Component {
constructor ( props ) {
super ( props ) ;
this . state = {
isLoading : true ,
errMessage : '' ,
repoList : [ ] ,
sortBy : cookie . load ( 'seafile-repo-dir-sort-by' ) || 'name' , // 'name' or 'time' or 'size'
sortOrder : cookie . load ( 'seafile-repo-dir-sort-order' ) || 'asc' , // 'asc' or 'desc'
isSortOptionsDialogOpen : false ,
libraryType : 'public' ,
} ;
}
componentDidMount ( ) {
seafileAPI . listRepos ( { type : 'public' } ) . then ( ( res ) => {
let repoList = res . data . repos . map ( item => {
let repo = new Repo ( item ) ;
return repo ;
} ) ;
this . setState ( {
isLoading : false ,
repoList : Utils . sortRepos ( repoList , this . state . sortBy , this . state . sortOrder )
} ) ;
} ) . catch ( ( error ) => {
this . setState ( {
isLoading : false ,
errorMsg : Utils . getErrorMsg ( error , true ) // true: show login tip if 403
} ) ;
} ) ;
}
onItemUnshare = ( repo ) => {
seafileAPI . unshareRepo ( repo . repo _id , { share _type : 'public' } ) . then ( ( ) => {
let repoList = this . state . repoList . filter ( item => {
return item . repo _id !== repo . repo _id ;
} ) ;
this . setState ( { repoList : repoList } ) ;
let message = gettext ( 'Successfully unshared {name}' ) . replace ( '{name}' , repo . repo _name ) ;
toaster . success ( message ) ;
} ) . catch ( error => {
let errMessage = Utils . getErrorMsg ( error ) ;
if ( errMessage === gettext ( 'Error' ) ) {
errMessage = gettext ( 'Failed to unshare {name}' ) . replace ( '{name}' , repo . repo _name ) ;
}
toaster ( errMessage ) ;
} ) ;
} ;
onItemDelete = ( ) => {
// todo need to optimized
} ;
addRepoItem = ( repo ) => {
let isExist = false ;
let repoIndex = 0 ;
let repoList = this . state . repoList ;
for ( let i = 0 ; i < repoList . length ; i ++ ) {
if ( repo . repo _id === repoList [ i ] . repo _id ) {
isExist = true ;
repoIndex = i ;
break ;
}
}
if ( isExist ) {
this . state . repoList . splice ( repoIndex , 1 ) ;
}
let newRepoList = this . state . repoList . map ( item => { return item ; } ) ;
newRepoList . unshift ( repo ) ;
this . setState ( { repoList : newRepoList } ) ;
} ;
sortItems = ( sortBy , sortOrder ) => {
cookie . save ( 'seafile-repo-dir-sort-by' , sortBy ) ;
cookie . save ( 'seafile-repo-dir-sort-order' , sortOrder ) ;
this . setState ( {
sortBy : sortBy ,
sortOrder : sortOrder ,
repoList : Utils . sortRepos ( this . state . repoList , sortBy , sortOrder )
} ) ;
} ;
toggleSortOptionsDialog = ( ) => {
this . setState ( {
isSortOptionsDialogOpen : ! this . state . isSortOptionsDialogOpen
} ) ;
} ;
renderContent = ( ) => {
const { errMessage } = this . state ;
const emptyTip = (
< EmptyTip >
< h2 > { gettext ( 'No public libraries' ) } < / h 2 >
< p > { gettext ( 'No public libraries have been created yet. A public library is accessible by all users. You can create a public library by clicking the "Add Library" button in the menu bar.' ) } < / p >
< / E m p t y T i p >
) ;
const { inAllLibs = false } = this . props ; // inAllLibs: in 'All Libs'('Files') page
return (
< >
{ this . state . isLoading && < Loading / > }
{ ( ! this . state . isLoading && errMessage ) && errMessage }
{ ( ! this . state . isLoading && this . state . repoList . length === 0 ) && emptyTip }
{ ( ! this . state . isLoading && this . state . repoList . length > 0 ) &&
< SharedRepoListView
libraryType = { this . state . libraryType }
repoList = { this . state . repoList }
sortBy = { this . state . sortBy }
sortOrder = { this . state . sortOrder }
sortItems = { this . sortItems }
onItemUnshare = { this . onItemUnshare }
onItemDelete = { this . onItemDelete }
theadHidden = { inAllLibs }
/ >
}
< / >
) ;
} ;
renderSortIconInMobile = ( ) => {
return (
< >
{ ( ! Utils . isDesktop ( ) && this . state . repoList . length > 0 ) && < span className = "sf3-font sf3-font-sort action-icon" onClick = { this . toggleSortOptionsDialog } > < / s p a n > }
< / >
) ;
} ;
render ( ) {
const { inAllLibs = false } = this . props ; // inAllLibs: in 'All Libs'('Files') page
if ( inAllLibs ) {
return (
< >
2024-05-01 21:13:56 +08:00
< div className = "d-flex justify-content-between mt-3 py-1 sf-border-bottom" >
2024-04-26 21:51:50 +08:00
< h4 className = "sf-heading m-0" >
< span className = "sf3-font-share-with-all sf3-font nav-icon" aria - hidden = "true" > < / s p a n >
{ gettext ( 'Shared with all' ) }
< / h 4 >
2024-04-19 14:51:41 +08:00
{ this . renderSortIconInMobile ( ) }
< / d i v >
{ this . renderContent ( ) }
< / >
) ;
}
return (
< Fragment >
< div className = "main-panel-north border-left-show" >
{ canAddPublicRepo && < TopToolbar onShowSidePanel = { this . props . onShowSidePanel } addRepoItem = { this . addRepoItem } / > }
< CommonToolbar onSearchedClick = { this . props . onSearchedClick } / >
< / d i v >
< div className = "main-panel-center" >
< div className = "cur-view-container" >
< div className = "cur-view-path" >
< h3 className = "sf-heading m-0" > { gettext ( 'Shared with all' ) } < / h 3 >
{ this . renderSortIconInMobile ( ) }
< / d i v >
< div className = "cur-view-content" >
{ this . renderContent ( ) }
< / d i v >
< / d i v >
< / d i v >
{ this . state . isSortOptionsDialogOpen &&
< SortOptionsDialog
toggleDialog = { this . toggleSortOptionsDialog }
sortBy = { this . state . sortBy }
sortOrder = { this . state . sortOrder }
sortItems = { this . sortItems }
/ >
}
< / F r a g m e n t >
) ;
}
}
PublicSharedView . propTypes = propTypes ;
export default PublicSharedView ;