MLE: Update
This commit is contained in:
parent
288510d1e7
commit
ede9193d9f
3 changed files with 91 additions and 19 deletions
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Vencord, a Discord client mod
|
||||
* Copyright (c) 2024 Vendicated and contributors
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { Button } from "@components/Button";
|
||||
import { Alerts, Toasts, useState } from "@webpack/common";
|
||||
|
||||
import { clearMessagesIDB } from "../db";
|
||||
import { Flogger } from "../index";
|
||||
|
||||
interface ClearLogsButtonProps {
|
||||
label?: string;
|
||||
onCleared?: () => void;
|
||||
}
|
||||
|
||||
export function ClearLogsButton({ label = "Clear Logs", onCleared }: ClearLogsButtonProps) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
return (
|
||||
<Button
|
||||
disabled={loading}
|
||||
variant="dangerPrimary"
|
||||
onClick={() => Alerts.show({
|
||||
title: "Clear Logs",
|
||||
body: "Are you sure you want to clear all logs?",
|
||||
// @ts-ignore
|
||||
confirmVariant: "critical-primary",
|
||||
confirmText: "Clear",
|
||||
cancelText: "Cancel",
|
||||
onConfirm: async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await clearMessagesIDB();
|
||||
onCleared?.();
|
||||
Toasts.show({
|
||||
id: Toasts.genId(),
|
||||
message: "Cleared Logs",
|
||||
type: Toasts.Type.SUCCESS
|
||||
});
|
||||
} catch (err) {
|
||||
Flogger.error("Failed to clear logs", err);
|
||||
Toasts.show({
|
||||
id: Toasts.genId(),
|
||||
message: "Failed to clear logs",
|
||||
type: Toasts.Type.FAILURE
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
})}
|
||||
>
|
||||
{loading ? "Clearing Logs..." : label}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
|
@ -258,7 +258,19 @@ export async function deleteMessagesBulkIDB(message_ids: string[]) {
|
|||
|
||||
export async function clearMessagesIDB(showToast = true) {
|
||||
cachedMessages.clear();
|
||||
await db.clear("messages");
|
||||
|
||||
const deleted = await new Promise<boolean>(resolve => {
|
||||
db.close();
|
||||
const req = indexedDB.deleteDatabase(DB_NAME);
|
||||
req.onsuccess = () => resolve(true);
|
||||
req.onerror = () => resolve(false);
|
||||
});
|
||||
|
||||
await initIDB();
|
||||
if (!deleted) await clearMessagesChunkedIDB();
|
||||
|
||||
cachedMessages.clear();
|
||||
|
||||
if (!showToast) return;
|
||||
|
||||
Toasts.show({
|
||||
|
|
@ -267,3 +279,20 @@ export async function clearMessagesIDB(showToast = true) {
|
|||
id: Toasts.genId()
|
||||
});
|
||||
}
|
||||
|
||||
// faster than db.clear on large dbs
|
||||
async function clearMessagesChunkedIDB() {
|
||||
const CLEAR_BATCH_SIZE = 5000;
|
||||
while (true) {
|
||||
const tx = db.transaction("messages", "readwrite", { durability: "relaxed" });
|
||||
const { store } = tx;
|
||||
const keys = (await store.getAllKeys(undefined, CLEAR_BATCH_SIZE)) as string[];
|
||||
if (keys.length === 0) {
|
||||
await tx.done;
|
||||
break;
|
||||
}
|
||||
|
||||
const range = IDBKeyRange.bound(keys[0], keys[keys.length - 1]);
|
||||
await Promise.all([store.delete(range), tx.done]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,10 @@ import { definePluginSettings, Settings } from "@api/Settings";
|
|||
import { Button } from "@components/Button";
|
||||
import ErrorBoundary from "@components/ErrorBoundary";
|
||||
import { OptionType } from "@utils/types";
|
||||
import { Alerts, useState } from "@webpack/common";
|
||||
import { useState } from "@webpack/common";
|
||||
|
||||
import { clearLogs, Native } from ".";
|
||||
import { Native } from ".";
|
||||
import { ClearLogsButton } from "./components/ClearLogsButton";
|
||||
import { ImageCacheDir, LogsDir } from "./components/FolderSelectInput";
|
||||
import { openLogModal } from "./components/LogsModal";
|
||||
import { blockedExts } from "./list";
|
||||
|
|
@ -300,22 +301,7 @@ export const settings = definePluginSettings({
|
|||
clearLogs: {
|
||||
type: OptionType.COMPONENT,
|
||||
description: "Clear Logs",
|
||||
component: () =>
|
||||
<Button
|
||||
variant="dangerPrimary"
|
||||
onClick={() => Alerts.show({
|
||||
title: "Clear Logs",
|
||||
body: "Are you sure you want to clear all logs?",
|
||||
confirmVariant: "critical-primary",
|
||||
confirmText: "Clear",
|
||||
cancelText: "Cancel",
|
||||
onConfirm: async () => {
|
||||
await clearLogs();
|
||||
},
|
||||
})}
|
||||
>
|
||||
Clear Logs
|
||||
</Button>
|
||||
component: () => <ClearLogsButton />
|
||||
},
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue