2023-02-26 22:56:36 +00:00
|
|
|
import { ButtonColors, Plugin } from "@types";
|
2023-03-17 21:58:37 +00:00
|
|
|
import { NavigationNative, clipboard } from "@metro/common";
|
2023-04-06 00:51:53 +01:00
|
|
|
import { MMKVManager } from "@lib/native";
|
2023-01-04 22:39:28 +00:00
|
|
|
import { getAssetIDByName } from "@ui/assets";
|
2023-01-07 23:05:14 +00:00
|
|
|
import { showToast } from "@ui/toasts";
|
2023-02-26 22:56:36 +00:00
|
|
|
import { showConfirmationAlert } from "@ui/alerts";
|
2023-04-06 00:51:53 +01:00
|
|
|
import { removePlugin, startPlugin, stopPlugin, getSettings, fetchPlugin } from "@lib/plugins";
|
2023-04-15 02:57:03 +01:00
|
|
|
import Card, { CardWrapper } from "@ui/settings/components/Card";
|
2023-01-03 00:18:19 +00:00
|
|
|
|
2023-04-15 02:57:03 +01:00
|
|
|
async function stopThenStart(plugin: Plugin, callback: Function) {
|
2023-04-06 00:51:53 +01:00
|
|
|
if (plugin.enabled) stopPlugin(plugin.id, false);
|
2023-04-15 02:57:03 +01:00
|
|
|
callback();
|
2023-04-06 00:51:53 +01:00
|
|
|
if (plugin.enabled) await startPlugin(plugin.id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 02:57:03 +01:00
|
|
|
export default function PluginCard({ item: plugin, index }: CardWrapper<Plugin>) {
|
2023-02-04 22:37:28 +00:00
|
|
|
const settings = getSettings(plugin.id);
|
|
|
|
|
const navigation = NavigationNative.useNavigation();
|
2023-01-07 23:05:14 +00:00
|
|
|
const [removed, setRemoved] = React.useState(false);
|
2023-02-16 02:16:51 +00:00
|
|
|
|
2023-01-29 22:05:47 +01:00
|
|
|
// This is needed because of React™
|
|
|
|
|
if (removed) return null;
|
2023-01-07 23:05:14 +00:00
|
|
|
|
2023-03-17 21:58:37 +00:00
|
|
|
return (
|
2023-03-23 19:07:03 -04:00
|
|
|
<Card
|
2023-03-17 21:58:37 +00:00
|
|
|
index={index}
|
|
|
|
|
// TODO: Actually make use of user IDs
|
|
|
|
|
headerLabel={`${plugin.manifest.name} by ${plugin.manifest.authors.map(i => i.name).join(", ")}`}
|
|
|
|
|
headerIcon={plugin.manifest.vendetta?.icon || "ic_application_command_24px"}
|
|
|
|
|
toggleType="switch"
|
|
|
|
|
toggleValue={plugin.enabled}
|
|
|
|
|
onToggleChange={(v: boolean) => {
|
|
|
|
|
try {
|
|
|
|
|
if (v) startPlugin(plugin.id); else stopPlugin(plugin.id);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showToast((e as Error).message, getAssetIDByName("Small"));
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
descriptionLabel={plugin.manifest.description}
|
2023-03-23 19:07:03 -04:00
|
|
|
overflowTitle={plugin.manifest.name}
|
|
|
|
|
overflowActions={[
|
2023-04-06 00:51:53 +01:00
|
|
|
{
|
|
|
|
|
icon: "ic_sync_24px",
|
|
|
|
|
label: "Refetch",
|
|
|
|
|
onPress: async () => {
|
|
|
|
|
stopThenStart(plugin, () => {
|
|
|
|
|
fetchPlugin(plugin.id).then(async () => {
|
|
|
|
|
showToast("Successfully refetched plugin.", getAssetIDByName("toast_image_saved"));
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
showToast("Failed to refetch plugin!", getAssetIDByName("Small"));
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: "copy",
|
|
|
|
|
label: "Copy URL",
|
|
|
|
|
onPress: () => {
|
|
|
|
|
clipboard.setString(plugin.id);
|
|
|
|
|
showToast("Copied plugin URL to clipboard.", getAssetIDByName("toast_copy_link"));
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-23 19:07:03 -04:00
|
|
|
{
|
|
|
|
|
icon: "ic_download_24px",
|
|
|
|
|
label: plugin.update ? "Disable updates" : "Enable updates",
|
|
|
|
|
onPress: () => {
|
|
|
|
|
plugin.update = !plugin.update;
|
|
|
|
|
showToast(`${plugin.update ? "Enabled" : "Disabled"} updates for ${plugin.manifest.name}.`, getAssetIDByName("toast_image_saved"));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-04-06 00:51:53 +01:00
|
|
|
icon: "ic_duplicate",
|
|
|
|
|
label: "Clear data",
|
|
|
|
|
isDestructive: true,
|
|
|
|
|
onPress: () => showConfirmationAlert({
|
|
|
|
|
title: "Wait!",
|
|
|
|
|
content: `Are you sure you wish to clear the data of ${plugin.manifest.name}?`,
|
|
|
|
|
confirmText: "Clear",
|
|
|
|
|
cancelText: "Cancel",
|
|
|
|
|
confirmColor: ButtonColors.RED,
|
|
|
|
|
onConfirm: () => {
|
|
|
|
|
stopThenStart(plugin, () => {
|
|
|
|
|
try {
|
|
|
|
|
MMKVManager.removeItem(plugin.id);
|
|
|
|
|
showToast(`Cleared data for ${plugin.manifest.name}.`, getAssetIDByName("trash"));
|
|
|
|
|
} catch {
|
|
|
|
|
showToast(`Failed to clear data for ${plugin.manifest.name}!`, getAssetIDByName("Small"));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}),
|
2023-03-23 19:07:03 -04:00
|
|
|
},
|
2023-03-17 21:58:37 +00:00
|
|
|
{
|
|
|
|
|
icon: "ic_message_delete",
|
2023-04-06 00:51:53 +01:00
|
|
|
label: "Delete",
|
2023-03-23 19:07:03 -04:00
|
|
|
isDestructive: true,
|
2023-03-17 21:58:37 +00:00
|
|
|
onPress: () => showConfirmationAlert({
|
|
|
|
|
title: "Wait!",
|
2023-04-06 00:51:53 +01:00
|
|
|
content: `Are you sure you wish to delete ${plugin.manifest.name}? This will clear all of the plugin's data.`,
|
2023-03-17 21:58:37 +00:00
|
|
|
confirmText: "Delete",
|
|
|
|
|
cancelText: "Cancel",
|
|
|
|
|
confirmColor: ButtonColors.RED,
|
|
|
|
|
onConfirm: () => {
|
2023-02-20 18:43:50 +00:00
|
|
|
try {
|
2023-03-17 21:58:37 +00:00
|
|
|
removePlugin(plugin.id);
|
|
|
|
|
setRemoved(true);
|
2023-02-20 18:43:50 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
showToast((e as Error).message, getAssetIDByName("Small"));
|
|
|
|
|
}
|
2023-03-17 21:58:37 +00:00
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
},
|
2023-03-23 19:07:03 -04:00
|
|
|
]}
|
|
|
|
|
actions={[
|
2023-03-17 21:58:37 +00:00
|
|
|
...(settings ? [{
|
|
|
|
|
icon: "settings",
|
|
|
|
|
onPress: () => navigation.push("VendettaCustomPage", {
|
|
|
|
|
title: plugin.manifest.name,
|
|
|
|
|
render: settings,
|
|
|
|
|
})
|
|
|
|
|
}] : []),
|
|
|
|
|
]}
|
|
|
|
|
/>
|
2023-01-03 00:18:19 +00:00
|
|
|
)
|
2023-01-29 22:05:47 +01:00
|
|
|
}
|