mattermost-mobile/app/screens/bottom_sheet/index.tsx
Felipe Martin 714c3dc769
feat: AI rewrite (#9280)
* feat: ai rewrite

* feat: allow crating content apart from editing it

* feat: feature parity with webapp

* feat: feature parity with webapp

* chore: fixed padding

* map ux to webapp

* refactored ai rewrite logic to separate package

* chore: tests and lint

* Update app/products/ai/rewrite/screens/options/options.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* rewrite post editor animation

* i18n

* ui feedback, centered icon and less top padding

* chore: lint

* Consolidate @ai product into @agents

Move all rewrite functionality from app/products/ai/ into
app/products/agents/ to unify under a single namespace.

- Add rewrite types, store, hooks, components, and screens to agents
- Merge AI client methods into agents client
- Update screen constants (AI_* -> AGENTS_*)
- Update all consumer imports
- Remove AI state from EphemeralStore
- Remove @ai path alias from config
- Delete app/products/ai/ directory

* refactor: load screens from product package

* refactor: move detection logic to the agents pacakge

* refactor: remove "backwards compatibility"

* refactor: move hooks to proper package

* refactor: styles

* refactor: remove unneedd position attribute

* refactor: remove unneeded cancel animation calls

* refactor: "backwards compat"

* refactor: optimize renderContent with useCallback for performance

* refactor: rename variable to avoid confusion

* refactor: update handleRewrite to use async/await for better error handling

* refactor: simplify handleRewrite by always dismissing keyboard

* refactor: use hook, always all keyboard.dismiss

* chore: enhance AgentSelector component with FlatList support

* feat: add rewriteMessage function for AI message rewriting and integrate it into useRewrite hook

* refactor: simplify message length calculation in useHandleSendMessage hook

* refactor: consolidate agent screen constants and integrate with existing screens

* fix: update dependency array in useMemo for isUnrevealedPost to include post expiration metadata

* refactor: remove unused variable to clean up Typing component

* refactor: simplify logic for atDisabled and slashDisabled flags in QuickActions component

* feat: add AIRewriteAction component for AI message rewriting functionality

* revert: thread.ts changes manually

* refactor: integrate useSafeAreaInsets

* refactor: cleanup unused methods

* chore: add comment to clarify casting

* refactor: use_agents

* chore: lint and test

* fix: trim

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-02-16 16:48:57 +01:00

284 lines
8.5 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import BottomSheetM, {BottomSheetBackdrop, BottomSheetScrollView, BottomSheetView, type BottomSheetBackdropProps} from '@gorhom/bottom-sheet';
import React, {type ReactNode, useCallback, useEffect, useMemo, useRef} from 'react';
import {DeviceEventEmitter, type Handle, InteractionManager, Keyboard, ScrollView, type StyleProp, View, type ViewStyle} from 'react-native';
import {ReduceMotion, useReducedMotion, type WithSpringConfig} from 'react-native-reanimated';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {Events} from '@constants';
import {useTheme} from '@context/theme';
import useAndroidHardwareBackHandler from '@hooks/android_back_handler';
import {useBottomSheetListsFix} from '@hooks/bottom_sheet_lists_fix';
import {useIsTablet} from '@hooks/device';
import useNavButtonPressed from '@hooks/navigation_button_pressed';
import SecurityManager from '@managers/security_manager';
import {dismissModal} from '@screens/navigation';
import {hapticFeedback} from '@utils/general';
import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme';
import Indicator from './indicator';
import type {AvailableScreens} from '@typings/screens/navigation';
export {default as BottomSheetButton, BUTTON_HEIGHT} from './button';
export {default as BottomSheetContent, TITLE_HEIGHT} from './content';
export const BOTTOM_SHEET_ANDROID_OFFSET = 12;
type Props = {
closeButtonId?: string;
componentId: AvailableScreens;
contentStyle?: StyleProp<ViewStyle>;
initialSnapIndex?: number;
footerComponent?: React.FC<unknown>;
renderContent: () => ReactNode;
snapPoints?: Array<string | number>;
enableDynamicSizing?: boolean;
testID?: string;
scrollable?: boolean;
keyboardBehavior?: 'extend' | 'fillParent' | 'interactive';
keyboardBlurBehavior?: 'none' | 'restore';
}
const PADDING_TOP_MOBILE = 20;
const PADDING_TOP_TABLET = 8;
export const getStyleSheet = makeStyleSheetFromTheme((theme: Theme) => {
return {
bottomSheet: {
backgroundColor: theme.centerChannelBg,
borderTopStartRadius: 24,
borderTopEndRadius: 24,
shadowOffset: {
width: 0,
height: 8,
},
shadowOpacity: 0.12,
shadowRadius: 24,
shadowColor: '#000',
elevation: 24,
},
bottomSheetBackground: {
backgroundColor: theme.centerChannelBg,
borderColor: changeOpacity(theme.centerChannelColor, 0.16),
},
content: {
flex: 1,
paddingHorizontal: 20,
paddingTop: PADDING_TOP_MOBILE,
},
contentTablet: {
paddingTop: PADDING_TOP_TABLET,
},
separator: {
height: 1,
borderTopWidth: 1,
borderColor: changeOpacity(theme.centerChannelColor, 0.08),
},
view: {
flex: 1,
},
};
});
export const animatedConfig: Omit<WithSpringConfig, 'velocity'> = {
damping: 50,
mass: 0.3,
stiffness: 121.6,
overshootClamping: true,
restSpeedThreshold: 0.3,
restDisplacementThreshold: 0.3,
};
const BottomSheet = ({
closeButtonId,
componentId,
contentStyle,
initialSnapIndex = 1,
footerComponent,
renderContent,
snapPoints = [1, '50%', '80%'],
testID,
enableDynamicSizing = false,
scrollable = false,
keyboardBehavior = 'extend',
keyboardBlurBehavior = 'restore',
}: Props) => {
const reducedMotion = useReducedMotion();
const sheetRef = useRef<BottomSheetM>(null);
const isTablet = useIsTablet();
const insets = useSafeAreaInsets();
const theme = useTheme();
const styles = getStyleSheet(theme);
const interaction = useRef<Handle>();
const timeoutRef = useRef<NodeJS.Timeout>();
const {enabled, panResponder} = useBottomSheetListsFix();
const animationConfigs = useMemo(() => ({
...animatedConfig,
reduceMotion: reducedMotion ? ReduceMotion.Always : ReduceMotion.Never,
}), [reducedMotion]);
useEffect(() => {
interaction.current = InteractionManager.createInteractionHandle();
}, []);
const bottomSheetBackgroundStyle = useMemo(() => [
styles.bottomSheetBackground,
{borderWidth: isTablet ? 0 : 1},
], [isTablet, styles]);
const close = useCallback(() => {
dismissModal({componentId});
}, [componentId]);
useEffect(() => {
const listener = DeviceEventEmitter.addListener(Events.CLOSE_BOTTOM_SHEET, () => {
if (sheetRef.current) {
sheetRef.current.close();
} else {
close();
}
});
return () => listener.remove();
}, [close]);
const handleAnimationStart = useCallback(() => {
if (!interaction.current) {
interaction.current = InteractionManager.createInteractionHandle();
}
}, []);
const handleClose = useCallback(() => {
if (sheetRef.current) {
sheetRef.current.close();
} else {
close();
}
}, [close]);
const handleChange = useCallback((index: number) => {
timeoutRef.current = setTimeout(() => {
if (interaction.current) {
InteractionManager.clearInteractionHandle(interaction.current);
interaction.current = undefined;
}
});
if (index <= 0) {
close();
}
}, [close]);
useAndroidHardwareBackHandler(componentId, handleClose);
useNavButtonPressed(closeButtonId || '', componentId, close, [close]);
useEffect(() => {
hapticFeedback();
Keyboard.dismiss();
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
if (interaction.current) {
InteractionManager.clearInteractionHandle(interaction.current);
}
};
}, []);
const renderBackdrop = useCallback((props: BottomSheetBackdropProps) => {
return (
<BottomSheetBackdrop
{...props}
disappearsOnIndex={0}
appearsOnIndex={1}
opacity={0.6}
/>
);
}, []);
const renderContainerContent = () => (
<View
style={[styles.content, isTablet && styles.contentTablet, contentStyle]}
testID={`${testID}.screen`}
>
{renderContent()}
</View>
);
const scrollViewProps = {
style: styles.view,
showsVerticalScrollIndicator: false,
scrollEnabled: enabled,
...panResponder.panHandlers,
};
if (isTablet) {
const FooterComponent = footerComponent;
let content = renderContainerContent();
if (scrollable) {
content = (
<ScrollView {...scrollViewProps}>
{content}
</ScrollView>
);
}
return (
<View
style={styles.view}
nativeID={SecurityManager.getShieldScreenId(componentId)}
>
<View style={styles.separator}/>
{content}
{FooterComponent && (<FooterComponent/>)}
</View>
);
}
let content;
if (scrollable) {
content = (
<BottomSheetScrollView {...scrollViewProps}>
{renderContainerContent()}
</BottomSheetScrollView>
);
} else {
content = (
<BottomSheetView style={styles.view}>
{renderContainerContent()}
</BottomSheetView>
);
}
return (
<BottomSheetM
ref={sheetRef}
index={initialSnapIndex}
snapPoints={snapPoints}
animateOnMount={true}
backdropComponent={renderBackdrop}
onAnimate={handleAnimationStart}
onChange={handleChange}
animationConfigs={animationConfigs}
handleComponent={Indicator}
style={styles.bottomSheet}
backgroundStyle={bottomSheetBackgroundStyle}
footerComponent={footerComponent}
keyboardBehavior={keyboardBehavior}
keyboardBlurBehavior={keyboardBlurBehavior}
onClose={close}
bottomInset={insets.bottom}
enableDynamicSizing={enableDynamicSizing}
>
{content}
</BottomSheetM>
);
};
export default BottomSheet;