mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-07 13:43:03 -04:00
move to a list on load instead of each reqest, move refresh and set database to toolbox
This commit is contained in:
parent
196d1f3b80
commit
c7ee1bb0bf
3 changed files with 66 additions and 96 deletions
|
@ -24,19 +24,14 @@ export function SetTimezoneModal({ userId, modalProps, database }: { userId: str
|
|||
const [currentValue, setCurrentValue] = useState<string | null>(timezones[userId] ?? null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!database) return;
|
||||
|
||||
const localTimezone = timezones[userId];
|
||||
const shouldUseDatabase =
|
||||
settings.store.useDatabase &&
|
||||
(settings.store.preferDatabaseOverLocal || !localTimezone);
|
||||
|
||||
if (shouldUseDatabase) {
|
||||
getTimezone(userId).then(e => setCurrentValue(e ?? localTimezone));
|
||||
} else {
|
||||
setCurrentValue(localTimezone);
|
||||
}
|
||||
}, [userId, settings.store.useDatabase, settings.store.preferDatabaseOverLocal, database]);
|
||||
setCurrentValue(shouldUseDatabase ? getTimezone(userId) ?? localTimezone : localTimezone);
|
||||
}, [userId, settings.store.useDatabase, settings.store.preferDatabaseOverLocal]);
|
||||
|
||||
|
||||
const options = useMemo(() => {
|
||||
return Intl.supportedValuesOf("timeZone").map(timezone => {
|
||||
|
|
|
@ -4,55 +4,45 @@
|
|||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { DataStore } from "@api/index";
|
||||
import { openModal } from "@utils/modal";
|
||||
import { openModal } from "@utils/index";
|
||||
import { OAuth2AuthorizeModal, showToast, Toasts } from "@webpack/common";
|
||||
|
||||
import { databaseTimezones } from ".";
|
||||
const databaseTimezones: Record<string, { value: string | null; }> = {};
|
||||
|
||||
export const DOMAIN = "https://timezone.creations.works";
|
||||
export const REDIRECT_URI = `${DOMAIN}/auth/discord/callback`;
|
||||
export const CLIENT_ID = "1377021506810417173";
|
||||
|
||||
export const DATASTORE_KEY = "vencord-database-timezones";
|
||||
|
||||
const pendingRequests: Record<string, Promise<string | null>> = {};
|
||||
const DOMAIN = "https://timezone.creations.works";
|
||||
const REDIRECT_URI = `${DOMAIN}/auth/discord/callback`;
|
||||
const CLIENT_ID = "1377021506810417173";
|
||||
|
||||
export async function setUserDatabaseTimezone(userId: string, timezone: string | null) {
|
||||
databaseTimezones[userId] = {
|
||||
value: timezone,
|
||||
expires: Date.now() + 60 * 60 * 1000 // 1 hour
|
||||
};
|
||||
await DataStore.set(DATASTORE_KEY, databaseTimezones);
|
||||
databaseTimezones[userId] = { value: timezone };
|
||||
}
|
||||
|
||||
export async function getTimezone(userId: string, force?: boolean): Promise<string | null> {
|
||||
const now = Date.now();
|
||||
export function getTimezone(userId: string): string | null {
|
||||
return databaseTimezones[userId]?.value ?? null;
|
||||
}
|
||||
|
||||
const cached = databaseTimezones[userId];
|
||||
if (cached && now < cached.expires && !force) return cached.value;
|
||||
export async function loadDatabaseTimezones(): Promise<boolean> {
|
||||
try {
|
||||
const res = await fetch(`${DOMAIN}/list`, {
|
||||
headers: { Accept: "application/json" }
|
||||
});
|
||||
|
||||
if (!pendingRequests[userId]) {
|
||||
pendingRequests[userId] = (async () => {
|
||||
const res = await fetch(`${DOMAIN}/get?id=${userId}`, {
|
||||
headers: { Accept: "application/json" }
|
||||
});
|
||||
|
||||
let value: string | null = null;
|
||||
if (res.ok) {
|
||||
const json = await res.json();
|
||||
if (json?.timezone && typeof json.timezone === "string") {
|
||||
value = json.timezone;
|
||||
}
|
||||
if (res.ok) {
|
||||
const json = await res.json();
|
||||
for (const id in json) {
|
||||
databaseTimezones[id] = {
|
||||
value: json[id]?.timezone ?? null
|
||||
};
|
||||
}
|
||||
|
||||
setUserDatabaseTimezone(userId, value);
|
||||
delete pendingRequests[userId];
|
||||
return value;
|
||||
})();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return pendingRequests[userId];
|
||||
return false;
|
||||
} catch (e) {
|
||||
console.error("Failed to fetch timezones list:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function setTimezone(timezone: string): Promise<boolean> {
|
||||
|
@ -115,4 +105,3 @@ export function authModal(callback?: () => void) {
|
|||
/>
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* Vencord, a Discord client mod
|
||||
* Copyright (c) 2024 Vendicated and contributors
|
||||
* Copyright (c) 2025 Vendicated and contributors
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
|
@ -17,15 +17,9 @@ import { findByPropsLazy } from "@webpack";
|
|||
import { Button, Menu, showToast, Toasts, Tooltip, useEffect, UserStore, useState } from "@webpack/common";
|
||||
import { Message, User } from "discord-types/general";
|
||||
|
||||
import { authModal, deleteTimezone, getTimezone, setUserDatabaseTimezone } from "./database";
|
||||
import { authModal, deleteTimezone, getTimezone, loadDatabaseTimezones, setUserDatabaseTimezone } from "./database";
|
||||
import { SetTimezoneModal } from "./TimezoneModal";
|
||||
|
||||
type CacheEntry = {
|
||||
value: string | null;
|
||||
expires: number;
|
||||
};
|
||||
|
||||
export let databaseTimezones: Record<string, CacheEntry> = {};
|
||||
export let timezones: Record<string, string | null> = {};
|
||||
export const DATASTORE_KEY = "vencord-timezones";
|
||||
|
||||
|
@ -70,8 +64,7 @@ export const settings = definePluginSettings({
|
|||
<Button onClick={() => {
|
||||
authModal(async () => {
|
||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||
}
|
||||
);
|
||||
});
|
||||
}}>
|
||||
Set Timezone on Database
|
||||
</Button>
|
||||
|
@ -112,6 +105,7 @@ interface Props {
|
|||
timestamp?: string;
|
||||
type: "message" | "profile";
|
||||
}
|
||||
|
||||
const TimestampComponent = ErrorBoundary.wrap(({ userId, timestamp, type }: Props) => {
|
||||
const [currentTime, setCurrentTime] = useState(timestamp || Date.now());
|
||||
const [timezone, setTimezone] = useState<string | null>(null);
|
||||
|
@ -123,7 +117,7 @@ const TimestampComponent = ErrorBoundary.wrap(({ userId, timestamp, type }: Prop
|
|||
(settings.store.preferDatabaseOverLocal || !localTimezone);
|
||||
|
||||
if (shouldUseDatabase) {
|
||||
getTimezone(userId).then(e => setTimezone(e ?? localTimezone));
|
||||
setTimezone(getTimezone(userId) ?? localTimezone);
|
||||
} else {
|
||||
setTimezone(localTimezone);
|
||||
}
|
||||
|
@ -178,45 +172,19 @@ const TimestampComponent = ErrorBoundary.wrap(({ userId, timestamp, type }: Prop
|
|||
);
|
||||
}, { noop: true });
|
||||
|
||||
|
||||
const userContextMenuPatch: NavContextMenuPatchCallback = (children, { user }: { user: User; }) => {
|
||||
if (user?.id == null) return;
|
||||
|
||||
const label = settings.store.useDatabase ? "Set Local Timezone" : "Set Timezone";
|
||||
const setTimezoneItem = (
|
||||
<Menu.MenuItem
|
||||
label="Set Timezone"
|
||||
label={label}
|
||||
id="set-timezone"
|
||||
action={() => openModal(modalProps => <SetTimezoneModal userId={user.id} modalProps={modalProps} />)}
|
||||
/>
|
||||
);
|
||||
|
||||
children.push(<Menu.MenuSeparator />, setTimezoneItem);
|
||||
|
||||
if (settings.store.useDatabase) {
|
||||
const refreshTimezoneItem = (
|
||||
<Menu.MenuItem
|
||||
label="Refresh Timezone"
|
||||
id="refresh-timezone"
|
||||
action={async () => {
|
||||
showToast("Refreshing timezone...", Toasts.Type.CLOCK);
|
||||
|
||||
try {
|
||||
const timezone = await getTimezone(user.id, true);
|
||||
|
||||
if (timezone) {
|
||||
showToast("Timezone refreshed successfully!", Toasts.Type.SUCCESS);
|
||||
} else {
|
||||
showToast("Timezone reset successfully!", Toasts.Type.SUCCESS);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to refresh timezone:", error);
|
||||
showToast("Failed to refresh timezone.", Toasts.Type.FAILURE);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
);
|
||||
children.push(refreshTimezoneItem);
|
||||
}
|
||||
};
|
||||
|
||||
export default definePlugin({
|
||||
|
@ -246,31 +214,49 @@ export default definePlugin({
|
|||
}
|
||||
],
|
||||
|
||||
toolboxActions: {
|
||||
"Set Database Timezone": () => {
|
||||
authModal(async () => {
|
||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||
});
|
||||
},
|
||||
"Refresh Database Timezones": async () => {
|
||||
try {
|
||||
const good = await loadDatabaseTimezones();
|
||||
|
||||
if (good) {
|
||||
showToast("Timezones refreshed successfully!", Toasts.Type.SUCCESS);
|
||||
} else {
|
||||
showToast("Timezones Failed to refresh!", Toasts.Type.FAILURE);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Failed to refresh timezone:", error);
|
||||
showToast("Failed to refresh timezones.", Toasts.Type.FAILURE);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async start() {
|
||||
databaseTimezones = await DataStore.get<Record<string, CacheEntry>>(DATASTORE_KEY) || {};
|
||||
timezones = await DataStore.get<Record<string, string>>(DATASTORE_KEY) || {};
|
||||
|
||||
if (settings.store.useDatabase) {
|
||||
await loadDatabaseTimezones();
|
||||
}
|
||||
},
|
||||
|
||||
settings,
|
||||
getTime,
|
||||
|
||||
|
||||
renderProfileTimezone: (props?: { user?: User; }) => {
|
||||
if (!settings.store.showProfileTime || !props?.user?.id) return null;
|
||||
|
||||
return <TimestampComponent
|
||||
userId={props.user.id}
|
||||
type="profile"
|
||||
/>;
|
||||
return <TimestampComponent userId={props.user.id} type="profile" />;
|
||||
},
|
||||
|
||||
renderMessageTimezone: (props?: { message?: Message; }) => {
|
||||
if (!settings.store.showMessageHeaderTime || !props?.message) return null;
|
||||
|
||||
return <TimestampComponent
|
||||
userId={props.message.author.id}
|
||||
timestamp={props.message.timestamp.toISOString()}
|
||||
type="message"
|
||||
/>;
|
||||
return <TimestampComponent userId={props.message.author.id} timestamp={props.message.timestamp.toISOString()} type="message" />;
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue