-
Notifications
You must be signed in to change notification settings - Fork 40
feat(react-native): expose useModeration hook #2073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
52 changes: 52 additions & 0 deletions
52
packages/react-native-sdk/src/contexts/internal/BackgroundFiltersContext.tsx
This file contains hidden or 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,52 @@ | ||
| import { createContext } from 'react'; | ||
| import { type ImageSourcePropType } from 'react-native'; | ||
|
|
||
| // excluding array of images and only allow one image | ||
| export type ImageSourceType = Exclude<ImageSourcePropType, Array<any>>; | ||
|
|
||
| export type BlurIntensity = 'light' | 'medium' | 'heavy'; | ||
|
|
||
| export type CurrentBackgroundFilter = { | ||
| blur?: BlurIntensity; | ||
| image?: ImageSourceType; | ||
| }; | ||
|
|
||
| export type BackgroundFiltersAPI = { | ||
| /** | ||
| * The currently applied background filter. Undefined value indicates that no filter is applied. | ||
| */ | ||
| currentBackgroundFilter: CurrentBackgroundFilter | undefined; | ||
| /** | ||
| * Whether the current device supports the background filters. | ||
| */ | ||
| isSupported: boolean; | ||
| /** | ||
| * Applies a background image filter to the video. | ||
| * | ||
| * @param imageSource the URL of the image to use as the background. | ||
| */ | ||
| applyBackgroundImageFilter: (imageSource: ImageSourceType) => Promise<void>; | ||
| /** | ||
| * Applies a background blur filter to the video. | ||
| * | ||
| * @param blurLevel the level of blur to apply to the background. | ||
| */ | ||
| applyBackgroundBlurFilter: (blurIntensity: BlurIntensity) => Promise<void>; | ||
| /** | ||
| * Applies a video blur filter to the video. | ||
| * | ||
| * @param blurIntensity the level of blur to apply to the video. | ||
| */ | ||
| applyVideoBlurFilter: (blurIntensity: BlurIntensity) => Promise<void>; | ||
| /** | ||
| * Disables all filters applied to the video. | ||
| */ | ||
| disableAllFilters: () => void; | ||
| }; | ||
|
|
||
| /** | ||
| * The context for the background filters. | ||
| */ | ||
| export const BackgroundFiltersContext = createContext< | ||
| BackgroundFiltersAPI | undefined | ||
| >(undefined); |
This file contains hidden or 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 hidden or 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,83 @@ | ||
| import { useContext, useEffect, useRef } from 'react'; | ||
| import { useCall } from '@stream-io/video-react-bindings'; | ||
| import { BackgroundFiltersContext } from '../contexts/internal/BackgroundFiltersContext'; | ||
|
|
||
| export interface ModerationOptions { | ||
| /** | ||
| * How long the moderation effect should stay active before being disabled. | ||
| * Set to `0` to keep it active indefinitely. Defaults to 5000 ms. | ||
| */ | ||
| duration?: number; | ||
| } | ||
|
|
||
| export const useModeration = (options?: ModerationOptions) => { | ||
| const { duration = 5000 } = options || {}; | ||
| const call = useCall(); | ||
|
|
||
| // accessing the filters context directly, as it is optional, but our | ||
| // useBackgroundFilters() throws an error if used outside the provider | ||
| const filtersApi = useContext(BackgroundFiltersContext); | ||
| const { | ||
| isSupported = false, | ||
| currentBackgroundFilter, | ||
| applyBackgroundBlurFilter, | ||
| applyBackgroundImageFilter, | ||
| applyVideoBlurFilter, | ||
| disableAllFilters, | ||
| } = filtersApi || {}; | ||
| const blurTimeoutRef = useRef<ReturnType<typeof setTimeout>>(undefined); | ||
| const restoreRef = useRef<Promise<void>>(undefined); | ||
| useEffect(() => { | ||
| if (!call) return; | ||
| const unsubscribe = call.on('call.moderation_blur', () => { | ||
| const turnCameraOff = () => | ||
| call.camera.disable().catch((err) => { | ||
| console.error(`Failed to disable camera`, err); | ||
| }); | ||
|
|
||
| // not scheduling a timeout to enable the camera | ||
| clearTimeout(blurTimeoutRef.current); | ||
| if (!isSupported) return turnCameraOff(); | ||
|
|
||
| restoreRef.current = (restoreRef.current || Promise.resolve()).then(() => | ||
| applyVideoBlurFilter?.('heavy').then(() => { | ||
| if (duration <= 0) return; | ||
|
|
||
| const restore = () => { | ||
| const { blur, image } = currentBackgroundFilter || {}; | ||
| const action = blur | ||
| ? applyVideoBlurFilter?.(blur) | ||
| : image | ||
| ? applyBackgroundImageFilter?.(image) | ||
| : Promise.resolve(disableAllFilters?.()); | ||
|
|
||
| action?.catch((err) => { | ||
| console.error(`Failed to restore pre-moderation effect`, err); | ||
| }); | ||
| }; | ||
|
|
||
| blurTimeoutRef.current = setTimeout(restore, duration); | ||
| }, turnCameraOff), | ||
| ); | ||
| }); | ||
| return () => { | ||
| unsubscribe(); | ||
| }; | ||
| }, [ | ||
| applyBackgroundBlurFilter, | ||
| applyBackgroundImageFilter, | ||
| applyVideoBlurFilter, | ||
| call, | ||
| currentBackgroundFilter, | ||
| disableAllFilters, | ||
| duration, | ||
| isSupported, | ||
| ]); | ||
|
|
||
| useEffect( | ||
| () => () => { | ||
| restoreRef.current?.then(() => clearTimeout(blurTimeoutRef.current)); | ||
| }, | ||
| [], | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not to pass
Promise.resolve()inuserRef()as init value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to create junk on every re-render.
useRef(Promise.resolve())will create many dangling promises that should be garbage collected afterwards.