-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[field] Use built-in preview abstraction for reference diff
- Loading branch information
Showing
7 changed files
with
76 additions
and
56 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
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
53 changes: 10 additions & 43 deletions
53
packages/@sanity/field/src/types/reference/diff/ReferenceFieldDiff.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 |
---|---|---|
@@ -1,49 +1,16 @@ | ||
import React from 'react' | ||
import LinkIcon from 'part:@sanity/base/link-icon' | ||
import { | ||
DiffComponent, | ||
ReferenceDiff, | ||
DiffAnnotationTooltip, | ||
DiffAnnotationCard | ||
} from '../../../diff' | ||
import styles from './ReferenceFieldDiff.css' | ||
import {useRefPreview} from '../../../diff/hooks' | ||
import {ChangeLayout, MetaInfo} from '../../../diff/components' | ||
|
||
// TODO: fix hooks error | ||
import {DiffComponent, ReferenceDiff} from '../../../diff' | ||
import {Change} from '../../../diff/components' | ||
import {ReferencePreview} from '../preview/ReferencePreview' | ||
|
||
export const ReferenceFieldDiff: DiffComponent<ReferenceDiff> = ({diff, schemaType}) => { | ||
const {fromValue, toValue} = diff | ||
const prev = fromValue && useRefPreview(fromValue, schemaType) | ||
const next = toValue && useRefPreview(toValue, schemaType) | ||
|
||
return ( | ||
<DiffAnnotationTooltip as="div" diff={diff} path="_ref"> | ||
<ChangeLayout | ||
layout={prev && next ? 'grid' : 'inline'} | ||
from={ | ||
prev && ( | ||
<DiffAnnotationCard className={styles.annotation} diff={diff} path="_ref"> | ||
<MetaInfo | ||
title={prev.title || 'Untitled'} | ||
action={prev && next ? 'changed' : 'removed'} | ||
icon={LinkIcon} | ||
/> | ||
</DiffAnnotationCard> | ||
) | ||
} | ||
to={ | ||
next && ( | ||
<DiffAnnotationCard className={styles.annotation} diff={diff} path="_ref"> | ||
<MetaInfo | ||
title={next.title || 'Untitled'} | ||
action={prev && next ? 'changed' : 'added'} | ||
icon={LinkIcon} | ||
/> | ||
</DiffAnnotationCard> | ||
) | ||
} | ||
/> | ||
</DiffAnnotationTooltip> | ||
<Change | ||
previewComponent={ReferencePreview} | ||
layout="grid" | ||
path="_ref" | ||
diff={diff} | ||
schemaType={schemaType} | ||
/> | ||
) | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/@sanity/field/src/types/reference/preview/ReferencePreview.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,9 @@ | ||
import React from 'react' | ||
import Preview from 'part:@sanity/base/preview' | ||
import {getReferencedType} from '../../../schema/helpers' | ||
import {PreviewComponent} from '../../../preview/types' | ||
import {Reference} from '../../../diff' | ||
|
||
export const ReferencePreview: PreviewComponent<Reference> = ({value, schemaType}) => ( | ||
<Preview type={getReferencedType(schemaType)} value={value} layout="default" /> | ||
) |