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

Re-use create file functionality for "Add README" button #3173

Merged
merged 5 commits into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 10 additions & 58 deletions catalog/app/containers/Bucket/Summary.js
@@ -1,10 +1,11 @@
import { basename, join } from 'path'
import { basename } from 'path'

import * as R from 'ramda'
import * as React from 'react'
import { Link, useHistory } from 'react-router-dom'
import { Link } from 'react-router-dom'
import * as M from '@material-ui/core'

import * as FileEditor from 'components/FileEditor'
import * as Pagination from 'components/Pagination'
import * as Preview from 'components/Preview'
import Thumbnail, { SUPPORTED_EXTENSIONS } from 'components/Thumbnail'
Expand All @@ -26,68 +27,19 @@ const useAddReadmeSectionStyles = M.makeStyles((t) => ({
},
}))

const variants = ['README.md', 'README.txt', 'README']

function AddReadmeSection({ packageHandle: { bucket, name } }) {
function AddReadmeSection({ packageHandle, path }) {
const prompt = FileEditor.useCreateFileInPackage(packageHandle, path)
const classes = useAddReadmeSectionStyles()
const { urls } = NamedRoutes.use()
const history = useHistory()
const toConfig = React.useCallback(
(index) => {
const next = urls.bucketPackageDetail(bucket, name, { action: 'revisePackage' })
const key = join(name, variants[index])
return urls.bucketFile(bucket, key, {
add: key,
edit: true,
next,
})
},
[bucket, name, urls],
)
const [selected, setSelected] = React.useState(0)
const handleClick = React.useCallback(() => {
const url = toConfig(selected)
history.push(url)
}, [history, toConfig, selected])
const options = React.useMemo(() => variants.map((x) => `Add ${x}`), [])
return (
<div className={classes.root}>
<SplitButton options={options} onClick={handleClick} onChange={setSelected}>
{options[selected]}
</SplitButton>
{prompt.render()}
<M.Button color="primary" size="small" variant="contained" onClick={prompt.open}>
Add README
</M.Button>
</div>
)
}

function SplitButton({ onClick, children, onChange, options }) {
const anchorRef = React.useRef(null)
const [anchorEl, setAnchorEl] = React.useState(null)
const handleSelect = React.useCallback(
(index) => () => {
onChange(index)
setAnchorEl(null)
},
[onChange],
)
return (
<>
<M.ButtonGroup ref={anchorRef} color="primary" size="small" variant="contained">
<M.Button onClick={onClick}>{children}</M.Button>
<M.Button onClick={() => setAnchorEl(anchorRef.current)}>
<M.Icon>arrow_drop_down</M.Icon>
</M.Button>
</M.ButtonGroup>
<M.Menu open={!!anchorEl} anchorEl={anchorEl} onClose={() => setAnchorEl(null)}>
{options.map((option, i) => (
<M.MenuItem key={`${option}_${i}`} onClick={handleSelect(i)}>
{option}
</M.MenuItem>
))}
</M.Menu>
</>
)
}

const README_RE = /^readme\.md$/i
const SUMMARIZE_RE = /^quilt_summarize\.json$/i

Expand Down Expand Up @@ -267,7 +219,7 @@ export default function BucketSummary({ files, mkUrl: mkUrlProp, packageHandle,
!path &&
!!packageHandle &&
!!preferences?.ui?.actions?.revisePackage && (
<AddReadmeSection packageHandle={packageHandle} />
<AddReadmeSection packageHandle={packageHandle} path={path} />
)}
{!!images.length && <Thumbnails {...{ images, mkUrl }} />}
{summarize && (
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Expand Up @@ -165,6 +165,7 @@
* [Changed] Wrap wide Vega charts with horizontal scroll ([#2304](https://github.com/quiltdata/quilt/pull/2304))
* [Changed] Unify package creation and update dialogs (support adding S3 files in both) ([#2302](https://github.com/quiltdata/quilt/pull/2302))
* [Changed] Warmer chart colors ([#2329](https://github.com/quiltdata/quilt/pull/2329), [#2338](https://github.com/quiltdata/quilt/pull/2338))
* [Changed] Remove custom button for adding Readme, and re-use dialog for creating files. Fix creating README in package ([#3173](https://github.com/quiltdata/quilt/pull/3173))
* [Fixed] `UnicodeDecodeError` in indexer and pkgselect lambdas ([#2123](https://github.com/quiltdata/quilt/pull/2123))
* [Fixed] Catch and display package-related errors properly ("no such package" and "bad revision") ([#2160](https://github.com/quiltdata/quilt/pull/2160))
* [Fixed] Crashing `pkgselect` lambda's folder view on an empty manifest ([#2147](https://github.com/quiltdata/quilt/pull/2147))
Expand Down