fix timezonedb

This commit is contained in:
Manti 2024-04-08 16:55:34 +03:00
parent 9d836dd5c8
commit 558f2a5e7c
2 changed files with 30 additions and 54 deletions

View file

@ -1,27 +1,16 @@
/* /*
* Vencord, a modification for Discord's desktop app * Vencord, a Discord client mod
* Copyright (c) 2022 Vendicated and contributors * Copyright (c) 2023 Vendicated and contributors
* * SPDX-License-Identifier: GPL-3.0-or-later
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import * as DataStore from "@api/DataStore"; import * as DataStore from "@api/DataStore";
import { VENCORD_USER_AGENT } from "@utils/constants"; import { findStoreLazy } from "@webpack";
import { debounce } from "@utils/debounce";
import { findLazy } from "@webpack";
export const DATASTORE_KEY = "plugins.Timezones.savedTimezones"; export const DATASTORE_KEY = "plugins.Timezones.savedTimezones";
import { debounce } from "@shared/debounce";
import { VENCORD_USER_AGENT } from "@shared/vencordUserAgent";
import { CustomTimezonePreference } from "./settings"; import { CustomTimezonePreference } from "./settings";
export interface TimezoneDB { export interface TimezoneDB {
@ -31,10 +20,11 @@ export interface TimezoneDB {
export const API_URL = "https://timezonedb.catvibers.me"; export const API_URL = "https://timezonedb.catvibers.me";
const Cache: Record<string, string> = {}; const Cache: Record<string, string> = {};
const PreloadedUserSettings = findLazy(m => m.ProtoClass?.typeName === "discord_protos.discord_users.v1.PreloadedUserSettings"); const UserSettingsProtoStore = findStoreLazy("UserSettingsProtoStore");
export function getTimeString(timezone: string, timestamp = new Date()): string { export function getTimeString(timezone: string, timestamp = new Date()): string {
try { try {
const locale = PreloadedUserSettings.getCurrentValue().localization.locale.value; const locale = UserSettingsProtoStore.settings.localization.locale.value;
return new Intl.DateTimeFormat(locale, { hour: "numeric", minute: "numeric", timeZone: timezone }).format(timestamp); // we hate javascript return new Intl.DateTimeFormat(locale, { hour: "numeric", minute: "numeric", timeZone: timezone }).format(timestamp); // we hate javascript
} catch (e) { } catch (e) {
return "Error"; // incase it gets invalid timezone from api, probably not gonna happen but if it does this will prevent discord from crashing return "Error"; // incase it gets invalid timezone from api, probably not gonna happen but if it does this will prevent discord from crashing
@ -123,7 +113,6 @@ const timezonesLink = `https://gist.githubusercontent.com/ArjixWasTaken/${gist}/
export const getAllTimezones = async (): Promise<string[]> => { export const getAllTimezones = async (): Promise<string[]> => {
if (typeof Intl !== "undefined" && "supportedValuesOf" in Intl) { if (typeof Intl !== "undefined" && "supportedValuesOf" in Intl) {
try { try {
// @ts-expect-error fuck you typescript
return Intl.supportedValuesOf("timeZone"); return Intl.supportedValuesOf("timeZone");
} catch { } } catch { }
} }

View file

@ -1,37 +1,22 @@
/* /*
* Vencord, a modification for Discord's desktop app * Vencord, a Discord client mod
* Copyright (c) 2022 Vendicated and contributors * Copyright (c) 2023 Vendicated and contributors
* * SPDX-License-Identifier: GPL-3.0-or-later
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import * as DataStore from "@api/DataStore"; import * as DataStore from "@api/DataStore";
import { Devs, VENCORD_USER_AGENT } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { findByCodeLazy, findByPropsLazy } from "@webpack"; import { findByPropsLazy } from "@webpack";
import { React, SearchableSelect, Text, Toasts, UserStore } from "@webpack/common"; import { React, SearchableSelect, Text, Toasts, UserStore } from "@webpack/common";
import { Message, User } from "discord-types/general"; import { Message, User } from "discord-types/general";
import settings from "./settings"; import settings from "./settings";
const EditIcon = findByCodeLazy("M19.2929 9.8299L19.9409 9.18278C21.353 7.77064 21.353 5.47197 19.9409");
const DeleteIcon = findByCodeLazy("M15 3.999V2H9V3.999H3V5.999H21V3.999H15Z");
const classNames = findByPropsLazy("customStatusSection"); const classNames = findByPropsLazy("customStatusSection");
import { CogWheel, DeleteIcon } from "@components/Icons";
import { VENCORD_USER_AGENT } from "@shared/vencordUserAgent";
import { makeLazy } from "@utils/lazy"; import { makeLazy } from "@utils/lazy";
import { classes } from "@utils/misc"; import { classes } from "@utils/misc";
import { useForceUpdater } from "@utils/react"; import { useForceUpdater } from "@utils/react";
@ -70,18 +55,19 @@ export default definePlugin({
patches: [ patches: [
{ {
find: "showCommunicationDisabledStyles", find: "copyMetaData:\"User Tag\"",
replacement: { replacement: {
match: /(?<=return\s*\(0,\w{1,3}\.jsxs?\)\(.+!\w{1,3}&&)(\[{0,1}\(0,\w{1,3}.jsxs?\)\(.+?\{.+?\}\)*\]{0,1})/, match: /return(\(0,.\.jsx\)\(.\.default,{className:.+?}\)]}\)}\))/,
replace: "[$1, $self.getTimezonesComponent(e)]" replace: "return [$1, $self.getProfileTimezonesComponent(arguments[0])]"
}, },
}, },
{ {
find: "().customStatusSection", // thank you https://github.com/Syncxv/vc-timezones/blob/master/index.tsx for saving me from painful work
find: ".badgesContainer,",
replacement: { replacement: {
match: /user:(\w),nickname:\w,.*?children.*?\(\)\.customStatusSection.*?\}\),/, match: /id:\(0,\i\.getMessageTimestampId\)\(\i\),timestamp.{1,50}}\),/,
replace: "$&$self.getProfileTimezonesComponent({user:$1})," replace: "$&,$self.getTimezonesComponent(arguments[0]),"
} }
} }
], ],
@ -161,7 +147,7 @@ export default definePlugin({
display: "flex" display: "flex"
}} }}
> >
<EditIcon <CogWheel
style={{ cursor: "pointer", padding: "2px", border: "2px solid grey", borderRadius: "50px" }} style={{ cursor: "pointer", padding: "2px", border: "2px solid grey", borderRadius: "50px" }}
onClick={() => { onClick={() => {
if (!isInEditMode) { if (!isInEditMode) {
@ -236,6 +222,7 @@ export default definePlugin({
}, },
getTimezonesComponent: ({ message }: { message: Message; }) => { getTimezonesComponent: ({ message }: { message: Message; }) => {
const { showTimezonesInChat, preference } = settings.use(["preference", "showTimezonesInChat"]); const { showTimezonesInChat, preference } = settings.use(["preference", "showTimezonesInChat"]);
const [timezone, setTimezone] = React.useState<string | undefined>(); const [timezone, setTimezone] = React.useState<string | undefined>();
@ -250,7 +237,7 @@ export default definePlugin({
return ( return (
<span className={classes(styles.timestampInline, styles.timestamp)}> <span className={classes(styles.timestampInline, styles.timestamp)}>
{timezone && "• " + getTimeString(timezone, message.timestamp.toDate())} {timezone && "• " + getTimeString(timezone, message.timestamp)}
</span>); </span>);
} }
}); });