Skip to content

Commit

Permalink
[desk-tool] add datetime diff component
Browse files Browse the repository at this point in the history
  • Loading branch information
vicmeow authored and rexxars committed Oct 6, 2020
1 parent 0d749e9 commit 026b64c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/@sanity/desk-tool/sanity.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
{
"implements": "part:@sanity/base/diff-resolver",
"path": "diffs/portableTextDiffResolver"
},
{
"implements": "part:@sanity/base/diff-resolver",
"path": "diffs/diffResolver"
}
]
}
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>
)
}
19 changes: 19 additions & 0 deletions packages/@sanity/desk-tool/src/diffs/datetime/helpers.ts
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)
}
1 change: 1 addition & 0 deletions packages/@sanity/desk-tool/src/diffs/datetime/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DatetimeFieldDiff'
12 changes: 12 additions & 0 deletions packages/@sanity/desk-tool/src/diffs/diffResolver.ts
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

0 comments on commit 026b64c

Please sign in to comment.