import React, { ChangeEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Button, ButtonVariation, Color, Container, FlexExpander, Icon, Layout, Text, TextInput } from '@harness/uicore'
import { Link, useHistory } from 'react-router-dom'
import ReactJoin from 'react-join'
import cx from 'classnames'
import { SourceCodeEditor } from 'components/SourceCodeEditor/SourceCodeEditor'
import type { RepoFileContent } from 'services/code'
import { useAppContext } from 'AppContext'
import { GitInfoProps, isDir } from 'utils/GitUtils'
import { useStrings } from 'framework/strings'
import { filenameToLanguage, FILE_SEPERATOR } from 'utils/Utils'
import { CommitModalButton } from 'components/CommitModalButton/CommitModalButton'
import css from './FileEditor.module.scss'
const PathSeparator = () => /
function Editor({
resourceContent,
repoMetadata,
gitRef,
resourcePath
}: Pick) {
const history = useHistory()
const inputRef = useRef()
const isNew = useMemo(() => isDir(resourceContent), [resourceContent])
const [fileName, setFileName] = useState(isNew ? '' : (resourceContent.name as string))
const [parentPath, setParentPath] = useState(
isNew ? resourcePath : resourcePath.split(FILE_SEPERATOR).slice(0, -1).join(FILE_SEPERATOR)
)
const { getString } = useStrings()
const { routes } = useAppContext()
const [language, setLanguage] = useState(() => filenameToLanguage(fileName))
const [originalContent, setOriginalContent] = useState(
window.atob((resourceContent?.content as RepoFileContent)?.data || '')
)
const [content, setContent] = useState(originalContent)
const rebuildPaths = useCallback(() => {
const _tokens = fileName.split(FILE_SEPERATOR).filter(part => !!part.trim())
const _fileName = _tokens.pop() as string
const _parentPath = parentPath
.split(FILE_SEPERATOR)
.concat(_tokens)
.map(p => p.trim())
.filter(part => !!part.trim())
.join(FILE_SEPERATOR)
if (_fileName && _fileName !== fileName) {
const normalizedFilename = _fileName.trim()
const newLanguage = filenameToLanguage(normalizedFilename)
setFileName(normalizedFilename)
// This is a workaround to force Monaco update content
// with new language. Monaco still throws an error
// textModel.js:178 Uncaught Error: Model is disposed!
if (language !== newLanguage) {
setLanguage(newLanguage)
setOriginalContent(content)
}
}
setParentPath(_parentPath)
}, [fileName, setFileName, parentPath, setParentPath, language, content])
const fileResourcePath = useMemo(
() => [parentPath, fileName].filter(p => !!p.trim()).join(FILE_SEPERATOR),
[parentPath, fileName]
)
useEffect(() => {
if (inputRef.current) {
inputRef.current.size = Math.min(Math.max(fileName.length - 2, 20), 50)
}
}, [fileName, inputRef])
return (
{repoMetadata.uid}
{parentPath && (
<>
}>
{parentPath.split(FILE_SEPERATOR).map((_path, index, paths) => {
const pathAtIndex = paths.slice(0, index + 1).join('/')
return (
{_path}
)
})}
>
)}
(inputRef.current = ref)}
wrapperClassName={css.inputContainer}
placeholder={getString('nameYourFile')}
onInput={(event: ChangeEvent) => {
setFileName(event.currentTarget.value)
}}
onBlur={rebuildPaths}
onFocus={({ target }) => {
const value = (parentPath ? parentPath + FILE_SEPERATOR : '') + fileName
setFileName(value)
setParentPath('')
setTimeout(() => {
target.setSelectionRange(value.length, value.length)
target.scrollLeft = Number.MAX_SAFE_INTEGER
}, 0)
}}
/>
{getString('in')}
{gitRef}
console.log({ data })}
/>
)
}
export const FileEditor = React.memo(Editor)