mattermost-mobile/app/components/markdown/markdown_table/index.tsx
Elias Nahum b8c088cc70
Upgrade RN as well as update or replace other dependencies (#8011)
* start upgrade to RN 74

* migrate react-native-fs to expo-file-system

* exclude expo-asset module

* fix database manager remove directory

* fix: android network helper

* include expo on android

* temporarily disable android dep lock

* replace react-native-create-thumbnail with expo-video-thumnails

* update patches file version

* fix android build on 74

* create local library to replace MattermostManaged, Notifications and SplitView modules with new arch support

* migrate app to use new mattermost-rnutils library

* remove unused flipper class for android unsigned

* fix mattermost-rnutils android foldedObserver lifecycle

* use mattermost-rnutils on Android

* use mattermost-rnutils on iOS

* path react-native-navigation to not crash when activity is not NavigationActivity

* create local library for android share extension with new arch support

* Replace ShareModule with @mattermost/rnshare library

* remove ShareModule from android native code

* update react-intl

* update nodejs to 20.x.x npm to 10.x.x and dev deps

* update @gorhom/bottom-sheet

* use MattermostShare conditionally based on the platform

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

* remove unused react-native-calendars

* fix metro config

* fix terminate session race condition

* remove unused analytics

* replace react-native-device-info with expo-application and expo-device

* update @react-native-clipboard/clipboard

* update @react-native-community/datetimepicker

* update @react-native-community/netinfo

* update @sentry/react-native

* update react-native-document-picker

* update react-native-gesture-handler

* update react-native-share

* update react-native-svg and react-native-svg-transformer

* update react-native-vector-icons

* update babel

* update react-native-shadow-2

* update semver

* remove react-native-svg-transformer and convert svg files to svg components

* fix @mattermost/rnshare new-arch build on android

* remove react-native-create-thumbnail resolution in build.gradle

* create @mattermost/hardware-keyboard library to replace hw-keyboard-events

* fix hardware-keyboard library

* fix rnutils library

* create @mattermost/keyboard-tracker library

* replace react-native-keyboard-tracking-view with @mattermost/keyboard-tracker

* fix: rnutils to not crash on lateinit context

* fix: rnutils delete database

* revert changes to session_manager

* Removed react-native-webview and added expo-web-browser instead

With expo-web-browser we no longer need the webview for SSO login
the SSO login is now done by using "custom Chrome tabs" on Android
and ASWebAuthenticationSession on iOS

* remove patch for react-native-keyboard-tracking-view

* replace react-native-linear-gradient with expo-linear-gradient

* replace react-native-in-app-review with expo-store-review

* fix: shared group database directory on ios

* replace react-native-fast-image with expo-image

* remove unusued type def for react-native-keyboard-tracking-view

* replace react-native-elements and react-native-button with @rneui

* node version

* update sizzling methods

* fix tests using jest-expo

* replace jail-monkey with expo-device

* update babel deps

* update typescript eslint

* update rn and expo

* react-native-document-picker @react-native-camera-roll/camera-roll @react-native-community/datetimepicker react-native-reanimated react-native-safe-area-context

* update patches

* update @sentry/react-native

* upgrade react-native-navigation

* update expo & expo-image

* upgrade to working version of @sentry/react-native

* update node, cocoapods, bundler, fastlane versions

* @testing-library/react-native and eslint-plugin-jest

* fix: FloatingTextInput causing a crash with reanimated

* update sentry, localize, @types/lodash and uuid

* fix floating text input label

* update react-native-video

* fix: cannot calculate shadow efficiently on some components

* fix: reduce motion warning for bottomSheet

* fix: shadow on YouTube component

* update react-native-webrtc expo and @typescript-eslint

* audit fix

* fix swizzling bad merge

* temp use of github dependency for @mattermost libraries

* feedback review

* feedback review

* npm audit fix

* update bundle deps

* update @mattermost/react-native-turbo-log

* update deps
2024-06-19 09:33:45 +08:00

360 lines
12 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {LinearGradient} from 'expo-linear-gradient';
import React, {PureComponent, type ReactNode} from 'react';
import {injectIntl, type IntlShape} from 'react-intl';
import {Dimensions, type EventSubscription, type LayoutChangeEvent, Platform, type ScaledSize, ScrollView, type StyleProp, TouchableOpacity, View, type ViewStyle} from 'react-native';
import CompassIcon from '@components/compass_icon';
import {CELL_MAX_WIDTH, CELL_MIN_WIDTH} from '@components/markdown/markdown_table_cell';
import {Screens, Device} from '@constants';
import {goToScreen} from '@screens/navigation';
import {preventDoubleTap} from '@utils/tap';
import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme';
const MAX_HEIGHT = 300;
const MAX_PREVIEW_COLUMNS = 5;
type MarkdownTableState = {
cellWidth: number;
containerWidth: number;
contentHeight: number;
maxPreviewColumns: number;
}
type MarkdownTableInputProps = {
children: ReactNode;
numColumns: number;
}
type MarkdownTableProps = MarkdownTableInputProps & {
intl: IntlShape;
theme: Theme;
}
const getStyleSheet = makeStyleSheetFromTheme((theme) => {
return {
container: {
maxHeight: MAX_HEIGHT,
},
expandButton: {
height: 34,
width: 34,
},
iconContainer: {
maxWidth: '100%',
alignItems: 'flex-end',
paddingTop: 8,
paddingBottom: 4,
...Platform.select({
ios: {
paddingRight: 14,
},
}),
},
iconButton: {
backgroundColor: theme.centerChannelBg,
marginTop: -32,
marginRight: -6,
borderWidth: 1,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 50,
borderColor: changeOpacity(theme.centerChannelColor, 0.2),
width: 34,
height: 34,
},
icon: {
fontSize: 14,
color: theme.linkColor,
...Platform.select({
ios: {
fontSize: 13,
},
}),
},
displayFlex: {
flex: 1,
},
table: {
width: '100%',
borderColor: changeOpacity(theme.centerChannelColor, 0.2),
borderWidth: 1,
},
tablePadding: {
paddingRight: 10,
},
moreBelow: {
bottom: Platform.select({
ios: 34,
android: 33.75,
}),
height: 20,
position: 'absolute',
left: 0,
borderColor: changeOpacity(theme.centerChannelColor, 0.2),
},
moreRight: {
maxHeight: MAX_HEIGHT,
position: 'absolute',
top: 0,
width: 20,
borderColor: changeOpacity(theme.centerChannelColor, 0.2),
borderRightWidth: 1,
},
};
});
class MarkdownTable extends PureComponent<MarkdownTableProps, MarkdownTableState> {
private rowsSliced: boolean | undefined;
private colsSliced: boolean | undefined;
private dimensionsListener: EventSubscription | undefined;
state = {
containerWidth: 0,
contentHeight: 0,
cellWidth: 0,
maxPreviewColumns: 0,
};
componentDidMount() {
this.dimensionsListener = Dimensions.addEventListener('change', this.setMaxPreviewColumns);
const window = Dimensions.get('window');
this.setMaxPreviewColumns({window});
}
componentWillUnmount() {
this.dimensionsListener?.remove();
}
setMaxPreviewColumns = ({window}: {window: ScaledSize}) => {
const maxPreviewColumns = Math.floor(window.width / CELL_MIN_WIDTH);
this.setState({maxPreviewColumns});
};
getTableWidth = (isFullView = false) => {
const maxPreviewColumns = this.state.maxPreviewColumns || MAX_PREVIEW_COLUMNS;
const columns = Math.min(this.props.numColumns, maxPreviewColumns);
return (isFullView || columns === 1) ? columns * CELL_MAX_WIDTH : columns * CELL_MIN_WIDTH;
};
handlePress = preventDoubleTap(() => {
const {intl} = this.props;
const screen = Screens.TABLE;
const title = intl.formatMessage({
id: 'mobile.routes.table',
defaultMessage: 'Table',
});
const passProps = {
renderAsFlex: this.shouldRenderAsFlex(true),
renderRows: this.renderRows,
width: this.getTableWidth(true),
};
goToScreen(screen, title, passProps);
});
handleContainerLayout = (e: LayoutChangeEvent) => {
this.setState({
containerWidth: e.nativeEvent.layout.width,
});
};
handleContentSizeChange = (contentWidth: number, contentHeight: number) => {
this.setState({
contentHeight,
});
};
renderPreviewRows = (isFullView = false) => {
return this.renderRows(isFullView, true);
};
shouldRenderAsFlex = (isFullView = false) => {
const {numColumns} = this.props;
const {height, width} = Dimensions.get('window');
const isLandscape = width > height;
// render as flex in the channel screen, only for mobile phones on the portrait mode,
// and if tables have 2 ~ 4 columns
if (!isFullView && numColumns > 1 && numColumns < 4 && !Device.IS_TABLET) {
return true;
}
// render a 4 column table as flex when in landscape mode only
// otherwise it should expand beyond the device's full width
if (!isFullView && isLandscape && numColumns === 4) {
return true;
}
// render as flex in full table screen, only for mobile phones on portrait mode,
// and if tables have 3 or 4 columns
if (isFullView && numColumns >= 3 && numColumns <= 4 && !Device.IS_TABLET) {
return true;
}
return false;
};
getTableStyle = (isFullView: boolean) => {
const {theme} = this.props;
const style = getStyleSheet(theme);
const tableStyle: StyleProp<ViewStyle> = [style.table];
const renderAsFlex = this.shouldRenderAsFlex(isFullView);
if (renderAsFlex) {
tableStyle.push(style.displayFlex);
return tableStyle;
}
tableStyle.push({width: this.getTableWidth(isFullView)});
return tableStyle;
};
renderRows = (isFullView = false, isPreview = false) => {
const tableStyle = this.getTableStyle(isFullView);
let rows = React.Children.toArray(this.props.children) as React.ReactElement[];
if (isPreview) {
const {maxPreviewColumns} = this.state;
const prevRowLength = rows.length;
const prevColLength = React.Children.toArray(rows[0].props.children).length;
rows = rows.slice(0, maxPreviewColumns).map((row) => {
const children = React.Children.toArray(row.props.children).slice(0, maxPreviewColumns);
return {
...row,
props: {
...row.props,
children,
},
};
});
if (!rows.length) {
return null;
}
this.rowsSliced = prevRowLength > rows.length;
this.colsSliced = prevColLength > React.Children.toArray(rows[0].props.children).length;
}
// Add an extra prop to the last row of the table so that it knows not to render a bottom border
// since the container should be rendering that
rows[rows.length - 1] = React.cloneElement(rows[rows.length - 1], {
isLastRow: true,
});
// Add an extra prop to the first row of the table so that it can have a different background color
rows[0] = React.cloneElement(rows[0], {
isFirstRow: true,
});
return (
<View style={tableStyle}>
{rows}
</View>
);
};
render() {
const {containerWidth, contentHeight} = this.state;
const {theme} = this.props;
const style = getStyleSheet(theme);
const tableWidth = this.getTableWidth();
const renderAsFlex = this.shouldRenderAsFlex();
const previewRows = this.renderPreviewRows();
let leftOffset;
if (renderAsFlex || tableWidth > containerWidth) {
leftOffset = containerWidth - 20;
} else {
leftOffset = tableWidth - 20;
}
let expandButtonOffset = leftOffset;
if (Platform.OS === 'android') {
expandButtonOffset -= 10;
}
// Renders when the columns were sliced, or the table width exceeds the container,
// or if the columns exceed maximum allowed for previews
let moreRight = null;
if (this.colsSliced ||
(containerWidth && tableWidth > containerWidth && !renderAsFlex) ||
(this.props.numColumns > MAX_PREVIEW_COLUMNS)) {
moreRight = (
<LinearGradient
colors={[
changeOpacity(theme.centerChannelColor, 0.0),
changeOpacity(theme.centerChannelColor, 0.1),
]}
start={{x: 0, y: 0}}
end={{x: 1, y: 0}}
style={[style.moreRight, {height: contentHeight, left: leftOffset}]}
/>
);
}
let moreBelow = null;
if (this.rowsSliced || contentHeight > MAX_HEIGHT) {
const width = renderAsFlex ? '100%' : Math.min(tableWidth, containerWidth);
moreBelow = (
<LinearGradient
colors={[
changeOpacity(theme.centerChannelColor, 0.0),
changeOpacity(theme.centerChannelColor, 0.1),
]}
style={[style.moreBelow, {width}]}
/>
);
}
let expandButton = null;
if (expandButtonOffset > 0) {
expandButton = (
<TouchableOpacity
onPress={this.handlePress}
style={[style.expandButton, {left: expandButtonOffset}]}
testID='markdown_table.expand.button'
>
<View style={[style.iconContainer, {width: this.getTableWidth()}]}>
<View style={style.iconButton}>
<CompassIcon
name='arrow-expand'
style={style.icon}
/>
</View>
</View>
</TouchableOpacity>
);
}
return (
<TouchableOpacity
style={style.tablePadding}
onPress={this.handlePress}
testID='markdown_table'
>
<ScrollView
onContentSizeChange={this.handleContentSizeChange}
onLayout={this.handleContainerLayout}
scrollEnabled={false}
showsVerticalScrollIndicator={false}
style={style.container}
>
{previewRows}
</ScrollView>
{moreRight}
{moreBelow}
{expandButton}
</TouchableOpacity>
);
}
}
export default injectIntl(MarkdownTable);