Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename with split extension #1760

Merged
merged 17 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,38 @@ 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]}`

return {
fileName : name.replace(extension, ""),
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 = `${values.name.trim()}.${extension}`

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 +271,13 @@ const FileSystemGridItem = React.forwardRef(
}
autoFocus={editing === cid}
/>
{
!isFolder && (
<Typography component="span">
{ `.${extension}` }
</Typography>
)
}
</Form>
</FormikProvider>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ const useStyles = makeStyles(({ breakpoints, constants }: CSFTheme) => {
[breakpoints.up("md")]: {
margin: 0
},
[breakpoints.down("md")]: {
margin: `${constants.generalUnit * 4.2}px 0`
}

},
modalRoot: {
[breakpoints.down("md")]: {
Expand All @@ -64,18 +62,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 +168,48 @@ 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]}`

return {
fileName : name.replace(extension, ""),
RyRy79261 marked this conversation as resolved.
Show resolved Hide resolved
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 = `${values.name.trim()}.${extension}`

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 +525,7 @@ const FileSystemItem = ({
}}
closePosition="none"
active={editing === cid}
onClose={() => setEditing("")}
onClose={() => stopEditing()}
>
<FormikProvider value={formik}>
<Form className={classes.renameModal}>
Expand All @@ -496,13 +539,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 && (
<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,37 @@ 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]}`

return {
fileName : name.replace(extension, ""),
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 = `${values.name.trim()}.${extension}`

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 +247,13 @@ const FileSystemTableItem = React.forwardRef(
}
autoFocus={editing === cid}
/>
{
!isFolder && (
<Typography component="span">
{ `.${extension}` }
</Typography>
)
}
</Form>
</FormikProvider>
)
Expand Down