2023-09-04 19:08:43 +02:00
|
|
|
import { findByProps, find } from "@metro/filters";
|
2023-04-23 01:09:04 +02:00
|
|
|
import { ReactNative as RN, channels, url } from "@metro/common";
|
2023-04-23 02:43:34 +02:00
|
|
|
import { PROXY_PREFIX, THEMES_CHANNEL_ID } from "@lib/constants";
|
2023-04-23 01:09:04 +02:00
|
|
|
import { after, instead } from "@lib/patcher";
|
|
|
|
|
import { installPlugin } from "@lib/plugins";
|
|
|
|
|
import { installTheme } from "@lib/themes";
|
2023-04-23 02:43:34 +02:00
|
|
|
import { showConfirmationAlert } from "@ui/alerts";
|
2023-04-23 01:09:04 +02:00
|
|
|
import { getAssetIDByName } from "@ui/assets";
|
|
|
|
|
import { showToast } from "@ui/toasts";
|
2024-02-23 00:33:40 +08:00
|
|
|
import { isThemeSupported } from "@/lib/loader";
|
2023-04-23 01:09:04 +02:00
|
|
|
|
2023-09-04 19:08:43 +02:00
|
|
|
const showSimpleActionSheet = find((m) => m?.showSimpleActionSheet && !Object.getOwnPropertyDescriptor(m, "showSimpleActionSheet")?.get);
|
2023-04-23 01:09:04 +02:00
|
|
|
const handleClick = findByProps("handleClick");
|
|
|
|
|
const { openURL } = url;
|
|
|
|
|
const { getChannelId } = channels;
|
|
|
|
|
const { getChannel } = findByProps("getChannel");
|
|
|
|
|
|
|
|
|
|
const { TextStyleSheet } = findByProps("TextStyleSheet");
|
|
|
|
|
|
|
|
|
|
function typeFromUrl(url: string) {
|
|
|
|
|
if (url.startsWith(PROXY_PREFIX)) {
|
|
|
|
|
return "Plugin";
|
2024-02-23 00:33:40 +08:00
|
|
|
} else if (url.endsWith(".json") && isThemeSupported()) {
|
2023-04-23 01:09:04 +02:00
|
|
|
return "Theme";
|
|
|
|
|
} else return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function installWithToast(type: "Plugin" | "Theme", url: string) {
|
2023-09-04 19:08:43 +02:00
|
|
|
(type === "Plugin" ? installPlugin : installTheme)(url)
|
|
|
|
|
.then(() => {
|
|
|
|
|
showToast("Successfully installed", getAssetIDByName("Check"));
|
|
|
|
|
})
|
|
|
|
|
.catch((e: Error) => {
|
|
|
|
|
showToast(e.message, getAssetIDByName("Small"));
|
|
|
|
|
});
|
2023-04-23 01:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default () => {
|
2023-09-04 19:08:43 +02:00
|
|
|
const patches = new Array<Function>();
|
2023-04-23 01:09:04 +02:00
|
|
|
|
2023-09-04 19:08:43 +02:00
|
|
|
patches.push(
|
|
|
|
|
after("showSimpleActionSheet", showSimpleActionSheet, (args) => {
|
|
|
|
|
if (args[0].key !== "LongPressUrl") return;
|
|
|
|
|
const {
|
|
|
|
|
header: { title: url },
|
|
|
|
|
options,
|
|
|
|
|
} = args[0];
|
2023-04-23 01:09:04 +02:00
|
|
|
|
2023-09-04 19:08:43 +02:00
|
|
|
const urlType = typeFromUrl(url);
|
|
|
|
|
if (!urlType) return;
|
2023-04-23 01:09:04 +02:00
|
|
|
|
2023-09-04 19:08:43 +02:00
|
|
|
options.push({
|
|
|
|
|
label: `Install ${urlType}`,
|
|
|
|
|
onPress: () => installWithToast(urlType, url),
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
);
|
2023-04-23 01:09:04 +02:00
|
|
|
|
2023-09-04 19:08:43 +02:00
|
|
|
patches.push(
|
|
|
|
|
instead("handleClick", handleClick, async function (this: any, args, orig) {
|
|
|
|
|
const { href: url } = args[0];
|
2023-04-23 01:09:04 +02:00
|
|
|
|
2023-09-04 19:08:43 +02:00
|
|
|
const urlType = typeFromUrl(url);
|
|
|
|
|
if (!urlType) return orig.apply(this, args);
|
2023-04-23 01:09:04 +02:00
|
|
|
|
2023-09-04 19:08:43 +02:00
|
|
|
// Make clicking on theme links only work in #themes, should there be a theme proxy in the future, this can be removed.
|
|
|
|
|
if (urlType === "Theme" && getChannel(getChannelId())?.parent_id !== THEMES_CHANNEL_ID) return orig.apply(this, args);
|
|
|
|
|
|
|
|
|
|
showConfirmationAlert({
|
|
|
|
|
title: "Hold Up",
|
|
|
|
|
content: ["This link is a ", <RN.Text style={TextStyleSheet["text-md/semibold"]}>{urlType}</RN.Text>, ", would you like to install it?"],
|
|
|
|
|
onConfirm: () => installWithToast(urlType, url),
|
|
|
|
|
confirmText: "Install",
|
|
|
|
|
cancelText: "Cancel",
|
|
|
|
|
secondaryConfirmText: "Open in Browser",
|
|
|
|
|
onConfirmSecondary: () => openURL(url),
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
);
|
2023-04-23 01:09:04 +02:00
|
|
|
|
|
|
|
|
return () => patches.forEach((p) => p());
|
|
|
|
|
};
|