-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(roleColorEverywhere): make it more fun and easy to pull patc…
…hes from upstream
- Loading branch information
1 parent
a23376e
commit 939594a
Showing
5 changed files
with
112 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Vencord, a Discord client mod | ||
* Copyright (c) 2024 Vendicated and contributors | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
import { ModalProps } from "@utils/modal"; | ||
import { GuildStore, React } from "@webpack/common"; | ||
import { Guild } from "discord-types/general"; | ||
|
||
import { toggleRole } from "../storeHelper"; | ||
import { RoleModalList } from "./RolesView"; | ||
|
||
export function RoleModal({ modalProps, guild, colorsStore }: { modalProps: ModalProps, guild: Guild, colorsStore: Record<string, string[]> }) { | ||
const [ids, setIds] = React.useState(colorsStore[guild.id]); | ||
const roles = React.useMemo(() => ids.map(id => GuildStore.getRole(guild.id, id)), [ids]); | ||
|
||
return <RoleModalList | ||
modalProps={modalProps} | ||
roleList={roles} | ||
header={`${guild.name} highlighted roles.`} | ||
onRoleRemove={id => { | ||
toggleRole(colorsStore, guild.id, id); | ||
setIds(colorsStore[guild.id]); | ||
}} | ||
/>; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Vencord, a Discord client mod | ||
* Copyright (c) 2024 Vendicated and contributors | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
import { GuildStore } from "@webpack/common"; | ||
|
||
// Using plain replaces cause i dont want sanitize regexp | ||
export function toggleRole(colorsStore: ColorsStore, guildId: string, id: string) { | ||
let roles = colorsStore[guildId]; | ||
const len = roles.length; | ||
|
||
roles = roles.filter(e => e !== id); | ||
|
||
if (len === roles.length) { | ||
roles.push(id); | ||
} | ||
|
||
colorsStore[guildId] = roles; | ||
} | ||
|
||
export function atLeastOneOverrideAppliesToGuild(overrides: string[], guildId: string) { | ||
for (const role of overrides) { | ||
if (GuildStore.getRole(guildId, role)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Vencord, a Discord client mod | ||
* Copyright (c) 2024 Vendicated and contributors | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
type ColorsStore = Record<string, string[]>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Vencord, a Discord client mod | ||
* Copyright (c) 2024 Vendicated and contributors | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
import { GuildStore } from "@webpack/common"; | ||
|
||
import { blendColors } from "./blendColors"; | ||
import { atLeastOneOverrideAppliesToGuild } from "./storeHelper"; | ||
|
||
export function brewUserColor(colorsStore: ColorsStore, roles: string[], guildId: string) { | ||
const overrides = colorsStore[guildId]; | ||
if (!overrides?.length) return null; | ||
|
||
if (atLeastOneOverrideAppliesToGuild(overrides, guildId!)) { | ||
const memberRoles = roles.map(role => GuildStore.getRole(guildId!, role)).filter(e => e); | ||
const blendColorsFromRoles = memberRoles | ||
.filter(role => overrides.includes(role.id)) | ||
.sort((a, b) => b.color - a.color); | ||
|
||
// if only one override apply, return the first role color | ||
if (blendColorsFromRoles.length < 2) | ||
return blendColorsFromRoles[0]?.colorString ?? null; | ||
|
||
const color = blendColorsFromRoles | ||
.slice(1) | ||
.reduce( | ||
(p, c) => blendColors(p, c!.colorString!, .5), | ||
blendColorsFromRoles[0].colorString! | ||
); | ||
|
||
return color; | ||
} | ||
|
||
return null; | ||
} |