Save the custom timezone and handle edge cases.

This commit is contained in:
ArjixWasTaken 2023-02-19 15:56:51 +02:00
parent 11a4ca227e
commit 82c1fd45bc
2 changed files with 54 additions and 7 deletions

View file

@ -89,13 +89,12 @@ const bulkFetch = debounce(async () => {
export function getUserTimezone(discordID: string): Promise<typeof timezones[number] | undefined> {
return new Promise(res => {
if (discordID in Cache) res(Cache[discordID]);
const timezone = (DataStore.get(DATASTORE_KEY) as Promise<TimezoneDB | undefined>).then(tzs => tzs?.[discordID]);
timezone.then(tz => {
if (tz) res(tz);
else {
if (discordID in requestQueue) requestQueue[discordID].push(res);
if (discordID in Cache) res(Cache[discordID]);
else if (discordID in requestQueue) requestQueue[discordID].push(res);
// If not already added, then add it and call the debounced function to make sure the request gets executed
else {
requestQueue[discordID] = [res];

View file

@ -182,7 +182,6 @@ export default definePlugin({
],
getProfileTimezonesComponent: (e: any) => {
const user = e.user as User;
const [timezone, setTimezone] = React.useState<string | undefined>();
@ -192,6 +191,7 @@ export default definePlugin({
React.useEffect(() => {
getUserTimezone(user.id).then(timezone => setTimezone(timezone));
// Rerender every second to stay in sync.
setInterval(forceUpdate, 1000);
}, [user.id]);
@ -248,17 +248,65 @@ export default definePlugin({
>
<EditIcon
style={{ cursor: "pointer", padding: "2px", border: "2px solid grey", borderRadius: "50px" }}
onClick={() => { setIsInEditMode(old => !old); }}
onClick={() => {
if (isInEditMode) {
if (timezone) {
DataStore.update(DATASTORE_KEY, (oldValue: TimezoneDB | undefined) => {
oldValue = oldValue || {};
oldValue[user.id] = timezone as typeof timezones[number];
return oldValue;
}).then(() => {
Toasts.show({
type: Toasts.Type.SUCCESS,
message: "Timezone set!",
id: Toasts.genId()
});
setIsInEditMode(false);
}).catch(err => {
console.error(err);
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Something went wrong, please try again later.",
id: Toasts.genId()
});
});
} else {
setIsInEditMode(false);
}
} else {
setIsInEditMode(true);
}
}}
color="var(--primary-330)"
height="16"
width="16"
/>
{isInEditMode &&
<DeleteIcon
style={{ cursor: "pointer", padding: "2px", border: "2px solid grey", borderRadius: "50px" }}
onClick={() => { setTimezone(undefined); setIsInEditMode(false); }}
onClick={() => {
DataStore.update(DATASTORE_KEY, (oldValue: TimezoneDB | undefined) => {
oldValue = oldValue || {};
delete oldValue[user.id];
return oldValue;
}).then(async () => {
Toasts.show({
type: Toasts.Type.SUCCESS,
message: "Timezone removed!",
id: Toasts.genId()
});
setIsInEditMode(false);
setTimezone(await getUserTimezone(user.id));
}).catch(err => {
console.error(err);
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Something went wrong, please try again later.",
id: Toasts.genId()
});
});
}}
color="var(--red-360)"
height="16"
width="16"