Timezones: If DB Fails Use Local Timezone

This commit is contained in:
thororen1234 2025-05-28 09:45:37 -04:00
parent f99327caa1
commit c9ec915dcc
No known key found for this signature in database
3 changed files with 19 additions and 16 deletions

View file

@ -29,10 +29,10 @@ export function SetTimezoneModal({ userId, modalProps, database }: { userId: str
const localTimezone = timezones[userId]; const localTimezone = timezones[userId];
const shouldUseDatabase = const shouldUseDatabase =
settings.store.useDatabase && settings.store.useDatabase &&
(settings.store.preferDatabaseOverLocal || localTimezone == null); (settings.store.preferDatabaseOverLocal || !localTimezone);
if (shouldUseDatabase) { if (shouldUseDatabase) {
getTimezone(userId).then(setCurrentValue); getTimezone(userId).then(e => setCurrentValue(e ?? localTimezone));
} else { } else {
setCurrentValue(localTimezone); setCurrentValue(localTimezone);
} }

View file

@ -4,24 +4,17 @@
* SPDX-License-Identifier: GPL-3.0-or-later * SPDX-License-Identifier: GPL-3.0-or-later
*/ */
type CacheEntry = {
value: string | null;
expires: number;
};
import { DataStore } from "@api/index"; import { DataStore } from "@api/index";
import { openModal } from "@utils/modal"; import { openModal } from "@utils/modal";
import { OAuth2AuthorizeModal, showToast, Toasts } from "@webpack/common"; import { OAuth2AuthorizeModal, showToast, Toasts } from "@webpack/common";
import { databaseTimezones } from ".";
export const DOMAIN = "https://timezone.creations.works"; export const DOMAIN = "https://timezone.creations.works";
export const REDIRECT_URI = `${DOMAIN}/auth/discord/callback`; export const REDIRECT_URI = `${DOMAIN}/auth/discord/callback`;
export const CLIENT_ID = "1377021506810417173"; export const CLIENT_ID = "1377021506810417173";
export const DATASTORE_KEY = "vencord-database-timezones"; export const DATASTORE_KEY = "vencord-database-timezones";
export let databaseTimezones: Record<string, CacheEntry> = {};
(async () => {
databaseTimezones = await DataStore.get<Record<string, CacheEntry>>(DATASTORE_KEY) || {};
})();
const pendingRequests: Record<string, Promise<string | null>> = {}; const pendingRequests: Record<string, Promise<string | null>> = {};

View file

@ -22,10 +22,14 @@ import { SetTimezoneModal } from "./TimezoneModal";
export const DATASTORE_KEY = "vencord-timezones"; export const DATASTORE_KEY = "vencord-timezones";
type CacheEntry = {
value: string | null;
expires: number;
};
export let databaseTimezones: Record<string, CacheEntry> = {};
export let timezones: Record<string, string | null> = {}; export let timezones: Record<string, string | null> = {};
(async () => {
timezones = await DataStore.get<Record<string, string>>(DATASTORE_KEY) || {};
})();
const classes = findByPropsLazy("timestamp", "compact", "contentOnly"); const classes = findByPropsLazy("timestamp", "compact", "contentOnly");
const locale = findByPropsLazy("getLocale"); const locale = findByPropsLazy("getLocale");
@ -118,10 +122,10 @@ const TimestampComponent = ErrorBoundary.wrap(({ userId, timestamp, type }: Prop
const localTimezone = timezones[userId]; const localTimezone = timezones[userId];
const shouldUseDatabase = const shouldUseDatabase =
settings.store.useDatabase && settings.store.useDatabase &&
(settings.store.preferDatabaseOverLocal || localTimezone == null); (settings.store.preferDatabaseOverLocal || !localTimezone);
if (shouldUseDatabase) { if (shouldUseDatabase) {
getTimezone(userId).then(setTimezone); getTimezone(userId).then(e => setTimezone(e ?? localTimezone));
} else { } else {
setTimezone(localTimezone); setTimezone(localTimezone);
} }
@ -240,6 +244,12 @@ export default definePlugin({
} }
} }
], ],
async start() {
databaseTimezones = await DataStore.get<Record<string, CacheEntry>>(DATASTORE_KEY) || {};
timezones = await DataStore.get<Record<string, string>>(DATASTORE_KEY) || {};
},
settings, settings,
getTime, getTime,