Skip to content

Commit

Permalink
use default browser, fix crash for matrix links, fix crash for search… (
Browse files Browse the repository at this point in the history
  • Loading branch information
gkasdorf authored Jul 8, 2023
1 parent 90fb210 commit 6bea36c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/components/screens/Feed/CommunityFeedScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function FeedsCommunityScreen({
<Text fontSize={16} fontWeight="semibold">
{communityName.toString()}
</Text>
<Text fontSize={12}>@{getBaseUrl(actorId.toString())}</Text>
<Text fontSize={12}>@{getBaseUrl(actorId?.toString())}</Text>
</VStack>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useActionSheet } from "@expo/react-native-action-sheet";
import { TableView } from "@gkasdorf/react-native-tableview-simple";
import { ScrollView, useTheme } from "native-base";
import React from "react";
import { StyleSheet } from "react-native";
import { StyleSheet, Switch } from "react-native";
import { setSetting } from "../../../../slices/settings/settingsActions";
import { selectSettings } from "../../../../slices/settings/settingsSlice";
import { useAppDispatch, useAppSelector } from "../../../../../store";
Expand All @@ -17,6 +17,10 @@ function GeneralSettingsScreen() {
const theme = useTheme();
const { showActionSheetWithOptions } = useActionSheet();

const onChange = (key: string, value: any) => {
dispatch(setSetting({ [key]: value }));
};

return (
<ScrollView backgroundColor={theme.colors.app.bg} flex={1}>
<TableView style={styles.table}>
Expand Down Expand Up @@ -48,6 +52,20 @@ function GeneralSettingsScreen() {
}}
/>
</CSection>
<CSection header="BROWSER">
<CCell
title="Use Default Browser"
backgroundColor={theme.colors.app.fg}
titleTextColor={theme.colors.app.textPrimary}
rightDetailColor={theme.colors.app.textSecondary}
cellAccessoryView={
<Switch
value={settings.useDefaultBrowser}
onValueChange={(v) => onChange("useDefaultBrowser", v)}
/>
}
/>
</CSection>
</TableView>
</ScrollView>
);
Expand Down
14 changes: 12 additions & 2 deletions src/helpers/LinkHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as WebBrowser from "expo-web-browser";
import { WebBrowserPresentationStyle } from "expo-web-browser";
import { NativeStackNavigationProp } from "@react-navigation/native-stack";
import { Alert } from "react-native";
import { Alert, Linking } from "react-native";

import axios from "axios";
import { URL } from "react-native-url-polyfill";
Expand Down Expand Up @@ -157,17 +157,27 @@ const openLemmyLink = (
};

const openWebLink = (link: string): void => {
const { settings } = store.getState();
const urlPattern =
/(http|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])/;

const { settings } = store.getState();
try {
writeToLog(`Trying to open link: ${link}`);

let fixedLink = decodeURIComponent(link);
fixedLink = fixedLink.match(urlPattern)[0];
fixedLink = fixedLink.replace("%5D", "");

// TODO Remove this once Expo publishes new fix. For now this will stop matrix crashes

const matrixCheck = /#/g;
const matrixCheckCount = (link.match(matrixCheck) || []).length;

if (settings.useDefaultBrowser || matrixCheckCount > 1) {
Linking.openURL(link).then();
return;
}

WebBrowser.openBrowserAsync(fixedLink, {
dismissButtonStyle: "close",
readerMode: settings.useReaderMode,
Expand Down
2 changes: 2 additions & 0 deletions src/slices/settings/settingsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface SettingsState {
hideReadPostsOnFeed: boolean;
appIcon: string;
showCommentActions: boolean;
useDefaultBrowser: boolean;
}

const initialState: SettingsState = {
Expand Down Expand Up @@ -72,6 +73,7 @@ const initialState: SettingsState = {
hideReadPostsOnFeed: false,
appIcon: "purple",
showCommentActions: true,
useDefaultBrowser: false,
};

const settingsSlice = createSlice({
Expand Down

0 comments on commit 6bea36c

Please sign in to comment.