Skip to content

Commit

Permalink
[field] Rename helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent e99169f commit d9013f1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import {useUserColorManager} from '@sanity/base/user-color'
import {Annotation, Diff, Path} from '../../types'
import {DiffAnnotationTooltip} from './DiffAnnotationTooltip'
import {getAnnotationForPath, getAnnotationColor} from './helpers'
import {getAnnotationAtPath, getAnnotationColor} from './helpers'

export interface AnnotationProps {
annotation: Annotation
Expand All @@ -25,7 +25,7 @@ export function DiffAnnotation(props: DiffAnnotationProps) {
const colorManager = useUserColorManager()
const {as = 'span', children, className} = props
const annotation =
'diff' in props ? getAnnotationForPath(props.diff, props.path || []) : props.annotation
'diff' in props ? getAnnotationAtPath(props.diff, props.path || []) : props.annotation

if (!annotation) {
return React.createElement(as, {className}, children)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {createElement} from 'react'
import {useAnnotationColor} from './hooks'
import {AnnotationProps, AnnotatedDiffProps} from './DiffAnnotation'
import {getAnnotationForPath} from './helpers'
import {getAnnotationAtPath} from './helpers'

interface BaseDiffAnnotationCardProps {
as?: React.ElementType | keyof JSX.IntrinsicElements
Expand All @@ -15,7 +15,7 @@ export function DiffAnnotationCard(
): React.ReactElement {
const {as = 'div', children, style = {}, ...restProps} = props
const annotation =
'diff' in props ? getAnnotationForPath(props.diff, props.path || []) : props.annotation
'diff' in props ? getAnnotationAtPath(props.diff, props.path || []) : props.annotation

const color = useAnnotationColor(annotation)
const colorStyle = color ? {background: color.background, color: color.text} : {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Tooltip} from 'part:@sanity/components/tooltip'
import React, {createElement} from 'react'
import {DiffAnnotationTooltipContent} from './DiffAnnotationTooltipContent'
import {AnnotationProps, AnnotatedDiffProps} from './DiffAnnotation'
import {getAnnotationForPath} from './helpers'
import {getAnnotationAtPath} from './helpers'

interface BaseAnnotationProps {
as?: React.ElementType | keyof JSX.IntrinsicElements
Expand All @@ -17,7 +17,7 @@ export type DiffAnnotationTooltipProps = (AnnotationProps | AnnotatedDiffProps)
export function DiffAnnotationTooltip(props: DiffAnnotationTooltipProps) {
const {className, as = 'div', children, style} = props
const annotation =
'diff' in props ? getAnnotationForPath(props.diff, props.path || []) : props.annotation
'diff' in props ? getAnnotationAtPath(props.diff, props.path || []) : props.annotation

if (!annotation) {
return createElement(as, {className, style}, children)
Expand Down
7 changes: 6 additions & 1 deletion packages/@sanity/field/src/diff/annotations/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ export function getAnnotationColor(
return annotation ? colorManager.get(annotation.author) : FALLBACK_DIFF_COLOR
}

export function getAnnotationForPath(diff: Diff, diffPath: string | Path): Annotation | undefined {
export function getAnnotationAtPath(diff: Diff, diffPath: string | Path): Annotation | undefined {
const path: Path = Array.isArray(diffPath) ? diffPath : stringToPath(diffPath)
return getAnnotationAt(diff, path)
}

export function getDiffAtPath(diff: Diff, diffPath: string | Path): Diff | undefined {
const path: Path = Array.isArray(diffPath) ? diffPath : stringToPath(diffPath)
return getDiffAt(diff, path)
}

function getAnnotationAt(diff: Diff, path: Path): Annotation | undefined {
const diffAt = getDiffAt(diff, path)
if (!diffAt) {
Expand Down
9 changes: 3 additions & 6 deletions packages/@sanity/field/src/diff/annotations/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {UserColor, useUserColorManager} from '@sanity/base/user-color'
import {Annotation, Diff, Path} from '../../types'
import {getAnnotationColor, getAnnotationForPath} from './helpers'
import {getAnnotationColor, getAnnotationAtPath} from './helpers'

export function useAnnotationColor(annotation?: Annotation | null): Readonly<UserColor> {
const userColorManager = useUserColorManager()
return getAnnotationColor(userColorManager, annotation)
}

export function useDiffAnnotationColor(
diff: Diff,
path: string | Path = []
): Readonly<UserColor> | undefined {
export function useDiffAnnotationColor(diff: Diff, path: string | Path = []): Readonly<UserColor> {
const userColorManager = useUserColorManager()
const annotation = getAnnotationForPath(diff, path)
const annotation = getAnnotationAtPath(diff, path)
return getAnnotationColor(userColorManager, annotation)
}

0 comments on commit d9013f1

Please sign in to comment.