2024-06-07 20:17:03 +03:00
'use client' ;
import clsx from 'clsx' ;
2024-06-26 18:59:49 +03:00
import { useCallback , useMemo , useState } from 'react' ;
2024-06-07 20:17:03 +03:00
2024-06-26 18:59:49 +03:00
import SelectLocationContext from '@/components/select/SelectLocationContext' ;
2024-06-07 20:17:03 +03:00
import SelectLocationHead from '@/components/select/SelectLocationHead' ;
import Label from '@/components/ui/Label' ;
import Modal , { ModalProps } from '@/components/ui/Modal' ;
import TextArea from '@/components/ui/TextArea' ;
import { useAuth } from '@/context/AuthContext' ;
2024-06-26 18:59:49 +03:00
import { useLibrary } from '@/context/LibraryContext' ;
2024-06-07 20:17:03 +03:00
import { LocationHead } from '@/models/library' ;
import { combineLocation , validateLocation } from '@/models/libraryAPI' ;
import { limits } from '@/utils/constants' ;
interface DlgChangeLocationProps extends Pick < ModalProps , ' hideWindow ' > {
initial : string ;
onChangeLocation : ( newLocation : string ) = > void ;
}
function DlgChangeLocation ( { hideWindow , initial , onChangeLocation } : DlgChangeLocationProps ) {
const { user } = useAuth ( ) ;
const [ head , setHead ] = useState < LocationHead > ( initial . substring ( 0 , 2 ) as LocationHead ) ;
const [ body , setBody ] = useState < string > ( initial . substring ( 3 ) ) ;
2024-06-26 18:59:49 +03:00
const { folders } = useLibrary ( ) ;
2024-06-07 20:17:03 +03:00
const location = useMemo ( ( ) = > combineLocation ( head , body ) , [ head , body ] ) ;
const isValid = useMemo ( ( ) = > initial !== location && validateLocation ( location ) , [ initial , location ] ) ;
2024-06-26 18:59:49 +03:00
const handleSelectLocation = useCallback ( ( newValue : string ) = > {
setHead ( newValue . substring ( 0 , 2 ) as LocationHead ) ;
setBody ( newValue . length > 3 ? newValue . substring ( 3 ) : '' ) ;
} , [ ] ) ;
2024-06-07 20:17:03 +03:00
return (
< Modal
overflowVisible
header = 'Изменение расположения'
submitText = 'Переместить'
submitInvalidTooltip = { ` Допустимы буквы, цифры, подчерк, пробел и "!". Сегмент пути не может начинаться и заканчиваться пробелом. Общая длина (с корнем) не должна превышать ${ limits . location_len } ` }
hideWindow = { hideWindow }
canSubmit = { isValid }
2024-08-15 23:23:10 +03:00
onSubmit = { ( ) = > onChangeLocation ( location ) }
2024-06-07 20:17:03 +03:00
className = { clsx ( 'w-[35rem]' , 'pb-3 px-6 flex gap-3' ) }
>
2024-08-21 16:49:04 +03:00
< div className = 'flex flex-col gap-2 min-w-[7rem] h-min' >
2024-06-26 18:59:49 +03:00
< Label className = 'select-none' text = 'Корень' / >
2024-06-07 20:17:03 +03:00
< SelectLocationHead
value = { head } // prettier: split-lines
onChange = { setHead }
excluded = { ! user ? . is_staff ? [ LocationHead . LIBRARY ] : [ ] }
/ >
< / div >
2024-06-26 18:59:49 +03:00
< SelectLocationContext
folderTree = { folders }
value = { location }
onChange = { handleSelectLocation }
className = 'max-h-[9.2rem]'
/ >
2024-06-07 20:17:03 +03:00
< TextArea
id = 'dlg_cst_body'
label = 'Путь'
className = 'w-[23rem]'
rows = { 3 }
value = { body }
onChange = { event = > setBody ( event . target . value ) }
/ >
< / Modal >
) ;
}
export default DlgChangeLocation ;