-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added duplicate action to doc list (#68)
- Loading branch information
1 parent
186af27
commit 8d65f8f
Showing
7 changed files
with
134 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import {Box, Button, Tooltip, Text} from '@sanity/ui' | ||
import React, {useState, useCallback} from 'react' | ||
import {filter, firstValueFrom} from 'rxjs' | ||
import {CopyIcon} from '@sanity/icons' | ||
import { | ||
useDocumentOperation, | ||
useDocumentPairPermissions, | ||
useDocumentStore, | ||
useTranslation, | ||
} from 'sanity' | ||
import {usePaneRouter} from 'sanity/structure' | ||
import {uuid} from '@sanity/uuid' | ||
import {fromString as pathFromString} from '@sanity/util/paths' | ||
|
||
import {structureLocaleNamespace} from 'sanity/structure' | ||
|
||
interface NewDocumentProps { | ||
id: string | ||
type: string | ||
} | ||
|
||
export default function DuplicateDocument(props: NewDocumentProps) { | ||
const {id, type} = props | ||
|
||
const documentStore = useDocumentStore() | ||
const {duplicate} = useDocumentOperation(id, type) | ||
const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter() | ||
const [isDuplicating, setDuplicating] = useState(false) | ||
const [permissions, isPermissionsLoading] = useDocumentPairPermissions({ | ||
id, | ||
type, | ||
permission: 'duplicate', | ||
}) | ||
|
||
const {t} = useTranslation(structureLocaleNamespace) | ||
|
||
const handle = useCallback( | ||
async (event: React.MouseEvent<HTMLElement>) => { | ||
event.stopPropagation() | ||
const dupeId = uuid() | ||
|
||
setDuplicating(true) | ||
|
||
// set up the listener before executing | ||
const duplicateSuccess = firstValueFrom( | ||
documentStore.pair | ||
.operationEvents(id, type) | ||
.pipe(filter((e) => e.op === 'duplicate' && e.type === 'success')) | ||
) | ||
duplicate.execute(dupeId) | ||
|
||
// only navigate to the duplicated document when the operation is successful | ||
await duplicateSuccess | ||
setDuplicating(false) | ||
|
||
const childParams = routerPanesState[groupIndex + 1]?.[0].params || {} | ||
const {parentRefPath} = childParams | ||
|
||
handleEditReference({ | ||
id: dupeId, | ||
type, | ||
parentRefPath: parentRefPath ? pathFromString(parentRefPath) : [``], | ||
template: {id: dupeId}, | ||
}) | ||
}, | ||
[documentStore.pair, duplicate, groupIndex, handleEditReference, id, routerPanesState, type] | ||
) | ||
|
||
if (isPermissionsLoading || !permissions?.granted) { | ||
return <></> | ||
} | ||
|
||
return ( | ||
<Tooltip | ||
content={ | ||
<Box> | ||
<Text muted size={1}> | ||
{t('action.duplicate.label')} | ||
</Text> | ||
</Box> | ||
} | ||
placement="left" | ||
portal | ||
> | ||
<Button | ||
onClick={handle} | ||
padding={2} | ||
fontSize={1} | ||
as={Box} | ||
icon={<CopyIcon />} | ||
mode="ghost" | ||
tone="default" | ||
aria-label={t('action.duplicate.label')} | ||
style={{cursor: 'pointer'}} | ||
disabled={isDuplicating || Boolean(duplicate.disabled) || isPermissionsLoading} | ||
/> | ||
</Tooltip> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters