forked from mattermost/mattermost-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
139 lines (121 loc) · 4.58 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import MattermostShare, {type ShareExtensionDataToSend, type SharedItem} from '@mattermost/rnshare';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import React, {useEffect, useMemo, useState} from 'react';
import {IntlProvider} from 'react-intl';
import {Appearance, BackHandler} from 'react-native';
import {getDefaultThemeByAppearance} from '@context/theme';
import {DEFAULT_LOCALE, getTranslations} from '@i18n';
import {initialize} from '@init/app';
import PerformanceMetricsManager from '@managers/performance_metrics_manager';
import {extractStartLink, isValidUrl} from '@utils/url';
import ChannelsScreen from './screens/channels';
import ServersScreen from './screens/servers';
import ShareScreen from './screens/share';
const Stack = createStackNavigator();
const closeExtension = (data: ShareExtensionDataToSend | null) => {
MattermostShare.close(data);
};
const ShareExtension = () => {
const [data, setData] = useState<SharedItem[]>();
const [theme, setTheme] = useState<Theme>(getDefaultThemeByAppearance());
const defaultNavigationOptions = useMemo(() => ({
headerStyle: {
backgroundColor: theme.sidebarHeaderBg,
},
headerTitleStyle: {
marginHorizontal: 0,
left: 0,
color: theme.sidebarHeaderTextColor,
},
headerBackTitleStyle: {
color: theme.sidebarHeaderTextColor,
margin: 0,
},
headerTintColor: theme.sidebarHeaderTextColor,
headerTopInsetEnabled: false,
cardStyle: {backgroundColor: theme.centerChannelBg},
}), [theme]);
const {text: message, link: linkPreviewUrl} = useMemo(() => {
let text = data?.filter((i) => i.isString)[0]?.value;
let link;
if (text) {
const first = extractStartLink(text);
if (first && isValidUrl(first)) {
link = first;
text = text.replace(first, '');
}
}
return {text, link};
}, [data]);
const files = useMemo(() => {
return data?.filter((i) => !i.isString) || [];
}, [data]);
useEffect(() => {
// Since the share functionality inits the app, the init mark gets set
// at this point. Therefore, any check on load times after this is done
// over the wrong value.
PerformanceMetricsManager.skipLoadMetric();
}, []);
useEffect(() => {
initialize().finally(async () => {
const items = await MattermostShare.getSharedData();
setData(items);
});
const backListener = BackHandler.addEventListener('hardwareBackPress', () => {
const scene = MattermostShare.getCurrentActivityName();
if (scene === 'ShareActivity') {
closeExtension(null);
return true;
}
return false;
});
const appearanceListener = Appearance.addChangeListener(() => {
setTheme(getDefaultThemeByAppearance());
});
return () => {
backListener.remove();
appearanceListener.remove();
};
}, []);
if (!data) {
return null;
}
return (
<IntlProvider
locale={DEFAULT_LOCALE}
messages={getTranslations(DEFAULT_LOCALE)}
>
<NavigationContainer>
<Stack.Navigator
initialRouteName='Share'
screenOptions={defaultNavigationOptions}
>
<Stack.Screen name='Share'>
{() => (
<ShareScreen
files={files}
linkPreviewUrl={linkPreviewUrl}
message={message}
theme={theme}
/>
)}
</Stack.Screen>
<Stack.Screen name='Servers'>
{() => (
<ServersScreen theme={theme}/>
)}
</Stack.Screen>
<Stack.Screen name='Channels'>
{() => (
<ChannelsScreen theme={theme}/>
)}
</Stack.Screen>
</Stack.Navigator>
</NavigationContainer>
</IntlProvider>
);
};
export default ShareExtension;