Skip to content

Commit

Permalink
Rename with split extension (#1760)
Browse files Browse the repository at this point in the history
* Filesplitting done

* Desktop done

* Mobile done

* Yolo files

* Fixed rename for folder

* Removing end of string

* Apply suggestions from code review

* fix lint

Co-authored-by: Tanmoy Basak Anjan <tanmoy3399@gmail.com>
Co-authored-by: Michael Yankelev <12774278+FSM1@users.noreply.github.com>
Co-authored-by: Michael Yankelev <myankelev@gmail.com>
  • Loading branch information
4 people authored Nov 25, 2021
1 parent 1335725 commit ffa9706
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useCallback, useEffect, useRef } from "react"
import React, { useCallback, useEffect, useMemo, useRef } from "react"
import { makeStyles, createStyles, useThemeSwitcher, useOnClickOutside, LongPressEvents } from "@chainsafe/common-theme"
import { t } from "@lingui/macro"
import clsx from "clsx"
import {
FormikTextInput,
IMenuItem,
MoreIcon
MoreIcon,
Typography
} from "@chainsafe/common-components"
import { CSFTheme } from "../../../../../Themes/types"
import { FileSystemItem } from "../../../../../Contexts/FilesContext"
Expand Down Expand Up @@ -64,9 +65,15 @@ const useStyles = makeStyles(({ breakpoints, constants, palette }: CSFTheme) =>
desktopRename: {
display: "flex",
flexDirection: "row",
alignItems: "center",
"& svg": {
width: 20,
height: 20
},
"& > span": {
fontSize: 16,
lineHeight: "20px",
marginLeft: constants.generalUnit / 2
}
},
dropdownIcon: {
Expand Down Expand Up @@ -152,15 +159,45 @@ const FileSystemGridItem = React.forwardRef(
const { desktop } = useThemeSwitcher()
const formRef = useRef(null)

const {
fileName,
extension
} = useMemo(() => {
if (isFolder) {
return {
fileName : name,
extension: ""
}
}
const split = name.split(".")
const extension = `.${split[split.length - 1]}`

if (split.length === 1) {
return {
fileName : name,
extension: ""
}
}

return {
fileName: name.slice(0, name.length - extension.length),
extension: split[split.length - 1]
}
}, [name, isFolder])

const formik = useFormik({
initialValues: {
name
name: fileName
},
validationSchema: nameValidator,
onSubmit: (values: { name: string }) => {
const newName = values.name.trim()
const newName = extension !== "" ? `${values.name.trim()}.${extension}` : values.name.trim()

newName && handleRename && handleRename(file.cid, newName)
if (newName !== name) {
newName && handleRename && handleRename(file.cid, newName)
} else {
stopEditing()
}
},
enableReinitialize: true
})
Expand Down Expand Up @@ -241,6 +278,13 @@ const FileSystemGridItem = React.forwardRef(
}
autoFocus={editing === cid}
/>
{
!isFolder && extension !== "" && (
<Typography component="span">
{ `.${extension}` }
</Typography>
)
}
</Form>
</FormikProvider>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ const useStyles = makeStyles(({ breakpoints, constants }: CSFTheme) => {
width: "100%",
[breakpoints.up("md")]: {
margin: 0
},
[breakpoints.down("md")]: {
margin: `${constants.generalUnit * 4.2}px 0`
}
},
modalRoot: {
Expand All @@ -64,18 +61,34 @@ const useStyles = makeStyles(({ breakpoints, constants }: CSFTheme) => {
maxWidth: `${breakpoints.width("md")}px !important`
}
},
renameModal: {
padding: constants.generalUnit * 4
},
renameHeader: {
textAlign: "center"
},
renameInputWrapper: {
display: "flex",
flexDirection: "row",
alignItems: "flex-end",
[breakpoints.down("md")]: {
margin: `${constants.generalUnit * 4.2}px 0`
},
"& > span": {
display: "block",
fontSize: 16,
lineHeight: "20px",
marginLeft: constants.generalUnit / 2,
marginBottom: (constants.generalUnit * 2.50),
transform: "translateY(50%)"
}
},
renameFooter: {
display: "flex",
flexDirection: "row",
alignItems: "center",
justifyContent: "flex-end"
},
renameModal: {
padding: constants.generalUnit * 4
},
okButton: {
marginLeft: constants.generalUnit
},
Expand Down Expand Up @@ -154,19 +167,55 @@ const FileSystemItem = ({
const { downloadMultipleFiles } = useFiles()
const { cid, name, isFolder, content_type } = file
const inSharedFolder = useMemo(() => bucket?.type === "share", [bucket])

const {
fileName,
extension
} = useMemo(() => {
if (isFolder) {
return {
fileName : name,
extension: ""
}
}
const split = name.split(".")
const extension = `.${split[split.length - 1]}`

if (split.length === 1) {
return {
fileName : name,
extension: ""
}
}

return {
fileName: name.slice(0, name.length - extension.length),
extension: split[split.length - 1]
}
}, [name, isFolder])

const formik = useFormik({
initialValues: {
name
name: fileName
},
validationSchema: nameValidator,
onSubmit: (values: { name: string }) => {
const newName = values.name.trim()
const newName = extension !== "" ? `${values.name.trim()}.${extension}` : values.name.trim()

newName && handleRename && handleRename(file.cid, newName)
if (newName !== name) {
newName && handleRename && handleRename(file.cid, newName)
} else {
stopEditing()
}
},
enableReinitialize: true
})

const stopEditing = useCallback(() => {
setEditing(undefined)
formik.resetForm()
}, [formik, setEditing])

let Icon
if (isFolder) {
Icon = FolderFilledSvg
Expand Down Expand Up @@ -482,7 +531,7 @@ const FileSystemItem = ({
}}
closePosition="none"
active={editing === cid}
onClose={() => setEditing("")}
onClose={() => stopEditing()}
>
<FormikProvider value={formik}>
<Form className={classes.renameModal}>
Expand All @@ -496,13 +545,22 @@ const FileSystemItem = ({
: <Trans>Rename file</Trans>
}
</Typography>
<FormikTextInput
label="Name"
className={classes.renameInput}
name="name"
placeholder={isFolder ? t`Please enter a folder name` : t`Please enter a file name`}
autoFocus={editing === cid}
/>
<div className={classes.renameInputWrapper}>
<FormikTextInput
label="Name"
className={classes.renameInput}
name="name"
placeholder={isFolder ? t`Please enter a folder name` : t`Please enter a file name`}
autoFocus={editing === cid}
/>
{
!isFolder && extension !== "" && (
<Typography component="span">
{ `.${extension}` }
</Typography>
)
}
</div>
<footer className={classes.renameFooter}>
<Button
onClick={() => setEditing("")}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useRef } from "react"
import React, { useCallback, useMemo, useRef } from "react"
import { makeStyles, createStyles, useThemeSwitcher, useOnClickOutside, LongPressEvents } from "@chainsafe/common-theme"
import { t } from "@lingui/macro"
import clsx from "clsx"
Expand Down Expand Up @@ -67,9 +67,15 @@ const useStyles = makeStyles(({ breakpoints, constants, palette }: CSFTheme) =>
desktopRename: {
display: "flex",
flexDirection: "row",
alignItems: "center",
"& svg": {
width: 20,
height: 20
},
"& > span": {
fontSize: 16,
lineHeight: "20px",
marginLeft: constants.generalUnit / 2
}
},
filename: {
Expand Down Expand Up @@ -141,13 +147,44 @@ const FileSystemTableItem = React.forwardRef(
const { name, cid, created_at, size } = file
const { desktop } = useThemeSwitcher()
const formRef = useRef(null)

const {
fileName,
extension
} = useMemo(() => {
if (isFolder) {
return {
fileName : name,
extension: ""
}
}
const split = name.split(".")
const extension = `.${split[split.length - 1]}`

if (split.length === 1) {
return {
fileName : name,
extension: ""
}
}

return {
fileName: name.slice(0, name.length - extension.length),
extension: split[split.length - 1]
}
}, [name, isFolder])

const formik = useFormik({
initialValues: { name },
initialValues: { name: fileName },
validationSchema: nameValidator,
onSubmit: (values: { name: string }) => {
const newName = values.name.trim()
const newName = extension !== "" ? `${values.name.trim()}.${extension}` : values.name.trim()

newName && handleRename && handleRename(file.cid, newName)
if (newName !== name) {
newName && handleRename && handleRename(file.cid, newName)
} else {
stopEditing()
}
},
enableReinitialize: true
})
Expand Down Expand Up @@ -217,6 +254,13 @@ const FileSystemTableItem = React.forwardRef(
}
autoFocus={editing === cid}
/>
{
!isFolder && extension !== "" && (
<Typography component="span">
{ `.${extension}` }
</Typography>
)
}
</Form>
</FormikProvider>
)
Expand Down

0 comments on commit ffa9706

Please sign in to comment.