mattermost-mobile/app/components/progress_bar/index.tsx
Elias Nahum 374f812b98
Update dependencies (#8721)
* update fastlane

* update dev dependencies

* update to eslint 9+

* update testing-library

* update react-intl

* update bottom-sheet

* update expo

* update reanimated

* upgrade msgpack

* upgrade datepicker

* upgrade react-navigation

* update sentry

* update FlasList

* update fuse.js moment-timezone node-html-parser and semver

* update gesture-handler

* update image-picker

* update react-native-keychain

* update react-native-localize

* update react-native-navigation

* update watermelonDB

* update react-native-permissions

* update react-native-safe-area-context and react-native-screens

* update react-native-share and react-native-svg

* update react-native-video and react-native-webrtc

* update @mattermost/rnutils

* update @mattermost/rnshare

* update @mattermost/hardware-keyboard

* fix isMainActivity

* update android dependencies

* fix upload file progress indicator

* fix entry update config & license

* revert to stable version of @sentry/react-native

* update react-intl again

* update moment-timezone

* upgrade @react-native-camera-roll/camera-roll

* update @react-native-clipboard/clipboard

* update @react-navigation again

* update @shopify/flash-list

* update eslint

* update expo again

* update html-entities

* update mime-db

* update react-native-permissions

* Revert "update react-intl again"

This reverts commit e8e6d5a60dfa56b82b810cbbd7cdffec7697ffc7.

* Revert "update react-intl"

This reverts commit c77f329bb38910aeeba03869b72d77a8b0e00ba1.

* update react-native-keychain

* update and patch react-intl

* mend

* feedback during review 1
2025-04-07 09:30:06 +08:00

162 lines
5.1 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useEffect} from 'react';
import {type LayoutChangeEvent, type StyleProp, View, type ViewStyle, StyleSheet} from 'react-native';
import {Gesture, GestureDetector} from 'react-native-gesture-handler';
import Animated, {clamp, runOnJS, useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
import {useTheme} from '@context/theme';
import {changeOpacity} from '@utils/theme';
type ProgressBarProps = {
color: string;
containerStyle?: StyleProp<ViewStyle>;
progress: number;
withCursor?: boolean;
style?: StyleProp<ViewStyle>;
onSeek?: (position: number) => void;
}
const styles = StyleSheet.create({
container: {
position: 'relative',
justifyContent: 'center',
paddingVertical: 10,
},
progressBarContainer: {
height: 4,
borderRadius: 2,
overflow: 'hidden',
width: '100%',
},
progressBar: {
flex: 1,
},
cursor: {
position: 'absolute',
borderRadius: 100,
width: 15,
height: 15,
},
});
const START_CURSOR_VALUE = 0;
const END_CURSOR_VALUE = 1;
const ProgressBar = ({color, containerStyle, progress, withCursor, style, onSeek}: ProgressBarProps) => {
const theme = useTheme();
const widthValue = useSharedValue(0);
const progressValue = useSharedValue(progress);
const isGestureActive = useSharedValue(false);
// eslint-disable-next-line new-cap
const panGesture = Gesture.Pan().
onStart(() => {
isGestureActive.value = true;
}).
onChange((e) => {
if (onSeek) {
const clampedSeekPosition = clamp(e.x / widthValue.value, START_CURSOR_VALUE, END_CURSOR_VALUE);
runOnJS(onSeek)(clampedSeekPosition);
}
}).
onEnd(() => {
isGestureActive.value = false;
}).
onFinalize(() => {
isGestureActive.value = false;
});
// eslint-disable-next-line new-cap
const tapGesture = Gesture.Tap().
onStart(() => {
isGestureActive.value = true;
}).
onEnd((e) => {
if (onSeek) {
const clampedSeekPosition = clamp(e.x / widthValue.value, START_CURSOR_VALUE, END_CURSOR_VALUE);
runOnJS(onSeek)(clampedSeekPosition);
}
isGestureActive.value = false;
}).
onFinalize(() => {
isGestureActive.value = false;
});
// eslint-disable-next-line new-cap
const composedGestures = Gesture.Race(tapGesture, panGesture);
const progressAnimatedStyle = useAnimatedStyle(() => {
const translateX = ((progressValue.value * 0.5) - 0.5) * widthValue.value;
const scaleX = progressValue.value ? progressValue.value : 0.0001;
return {
transform: [
{translateX: isGestureActive.value ? translateX : withTiming(translateX, {duration: 200})},
{scaleX: isGestureActive.value ? scaleX : withTiming(scaleX, {duration: 200})},
],
width: widthValue.value,
};
});
const cursorAnimatedStyle = useAnimatedStyle(() => {
const cursorWidth = 15;
const leftPosition = (progressValue.value * widthValue.value) - (cursorWidth / 2);
return {
left: isGestureActive.value ? leftPosition : withTiming(leftPosition, {duration: 100}),
};
});
useEffect(() => {
progressValue.value = progress;
}, [progress]);
const onLayout = useCallback((e: LayoutChangeEvent) => {
widthValue.value = e.nativeEvent.layout.width;
}, []);
return (
<GestureDetector gesture={composedGestures}>
<View
onTouchStart={(e) => e.stopPropagation()}
style={[styles.container, containerStyle]}
>
<View
onLayout={onLayout}
style={[
styles.progressBarContainer,
{
backgroundColor: withCursor ? changeOpacity(theme.centerChannelColor, 0.2) : 'rgba(255, 255, 255, 0.16)',
},
style,
]}
>
<Animated.View
style={[
styles.progressBar,
{
backgroundColor: color,
},
progressAnimatedStyle,
]}
/>
</View>
{withCursor &&
<Animated.View
style={[
styles.cursor,
{
backgroundColor: color,
},
cursorAnimatedStyle,
]}
/>}
</View>
</GestureDetector>
);
};
export default ProgressBar;