diff options
author | V <vendicated@riseup.net> | 2023-07-26 01:27:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-26 01:27:04 +0200 |
commit | 8620a1d86d302c23d51c1f6795f32cc8eb5e9207 (patch) | |
tree | d3144c2c84173c3d614499080505395f8b64352f /src/utils | |
parent | 198b35ffdce1ca7ba316409774bc816ac5b58e57 (diff) | |
download | Vencord-8620a1d86d302c23d51c1f6795f32cc8eb5e9207.tar.gz Vencord-8620a1d86d302c23d51c1f6795f32cc8eb5e9207.tar.bz2 Vencord-8620a1d86d302c23d51c1f6795f32cc8eb5e9207.zip |
New plugin: VoiceMessages (#1380)
Co-authored-by: V <vendicated@riseup.net>
Co-authored-by: Justice Almanzar <superdash993@gmail.com>
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/IpcEvents.ts | 1 | ||||
-rw-r--r-- | src/utils/react.tsx | 23 | ||||
-rw-r--r-- | src/utils/settingsSync.ts | 38 | ||||
-rw-r--r-- | src/utils/web.ts | 25 |
4 files changed, 62 insertions, 25 deletions
diff --git a/src/utils/IpcEvents.ts b/src/utils/IpcEvents.ts index 41d40a7..6994c91 100644 --- a/src/utils/IpcEvents.ts +++ b/src/utils/IpcEvents.ts @@ -32,4 +32,5 @@ export const enum IpcEvents { OPEN_MONACO_EDITOR = "VencordOpenMonacoEditor", OPEN_IN_APP__RESOLVE_REDIRECT = "VencordOIAResolveRedirect", + VOICE_MESSAGES_READ_RECORDING = "VencordVMReadRecording", } diff --git a/src/utils/react.tsx b/src/utils/react.tsx index a4c7152..e8c1081 100644 --- a/src/utils/react.tsx +++ b/src/utils/react.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { React, useEffect, useReducer, useState } from "@webpack/common"; +import { React, useEffect, useMemo, useReducer, useState } from "@webpack/common"; import { makeLazy } from "./lazy"; import { checkIntersecting } from "./misc"; @@ -135,3 +135,24 @@ export function LazyComponent<T extends object = any>(factory: () => React.Compo return <Component {...props} />; }; } + +interface TimerOpts { + interval?: number; + deps?: unknown[]; +} + +export function useTimer({ interval = 1000, deps = [] }: TimerOpts) { + const [time, setTime] = useState(0); + const start = useMemo(() => Date.now(), deps); + + useEffect(() => { + const intervalId = setInterval(() => setTime(Date.now() - start), interval); + + return () => { + setTime(0); + clearInterval(intervalId); + }; + }, deps); + + return time; +} diff --git a/src/utils/settingsSync.ts b/src/utils/settingsSync.ts index 72c876f..8766cbb 100644 --- a/src/utils/settingsSync.ts +++ b/src/utils/settingsSync.ts @@ -23,7 +23,7 @@ import { deflateSync, inflateSync } from "fflate"; import { getCloudAuth, getCloudUrl } from "./cloud"; import { Logger } from "./Logger"; -import { saveFile } from "./web"; +import { chooseFile, saveFile } from "./web"; export async function importSettings(data: string) { try { @@ -91,30 +91,20 @@ export async function uploadSettingsBackup(showToast = true): Promise<void> { } } } else { - const input = document.createElement("input"); - input.type = "file"; - input.style.display = "none"; - input.accept = "application/json"; - input.onchange = async () => { - const file = input.files?.[0]; - if (!file) return; - - const reader = new FileReader(); - reader.onload = async () => { - try { - await importSettings(reader.result as string); - if (showToast) toastSuccess(); - } catch (err) { - new Logger("SettingsSync").error(err); - if (showToast) toastFailure(err); - } - }; - reader.readAsText(file); - }; + const file = await chooseFile("application/json"); + if (!file) return; - document.body.appendChild(input); - input.click(); - setImmediate(() => document.body.removeChild(input)); + const reader = new FileReader(); + reader.onload = async () => { + try { + await importSettings(reader.result as string); + if (showToast) toastSuccess(); + } catch (err) { + new Logger("SettingsSync").error(err); + if (showToast) toastFailure(err); + } + }; + reader.readAsText(file); } } diff --git a/src/utils/web.ts b/src/utils/web.ts index 9cfe718..5c46aec 100644 --- a/src/utils/web.ts +++ b/src/utils/web.ts @@ -16,6 +16,10 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +/** + * Prompts the user to save a file to their system + * @param file The file to save + */ export function saveFile(file: File) { const a = document.createElement("a"); a.href = URL.createObjectURL(file); @@ -28,3 +32,24 @@ export function saveFile(file: File) { document.body.removeChild(a); }); } + +/** + * Prompts the user to choose a file from their system + * @param mimeTypes A comma separated list of mime types to accept, see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept#unique_file_type_specifiers + * @returns A promise that resolves to the chosen file or null if the user cancels + */ +export function chooseFile(mimeTypes: string) { + return new Promise<File | null>(resolve => { + const input = document.createElement("input"); + input.type = "file"; + input.style.display = "none"; + input.accept = mimeTypes; + input.onchange = async () => { + resolve(input.files?.[0] ?? null); + }; + + document.body.appendChild(input); + input.click(); + setImmediate(() => document.body.removeChild(input)); + }); +} |