-
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.
[desk-tool] add datetime diff component
- Loading branch information
Showing
5 changed files
with
76 additions
and
0 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
40 changes: 40 additions & 0 deletions
40
packages/@sanity/desk-tool/src/diffs/datetime/DatetimeFieldDiff.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,40 @@ | ||
import React from 'react' | ||
import { | ||
DiffComponent, | ||
// DatetimeDiff, | ||
DiffAnnotationTooltip, | ||
useDiffAnnotationColor | ||
} from '@sanity/field/diff' | ||
import styles from '../shared/BlockSegmentStyles.css' | ||
import {getDateFormat} from './helpers' | ||
import {DiffLayout} from '../shared' | ||
|
||
// TODO: fix DatetimeDiff | ||
|
||
export const DatetimeFieldDiff /* : DiffComponent<DatetimeDiff> */ = ({diff, schemaType}) => { | ||
const {fromValue, toValue} = diff | ||
const color = useDiffAnnotationColor(diff, []) | ||
const style = color ? {background: color.background, color: color.text} : {} | ||
const fromDate = getDateFormat(fromValue, schemaType.name, schemaType.options) | ||
const toDate = getDateFormat(toValue, schemaType.name, schemaType.options) | ||
return ( | ||
<DiffAnnotationTooltip className={styles.root} diff={diff}> | ||
<DiffLayout | ||
renderFrom={ | ||
fromValue !== undefined && ( | ||
<del className={`${styles.segment} ${styles.add}`} style={style}> | ||
{fromDate} | ||
</del> | ||
) | ||
} | ||
renderTo={ | ||
toValue && ( | ||
<ins className={`${styles.segment} ${styles.remove}`} style={style}> | ||
{toDate} | ||
</ins> | ||
) | ||
} | ||
/> | ||
</DiffAnnotationTooltip> | ||
) | ||
} |
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,19 @@ | ||
import formatDate from 'date-fns/format' | ||
|
||
export function getDateFormat(value, type = '', options){ | ||
let format = 'YYYY-MM-DD' | ||
if (options && options.dateFormat) { | ||
format = options.dateFormat | ||
} | ||
if (type === 'date') { | ||
// If the type is date only | ||
return formatDate(value, format) | ||
} | ||
// If the type is datetime | ||
if (options && options.timeFormat) { | ||
format += ` ${options.timeFormat}` | ||
} else { | ||
format += ' HH:mm' | ||
} | ||
return formatDate(value, format) | ||
} |
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 @@ | ||
export * from './DatetimeFieldDiff' |
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,12 @@ | ||
import {DiffComponentResolver} from '@sanity/field/diff' | ||
import {DatetimeFieldDiff} from './datetime' | ||
|
||
const diffResolver: DiffComponentResolver = function diffResolver({schemaType}) { | ||
// datetime or date | ||
if (['datetime', 'date'].includes(schemaType.name)) { | ||
return DatetimeFieldDiff | ||
} | ||
return undefined | ||
} | ||
|
||
export default diffResolver |