-
Notifications
You must be signed in to change notification settings - Fork 435
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
fix(structure): live edit on draft documents #7526
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c289c35
!TEMP(dev): add liveEdit to simple block
RitaDias 3030ff0
refactor(sanity): make livedit drafts readOnly
RitaDias 620294f
refactor(sanity): add banner explaining what needs to be done to get …
RitaDias 46ab3ac
refactor(sanity): add publish button to banner
RitaDias f88b279
feat(structure): allow discard draft in banner
RitaDias d7c0212
test(e2e): add e2e test to check banner exists
RitaDias 3b68609
refactor(e2e): add telemetry
RitaDias f53c472
refactor(structure): change names of telemetry events
RitaDias 37ad4f7
refactor(structure): update banner text
RitaDias 3b777cd
refactor(structure): send in useDocumentPane info as props vs fetchin…
RitaDias d06029a
refactor(structure): update telemetry names and event properties
RitaDias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
90 changes: 90 additions & 0 deletions
90
packages/sanity/src/structure/panes/document/documentPanel/banners/DraftLiveEditBanner.tsx
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,90 @@ | ||
import {type SanityDocument} from '@sanity/client' | ||
import {ErrorOutlineIcon} from '@sanity/icons' | ||
import {useTelemetry} from '@sanity/telemetry/react' | ||
import {Flex, Text} from '@sanity/ui' | ||
import {useCallback, useEffect, useState} from 'react' | ||
import { | ||
isDraftId, | ||
type ObjectSchemaType, | ||
Translate, | ||
useDocumentOperation, | ||
useTranslation, | ||
} from 'sanity' | ||
|
||
import {Button} from '../../../../../ui-components' | ||
import {structureLocaleNamespace} from '../../../../i18n' | ||
import {ResolvedLiveEdit} from './__telemetry__/DraftLiveEditBanner.telemetry' | ||
import {Banner} from './Banner' | ||
|
||
interface DraftLiveEditBannerProps { | ||
displayed: Partial<SanityDocument> | null | ||
documentId: string | ||
schemaType: ObjectSchemaType | ||
} | ||
|
||
export function DraftLiveEditBanner({ | ||
displayed, | ||
documentId, | ||
schemaType, | ||
}: DraftLiveEditBannerProps): JSX.Element | null { | ||
const {t} = useTranslation(structureLocaleNamespace) | ||
const [isPublishing, setPublishing] = useState(false) | ||
const [isDiscarding, setDiscarding] = useState(false) | ||
const telemetry = useTelemetry() | ||
|
||
const {publish, discardChanges} = useDocumentOperation(documentId, displayed?._type || '') | ||
|
||
const handlePublish = useCallback(() => { | ||
publish.execute() | ||
setPublishing(true) | ||
telemetry.log(ResolvedLiveEdit, {liveEditResolveType: 'publish'}) | ||
}, [publish, telemetry]) | ||
|
||
const handleDiscard = useCallback(() => { | ||
discardChanges.execute() | ||
setDiscarding(true) | ||
telemetry.log(ResolvedLiveEdit, {liveEditResolveType: 'discard'}) | ||
}, [discardChanges, telemetry]) | ||
|
||
useEffect(() => { | ||
return () => { | ||
setPublishing(false) | ||
setDiscarding(false) | ||
} | ||
}) | ||
|
||
if (displayed && displayed._id && !isDraftId(displayed._id)) { | ||
return null | ||
} | ||
|
||
return ( | ||
<Banner | ||
content={ | ||
<Flex align="center" justify="space-between" gap={1}> | ||
<Text size={1} weight="medium"> | ||
<Translate | ||
t={t} | ||
i18nKey={'banners.live-edit-draft-banner.text'} | ||
values={{schemaType: schemaType.title}} | ||
/> | ||
</Text> | ||
<Button | ||
onClick={handlePublish} | ||
text={t('action.publish.live-edit.label')} | ||
tooltipProps={{content: t('banners.live-edit-draft-banner.publish.tooltip')}} | ||
loading={isPublishing} | ||
/> | ||
|
||
<Button | ||
onClick={handleDiscard} | ||
text={t('banners.live-edit-draft-banner.discard.tooltip')} | ||
tooltipProps={{content: t('banners.live-edit-draft-banner.discard.tooltip')}} | ||
loading={isDiscarding} | ||
/> | ||
</Flex> | ||
} | ||
data-testid="live-edit-type-banner" | ||
icon={ErrorOutlineIcon} | ||
/> | ||
) | ||
} |
15 changes: 15 additions & 0 deletions
15
...cture/panes/document/documentPanel/banners/__telemetry__/DraftLiveEditBanner.telemetry.ts
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,15 @@ | ||
import {defineEvent} from '@sanity/telemetry' | ||
|
||
interface TypeInfo { | ||
liveEditResolveType: 'publish' | 'discard' | ||
} | ||
|
||
/** | ||
* When a draft in a live edit document is published | ||
* @internal | ||
*/ | ||
export const ResolvedLiveEdit = defineEvent<TypeInfo>({ | ||
name: 'Resolved LiveEdit Draft', | ||
version: 1, | ||
description: 'User resolved a draft of a live edit document to continue editing', | ||
}) |
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,31 @@ | ||
/* eslint-disable max-nested-callbacks */ | ||
import {expect, test} from '@playwright/test' | ||
|
||
import {createUniqueDocument, withDefaultClient} from '../../helpers' | ||
|
||
withDefaultClient((context) => { | ||
test.describe('sanity/structure: document pane', () => { | ||
test('on live edit document with a draft, a banner should appear', async ({page}) => { | ||
// create published document | ||
const uniqueDoc = await createUniqueDocument(context.client, {_type: 'playlist'}) | ||
const id = uniqueDoc._id! | ||
|
||
// create draft document | ||
await createUniqueDocument(context.client, { | ||
_type: 'playlist', | ||
_id: `drafts.${id}`, | ||
name: 'Edited by e2e test runner', | ||
}) | ||
|
||
await page.goto(`/test/content/playlist;${id}`) | ||
|
||
await expect(page.getByTestId('document-panel-scroller')).toBeAttached() | ||
await expect(page.getByTestId('string-input')).toBeAttached() | ||
|
||
// checks that inputs are set to read only | ||
await expect(await page.getByTestId('string-input')).toHaveAttribute('readonly', '') | ||
// checks that the banner is visible | ||
await expect(page.getByTestId('live-edit-type-banner')).toBeVisible() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
temp (commit will be removed before merging)