2021-06-23 05:37:08 -04:00
|
|
|
import { useEffect, useState, useRef } from 'react'
|
2021-04-27 05:06:44 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-03-10 07:19:54 -05:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
2024-09-04 03:52:08 -04:00
|
|
|
import OLFormControl from '@/features/ui/components/ol/ol-form-control'
|
|
|
|
import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
|
|
|
|
import MaterialIcon from '@/shared/components/material-icon'
|
|
|
|
import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
|
2021-03-10 07:19:54 -05:00
|
|
|
|
2022-05-18 09:46:10 -04:00
|
|
|
type ProjectNameEditableLabelProps = {
|
|
|
|
projectName: string
|
|
|
|
onChange: (value: string) => void
|
|
|
|
hasRenamePermissions?: boolean
|
|
|
|
className?: string
|
|
|
|
}
|
|
|
|
|
2021-03-10 07:19:54 -05:00
|
|
|
function ProjectNameEditableLabel({
|
|
|
|
projectName,
|
2021-06-25 04:14:07 -04:00
|
|
|
hasRenamePermissions,
|
2021-03-10 07:19:54 -05:00
|
|
|
onChange,
|
2021-04-27 03:52:58 -04:00
|
|
|
className,
|
2022-05-18 09:46:10 -04:00
|
|
|
}: ProjectNameEditableLabelProps) {
|
2021-04-27 05:06:44 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2021-03-10 07:19:54 -05:00
|
|
|
const [isRenaming, setIsRenaming] = useState(false)
|
|
|
|
|
2021-06-25 04:14:07 -04:00
|
|
|
const canRename = hasRenamePermissions && !isRenaming
|
2021-03-10 07:19:54 -05:00
|
|
|
|
|
|
|
const [inputContent, setInputContent] = useState(projectName)
|
|
|
|
|
2022-05-18 09:46:10 -04:00
|
|
|
const inputRef = useRef<HTMLInputElement | null>(null)
|
2021-03-10 07:19:54 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isRenaming) {
|
2022-05-18 09:46:10 -04:00
|
|
|
inputRef.current?.select()
|
2021-03-10 07:19:54 -05:00
|
|
|
}
|
|
|
|
}, [isRenaming])
|
|
|
|
|
|
|
|
function startRenaming() {
|
2021-06-25 04:14:07 -04:00
|
|
|
if (canRename) {
|
|
|
|
setInputContent(projectName)
|
|
|
|
setIsRenaming(true)
|
|
|
|
}
|
2021-03-10 07:19:54 -05:00
|
|
|
}
|
|
|
|
|
2021-08-11 04:50:01 -04:00
|
|
|
function finishRenaming() {
|
|
|
|
setIsRenaming(false)
|
|
|
|
onChange(inputContent)
|
|
|
|
}
|
|
|
|
|
2022-05-18 09:46:10 -04:00
|
|
|
function handleKeyDown(event: React.KeyboardEvent) {
|
2021-03-10 07:19:54 -05:00
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault()
|
2021-08-11 04:50:01 -04:00
|
|
|
finishRenaming()
|
2021-03-10 07:19:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 09:46:10 -04:00
|
|
|
function handleOnChange(event: React.ChangeEvent<HTMLInputElement>) {
|
2021-03-10 07:19:54 -05:00
|
|
|
setInputContent(event.target.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleBlur() {
|
2021-08-11 04:50:01 -04:00
|
|
|
if (isRenaming) {
|
|
|
|
finishRenaming()
|
|
|
|
}
|
2021-03-10 07:19:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames('project-name', className)}>
|
|
|
|
{!isRenaming && (
|
|
|
|
<span className="name" onDoubleClick={startRenaming}>
|
|
|
|
{projectName}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{isRenaming && (
|
2024-09-04 03:52:08 -04:00
|
|
|
<OLFormControl
|
2021-03-10 07:19:54 -05:00
|
|
|
ref={inputRef}
|
|
|
|
type="text"
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
onChange={handleOnChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={inputContent}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{canRename && (
|
2024-09-04 03:52:08 -04:00
|
|
|
<OLTooltip
|
2022-05-18 09:46:10 -04:00
|
|
|
id="online-user"
|
|
|
|
description={t('rename')}
|
|
|
|
overlayProps={{ placement: 'bottom', trigger: ['hover', 'focus'] }}
|
2021-04-27 05:06:44 -04:00
|
|
|
>
|
|
|
|
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid, jsx-a11y/click-events-have-key-events, jsx-a11y/interactive-supports-focus */}
|
|
|
|
<a className="rename" role="button" onClick={startRenaming}>
|
2024-09-04 03:52:08 -04:00
|
|
|
<BootstrapVersionSwitcher
|
|
|
|
bs3={<Icon type="pencil" fw />}
|
|
|
|
bs5={<MaterialIcon type="edit" className="align-text-bottom" />}
|
|
|
|
/>
|
2021-04-27 05:06:44 -04:00
|
|
|
</a>
|
2024-09-04 03:52:08 -04:00
|
|
|
</OLTooltip>
|
2021-03-10 07:19:54 -05:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ProjectNameEditableLabel
|