Skip to content

Commit

Permalink
[base] Restructure user-color files
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard authored and rexxars committed Oct 6, 2020
1 parent 5617f1b commit f7d989b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/@sanity/base/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {useDocumentPresence, useGlobalPresence} from './datastores/presence/hooks'
export {useUser} from './datastores/user/hooks'
export {useUserColor} from './user-color/useUserColor'
export {useUserColor} from './user-color/hooks'
export {useTimeAgo} from './time/useTimeAgo'
5 changes: 5 additions & 0 deletions packages/@sanity/base/src/user-color/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {createContext} from 'react'
import {userColorManager} from './singleton'
import {UserColorManager} from './types'

export const UserColorManagerContext = createContext<UserColorManager>(userColorManager)
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {useContext} from 'react'
import {of} from 'rxjs'
import {useObservable} from '../util/useObservable'
import {useUserColorManager} from './provider'
import {UserColor} from './types'
import {UserColorManagerContext} from './context'
import {UserColor, UserColorManager} from './types'

export function useUserColorManager(): UserColorManager {
return useContext(UserColorManagerContext)
}

export function useUserColor(userId: string | null): UserColor | null {
const manager = useUserColorManager()
Expand Down
8 changes: 4 additions & 4 deletions packages/@sanity/base/src/user-color/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {createUserColorManager} from './manager'
export {useUserColorManager, UserColorManagerProvider} from './provider'
export {userColorManager} from './singleton'
export {useUserColor} from './useUserColor'
export * from './hooks'
export * from './manager'
export * from './provider'
export * from './singleton'
export * from './types'
10 changes: 8 additions & 2 deletions packages/@sanity/base/src/user-color/manager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {Observable} from 'rxjs'
import {filter, shareReplay} from 'rxjs/operators'
import {color as SanityColor, ColorHueKey, COLOR_HUES} from '@sanity/color'
import {UserColorHue, ManagerOptions, UserColorManager, UserColor} from './types'
import {UserColorHue, UserColorManager, UserColor} from './types'

type UserId = string

export interface UserColorManagerOptions {
userStore?: {currentUser: Observable<{type: 'snapshot' | 'error'; user?: {id: string} | null}>}
colors?: Readonly<Record<UserColorHue, UserColor>>
currentUserColor?: UserColorHue
}

const defaultCurrentUserHue: ColorHueKey = 'purple'

// Remove green and red because they can be confused with "add" and "remove"
Expand All @@ -21,7 +27,7 @@ const defaultColors = defaultHues.reduce((colors, hue) => {
return colors
}, {} as Record<ColorHueKey, UserColor>)

export function createUserColorManager(options?: ManagerOptions): UserColorManager {
export function createUserColorManager(options?: UserColorManagerOptions): UserColorManager {
const colors = (options && options.colors) || defaultColors
const currentUserColor = (options && options.currentUserColor) || defaultCurrentUserHue
if (!colors.hasOwnProperty(currentUserColor)) {
Expand Down
17 changes: 7 additions & 10 deletions packages/@sanity/base/src/user-color/provider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import React, {createContext, useContext} from 'react'
import React from 'react'
import {UserColorManagerContext} from './context'
import {UserColorManager} from './types'
import {userColorManager} from './singleton'

interface ProviderProps {
interface UserColorManagerProviderProps {
children: React.ReactNode
manager: UserColorManager
}

const UserColorManagerContext = createContext<UserColorManager>(userColorManager)

export function useUserColorManager(): UserColorManager {
return useContext(UserColorManagerContext)
}

export function UserColorManagerProvider({children, manager}: ProviderProps) {
export function UserColorManagerProvider({
children,
manager
}: UserColorManagerProviderProps): React.ReactElement {
return React.createElement(UserColorManagerContext.Provider, {value: manager}, children)
}
6 changes: 0 additions & 6 deletions packages/@sanity/base/src/user-color/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,3 @@ export interface UserColorManager {
get: (userId: string) => UserColor
listen: (userId: string) => Observable<UserColor>
}

export interface ManagerOptions {
userStore?: {currentUser: Observable<{type: 'snapshot' | 'error'; user?: {id: string} | null}>}
colors?: Readonly<Record<UserColorHue, UserColor>>
currentUserColor?: UserColorHue
}

0 comments on commit f7d989b

Please sign in to comment.