Skip to content

Commit

Permalink
[desk-tool] add size % diff to files
Browse files Browse the repository at this point in the history
  • Loading branch information
vicmeow authored and rexxars committed Oct 6, 2020
1 parent 8c3b6bf commit 94f1e2a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
23 changes: 20 additions & 3 deletions packages/@sanity/desk-tool/src/diffs/file/FileFieldDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ import {getRefValue} from '../hooks'
import styles from './FileFieldDiff.css'
import FilePreview from './FilePreview'

function getSizeDiff(prev, next) {
if (!prev || !next) {
return 0
}
const increase = next - prev
const pct = Math.round((increase / prev) * 100)
return pct
}

export const FileFieldDiff: DiffComponent<ObjectDiff> = ({diff, schemaType}) => {
const {fromValue, toValue, fields} = diff
const fromAsset = fromValue?.asset
const toAsset = toValue?.asset
const prev = getRefValue(fromAsset?._ref)
const next = getRefValue(toAsset?._ref)
const prev: any = getRefValue(fromAsset?._ref)
const next: any = getRefValue(toAsset?._ref)

// Size in MB TODO: improve
const prevSize = prev?.size && Math.round(prev.size / 1024)
const nextSize = next?.size && Math.round(next.size / 1024)

const changedFields = Object.keys(fields)
.map(field => ({
Expand Down Expand Up @@ -45,7 +58,11 @@ export const FileFieldDiff: DiffComponent<ObjectDiff> = ({diff, schemaType}) =>
)}
{next && (
<DiffAnnotation diff={diff} path="asset._ref">
<FilePreview value={next} action={didAssetChange ? 'added' : 'changed'} />
<FilePreview
value={next}
action={didAssetChange ? 'added' : 'changed'}
pctDiff={getSizeDiff(prevSize, nextSize)}
/>
</DiffAnnotation>
)}
</div>
Expand Down
16 changes: 16 additions & 0 deletions packages/@sanity/desk-tool/src/diffs/file/FilePreview.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@
@nest & .title {
text-decoration: line-through;
}
}

.sizeDiff {
font-size: 13px;
padding: 1px 4px;
background: var(--component-bg);
border-radius: 2px;
margin: 0 var(--small-padding);

@nest &[data-number="positive"] {
color: var(--state-success-color);
}

@nest &[data-number="negative"] {
color: var(--state-danger-color);
}
}
10 changes: 8 additions & 2 deletions packages/@sanity/desk-tool/src/diffs/file/FilePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import styles from './FilePreview.css'
interface Props {
value: any // TODO
action: 'changed' | 'added' | 'removed'
pctDiff?: number
}

export default function FilePreview({value, action = 'changed'}: Props) {
export default function FilePreview({value, action = 'changed', pctDiff}: Props) {
const title = value.originalFilename || 'Untitled'
return (
<div className={styles.root}>
Expand All @@ -21,7 +22,12 @@ export default function FilePreview({value, action = 'changed'}: Props) {
</h3>
<div>
<span>{action}</span>
{action === 'changed' && <span>{value.size}</span>}
{pctDiff && pctDiff !== 0 && (
<span className={styles.sizeDiff} data-number={pctDiff > 0 ? 'positive' : 'negative'}>
{pctDiff > 0 && '+'}
{pctDiff}%
</span>
)}
</div>
</div>
</div>
Expand Down

0 comments on commit 94f1e2a

Please sign in to comment.