Skip to content

Commit

Permalink
Add 'Lookup Server Nicknames & Roles' and 'Lookup Display Names' sett…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
prathercc committed May 14, 2024
1 parent 17aeea4 commit be07d58
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
69 changes: 48 additions & 21 deletions src/containers/discrub-dialog/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { setSetting } from "../../../services/chrome-service";
import { AppSettings } from "../../../features/app/app-types";
import { DiscrubSetting } from "../../../enum/discrub-setting";
import Tooltip from "../../../common-components/tooltip/tooltip";

type SettingsProps = {
settings: AppSettings;
Expand All @@ -34,6 +35,28 @@ function Settings({ settings, onChangeSettings }: SettingsProps) {
{ value: "true", name: "Yes" },
{ value: "false", name: "No" },
],
description:
"This setting dictates if the extension will lookup reactions for messages.",
},
{
name: DiscrubSetting.SERVER_NICKNAME_LOOKUP,
label: "Lookup Server Nicknames & Roles",
options: [
{ value: "true", name: "Yes" },
{ value: "false", name: "No" },
],
description:
"This setting determines if a User's server nickname and roles will be looked up during message allocation.",
},
{
name: DiscrubSetting.DISPLAY_NAME_LOOKUP,
label: "Lookup Display Names",
options: [
{ value: "true", name: "Yes" },
{ value: "false", name: "No" },
],
description:
"Having this setting set to 'Yes' will ensure that User mentions are correctly displayed, even if the mentioned User is not apart of the conversation. It is recommended to keep this setting enabled.",
},
];

Expand All @@ -50,30 +73,34 @@ function Settings({ settings, onChangeSettings }: SettingsProps) {
</Stack>

<Stack sx={{ maxHeight: "600px", overflow: "auto" }}>
<Stack sx={{ padding: 3, spacing: 2 }}>
<Stack sx={{ padding: 3, spacing: 2, gap: "15px" }}>
{Object.keys(settings).length &&
controls.map((control) => {
return (
<FormControl fullWidth>
<InputLabel>{control.label}</InputLabel>
<Select
value={getValue(control.name)}
label={control.label}
onChange={(e) => {
if (
e.target.value !== null &&
e.target.value !== undefined
)
handleChange(control.name, e.target.value);
}}
>
{control.options.map((option) => {
return (
<MenuItem value={option.value}>{option.name}</MenuItem>
);
})}
</Select>
</FormControl>
<Tooltip title={control.description}>
<FormControl fullWidth>
<InputLabel>{control.label}</InputLabel>
<Select
value={getValue(control.name)}
label={control.label}
onChange={(e) => {
if (
e.target.value !== null &&
e.target.value !== undefined
)
handleChange(control.name, e.target.value);
}}
>
{control.options.map((option) => {
return (
<MenuItem value={option.value}>
{option.name}
</MenuItem>
);
})}
</Select>
</FormControl>
</Tooltip>
);
})}
</Stack>
Expand Down
2 changes: 2 additions & 0 deletions src/enum/discrub-setting.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export enum DiscrubSetting {
REACTIONS_ENABLED = "reactionsEnabled",
SERVER_NICKNAME_LOOKUP = "serverNickNameLookup",
DISPLAY_NAME_LOOKUP = "displayNameLookup",
}
2 changes: 2 additions & 0 deletions src/features/app/app-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { DiscrubSetting } from "../../enum/discrub-setting";

const defaultSettings: AppSettings = {
[DiscrubSetting.REACTIONS_ENABLED]: "false",
[DiscrubSetting.SERVER_NICKNAME_LOOKUP]: "false",
[DiscrubSetting.DISPLAY_NAME_LOOKUP]: "false",
};

const emptyTask: AppTask = {
Expand Down
9 changes: 6 additions & 3 deletions src/features/message/message-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,11 +1025,13 @@ const _getUserMap =
const _collectUserNames =
(userMap: ExportUserMap): AppThunk<Promise<void>> =>
async (dispatch, getState) => {
const { settings } = getState().app;
const { displayNameLookup } = settings;
const { token } = getState().user;
const { userMap: existingUserMap } = getState().export.exportMaps;
const updateMap = { ...userMap };

if (token) {
if (token && stringToBool(displayNameLookup)) {
const userIds = Object.keys(updateMap);
for (const [i, userId] of userIds.entries()) {
if (getState().app.discrubCancelled) break;
Expand Down Expand Up @@ -1065,11 +1067,13 @@ const _collectUserNames =
const _collectUserGuildData =
(userMap: ExportUserMap, guildId: Snowflake): AppThunk<Promise<void>> =>
async (dispatch, getState) => {
const { settings } = getState().app;
const { serverNickNameLookup } = settings;
const { token } = getState().user;
const { userMap: existingUserMap } = getState().export.exportMaps;
const updateMap = { ...userMap };

if (token) {
if (token && stringToBool(serverNickNameLookup)) {
const userIds = Object.keys(updateMap);
for (const [i, userId] of userIds.entries()) {
if (getState().app.discrubCancelled) break;
Expand Down Expand Up @@ -1114,7 +1118,6 @@ const _collectUserGuildData =
}
}
}

dispatch(resetStatus());
dispatch(setExportUserMap({ ...existingUserMap, ...updateMap }));
}
Expand Down
11 changes: 10 additions & 1 deletion src/services/chrome-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const sendChromeMessage = (msg: string, callback?: ChromeCallback) => {

const defaultSettings = [
{ name: DiscrubSetting.REACTIONS_ENABLED, value: "true" },
{ name: DiscrubSetting.SERVER_NICKNAME_LOOKUP, value: "true" },
{ name: DiscrubSetting.DISPLAY_NAME_LOOKUP, value: "true" },
];

export const initializeSettings = async () => {
Expand All @@ -40,7 +42,14 @@ export const getSettings = async (): Promise<AppSettings> => {
defaultSettings.map((setting) => setting.name)
);

return { reactionsEnabled: chromeSettings[DiscrubSetting.REACTIONS_ENABLED] };
return {
[DiscrubSetting.REACTIONS_ENABLED]:
chromeSettings[DiscrubSetting.REACTIONS_ENABLED],
[DiscrubSetting.SERVER_NICKNAME_LOOKUP]:
chromeSettings[DiscrubSetting.SERVER_NICKNAME_LOOKUP],
[DiscrubSetting.DISPLAY_NAME_LOOKUP]:
chromeSettings[DiscrubSetting.DISPLAY_NAME_LOOKUP],
};
};

export const setSetting = async (name: string, value: string) => {
Expand Down

0 comments on commit be07d58

Please sign in to comment.