-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[desk-tool] Add features context with useDeskToolFeatures hook
- Loading branch information
1 parent
88b7c65
commit 6cf9740
Showing
13 changed files
with
148 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const BREAKPOINT_SCREEN_MEDIUM = 512 | ||
export const EMPTY_PARAMS = {} | ||
export const LOADING_PANE = Symbol('LOADING_PANE') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {createContext} from 'react' | ||
import {DeskToolFeatures} from './types' | ||
|
||
export const DeskToolFeaturesContext = createContext<DeskToolFeatures>({ | ||
reviewChanges: false, | ||
splitViews: false | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {combineLatest, Observable} from 'rxjs' | ||
import {map} from 'rxjs/operators' | ||
import {BREAKPOINT_SCREEN_MEDIUM} from '../constants' | ||
import windowWidth$ from '../utils/windowWidth' | ||
import {DeskToolFeatures} from './types' | ||
|
||
export function createDeskToolFeaturesController() { | ||
// determine if the screen is narrow | ||
const isNarrowScreen$: Observable<boolean> = windowWidth$.pipe( | ||
map((windowWidth: number) => windowWidth < BREAKPOINT_SCREEN_MEDIUM) | ||
) | ||
|
||
// determine if "reviewChanges" are available | ||
const reviewChanges$ = isNarrowScreen$.pipe(map(val => !val)) | ||
|
||
// determine if "splitViews" are available | ||
const splitViews$ = isNarrowScreen$.pipe(map(val => !val)) | ||
|
||
// combine streams of features flags | ||
const state$: Observable<DeskToolFeatures> = combineLatest([reviewChanges$, splitViews$]).pipe( | ||
map(([reviewChanges, splitViews]) => ({ | ||
reviewChanges, | ||
splitViews | ||
})) | ||
) | ||
|
||
return {state$} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {useContext} from 'react' | ||
import {DeskToolFeaturesContext} from './context' | ||
|
||
export function useDeskToolFeatures() { | ||
const features = useContext(DeskToolFeaturesContext) | ||
|
||
if (!features) { | ||
throw new Error('DeskTool: missing features in context') | ||
} | ||
|
||
return features | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './provider' | ||
export * from './hooks' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React, {useEffect, useMemo, useState} from 'react' | ||
import {DeskToolFeaturesContext} from './context' | ||
import {createDeskToolFeaturesController} from './controller' | ||
import {DeskToolFeatures} from './types' | ||
|
||
export function DeskToolFeaturesProvider({children}: {children: React.ReactNode}) { | ||
const controller = useMemo(() => createDeskToolFeaturesController(), []) | ||
const [state, setState] = useState<DeskToolFeatures>({reviewChanges: false, splitViews: false}) | ||
|
||
useEffect(() => { | ||
const sub = controller.state$.subscribe(setState) | ||
return () => sub.unsubscribe() | ||
}, [controller]) | ||
|
||
return ( | ||
<DeskToolFeaturesContext.Provider value={state}>{children}</DeskToolFeaturesContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface DeskToolFeatures { | ||
reviewChanges: boolean | ||
splitViews: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters