Skip to content

Commit

Permalink
[google-maps-input] Refactor to use @sanity/field dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent 01a36a1 commit 8c7019a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 30 deletions.
1 change: 1 addition & 0 deletions packages/@sanity/google-maps-input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@sanity/check": "1.150.1",
"@sanity/components": "1.150.3",
"@sanity/diff": "1.150.1",
"@sanity/field": "1.150.1",
"@types/googlemaps": "^3.39.11",
"lodash": "^4.17.15",
"rimraf": "^2.7.1"
Expand Down
23 changes: 15 additions & 8 deletions packages/@sanity/google-maps-input/src/diff/GeopointArrayDiff.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as React from 'react'
import {ArrayDiff} from '@sanity/diff'
import {GeopointSchemaType, Geopoint, Annotation} from '../types'
import {ArrayDiff, ObjectDiff, Diff} from '@sanity/field/diff'
import {GeopointSchemaType, Geopoint} from '../types'
import {GoogleMapsLoadProxy} from '../loader/GoogleMapsLoadProxy'
import {GoogleMap} from '../map/Map'
import {GeopointMove} from './GeopointMove'
import styles from './GeopointFieldDiff.css'

export interface DiffProps {
diff: ArrayDiff<Annotation, Geopoint>
diff: ArrayDiff<Geopoint>
schemaType: GeopointSchemaType
}

Expand Down Expand Up @@ -39,24 +39,31 @@ function GeopointDiff({api, diff}: DiffProps & {api: typeof window.google.maps})
>
{map => (
<>
{diff.items
.filter(item => item.diff.action !== 'unchanged' && item.diff.type === 'object')
.map(({toIndex, diff: pointDiff}) => (
// @todo revisit diff type with holm
{diff.items.map(({toIndex, diff: pointDiff}) => {
if (!isChangeDiff(pointDiff)) {
return null
}

return (
<GeopointMove
key={toIndex}
api={api}
map={map}
diff={pointDiff}
label={`${toIndex}`}
/>
))}
)
})}
</>
)}
</GoogleMap>
)
}

function isChangeDiff(diff: Diff): diff is ObjectDiff<Geopoint> {
return diff.action !== 'unchanged' && diff.type === 'object'
}

function getBounds(
fromValue: google.maps.LatLngLiteral[] | null | undefined,
toValue: google.maps.LatLngLiteral[] | null | undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as React from 'react'
import {ObjectDiff} from '@sanity/diff'
import {GeopointSchemaType, Geopoint, Annotation} from '../types'
import {ObjectDiff} from '@sanity/field/diff'
import {GeopointSchemaType, Geopoint} from '../types'
import {GoogleMapsLoadProxy} from '../loader/GoogleMapsLoadProxy'
import {GoogleMap} from '../map/Map'
import {GeopointMove} from './GeopointMove'
import styles from './GeopointFieldDiff.css'

export interface DiffProps {
diff: ObjectDiff<Annotation, Geopoint>
diff: ObjectDiff<Geopoint>
schemaType: GeopointSchemaType
}

Expand Down
14 changes: 7 additions & 7 deletions packages/@sanity/google-maps-input/src/diff/GeopointMove.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import {ObjectDiff} from '@sanity/diff'
import {useUserColorManager} from '@sanity/base'
import {ObjectDiff, Annotation} from '@sanity/field/diff'
//import {useUserColorManager} from '@sanity/base'
import {useUser} from '@sanity/react-hooks'
import {UserAvatar} from '@sanity/components/presence'
import {Marker} from '../map/Marker'
import {Arrow} from '../map/Arrow'
import {Geopoint, Annotation} from '../types'
import {Geopoint} from '../types'

interface Props {
api: typeof window.google.maps
map: google.maps.Map
diff: ObjectDiff<Annotation, Geopoint>
diff: ObjectDiff<Geopoint>
label?: string
}

export function GeopointMove({diff, api, map, label}: Props) {
const userColorManager = useUserColorManager()
//const userColorManager = useUserColorManager()
const {fromValue: from, toValue: to} = diff
const annotation = diff.isChanged ? diff.annotation : undefined
const fromRef = React.useRef<google.maps.Marker>()
Expand Down Expand Up @@ -61,7 +61,7 @@ export function GeopointMove({diff, api, map, label}: Props) {
from={from}
to={to}
zIndex={1}
color={annotation ? userColorManager.get(annotation.author) : undefined}
//color={annotation ? userColorManager.get(annotation.author) : undefined}
/>
)}
{to && (
Expand Down Expand Up @@ -101,5 +101,5 @@ function UserItem(props: {userId: string}) {
}

function AnnotationInfo({annotation}: {annotation: Annotation}) {
return <UserItem userId={annotation.author} />
return annotation && <UserItem userId={annotation.author} />
}
3 changes: 2 additions & 1 deletion packages/@sanity/google-maps-input/src/diff/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {GeopointSchemaType, ArraySchemaType} from '../types'
import {ArraySchemaType} from '@sanity/field/diff'
import {GeopointSchemaType} from '../types'
import {GeopointFieldDiff} from './GeopointFieldDiff'
import {GeopointArrayDiff} from './GeopointArrayDiff'

Expand Down
14 changes: 3 additions & 11 deletions packages/@sanity/google-maps-input/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {ObjectSchemaType} from '@sanity/field/diff'

export interface LatLng {
lat: number
lng: number
Expand All @@ -11,16 +13,6 @@ export interface Geopoint {
alt?: number
}

export interface GeopointSchemaType {
export interface GeopointSchemaType extends ObjectSchemaType {
name: 'geopoint'
title?: string
description?: string
}

export interface ArraySchemaType {
name: 'array'
of: {name: string}[]
}

// @todo replace
export type Annotation = {author: string}

0 comments on commit 8c7019a

Please sign in to comment.