From 25b9898c09d357a2560f98f60e57b5722d9b2c85 Mon Sep 17 00:00:00 2001 From: sadan <117494111+sadan4@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:58:13 -0400 Subject: [PATCH] fix *some* horrorcode --- src/plugins/customFolderIcons/index.tsx | 139 ++++++++++++------------ src/plugins/customFolderIcons/style.css | 7 -- 2 files changed, 69 insertions(+), 77 deletions(-) delete mode 100644 src/plugins/customFolderIcons/style.css diff --git a/src/plugins/customFolderIcons/index.tsx b/src/plugins/customFolderIcons/index.tsx index f2d51501a..aacf5d2fb 100644 --- a/src/plugins/customFolderIcons/index.tsx +++ b/src/plugins/customFolderIcons/index.tsx @@ -4,29 +4,26 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -import "./style.css"; - import { DataStore } from "@api/index"; import { Devs } from "@utils/constants"; import { closeModal, ModalContent, ModalHeader, ModalRoot, openModalLazy } from "@utils/modal"; import definePlugin from "@utils/types"; import { Button, Menu, TextInput } from "@webpack/common"; const DATA_STORE_NAME = "CFI_DATA"; -enum ICON_TYPE { - PNG, - SVG -} interface folderIcon{ - type: ICON_TYPE, - url: string + url: string, } -interface config { - [key: string]: folderIcon +interface folderMap { + [key: string]: folderIcon | undefined } -let d: config; +interface folderProp { + folderId: string; + folderColor: number; +} +let d: folderMap; export default definePlugin({ start: async ()=>{ - d = await DataStore.get(DATA_STORE_NAME) || {} as config; + d = await DataStore.get(DATA_STORE_NAME) || {} as folderMap; }, name: "_customFolderIcons", description: "customize folder icons with any png", @@ -37,15 +34,15 @@ export default definePlugin({ { find: ".expandedFolderIconWrapper", replacement: { - match: /(return.{0,80}expandedFolderIconWrapper.*,)\(0,..jsxs\)\(.*]}\)/, - replace: "$1$self.test(e)" + match: /(return.{0,80}expandedFolderIconWrapper.*,)(\(0,..jsxs\)\(.*]}\))/, + replace: "$1$self.shouldReplace(e)?$self.replace(e):$2" } } ], contextMenus: { - "guild-context": (c, a) => { + "guild-context": (c, a: folderProp) => { if(!("folderId" in a)) return; - c.push(makeContextItem(a.folderId)); + c.push(makeContextItem(a)); } }, commands: [ @@ -53,68 +50,52 @@ export default definePlugin({ name: "test", description: "test command for some wack shit", execute: async () => { - console.log("asd"); } } ], - test(e){ - console.log(e); - if(d && e.folderNode.id in d){ - switch(d[e.folderNode.id].type){ - case ICON_TYPE.PNG: - return ( - - ); - case ICON_TYPE.SVG: - return null; - } + shouldReplace(e: any){ + return d && e.folderNode.id in d && d[e.folderNode.id] && d[e.folderNode.id]?.url; + }, + replace(e: any){ + if(d && d[e.folderNode.id]){ + return ( + + ); } - // TODO: when using the default set the color properly - return( -
- -
- ); } }); -const setFolderUrl = async (id: string, url: string) => { - console.log(id, url); - DataStore.get(DATA_STORE_NAME).then(v => { - if(v){ - v[id] = { - type: ICON_TYPE.PNG, - url: url - }; - DataStore.set(DATA_STORE_NAME, v).then(() => { d = v; }).catch(e => { - handleUpdateError(e); - }); - }else{ - v = {}; - v[id] = { - type: ICON_TYPE.PNG, - url: url - }; - DataStore.set(DATA_STORE_NAME, v).then(() => { d = v; }).catch(e => { - handleUpdateError(e); - }); - } +/** + * @param i RGB value + * @param a alpha bewteen zero and 1 + */ +const int2rgba = (i: number, a: number = 1)=>{ + const b = i & 0xFF, + g = (i & 0xFF00) >>> 8, + r = (i & 0xFF0000) >>> 16; + return `rgba(${[r,g,b].join(",")},${a})`; +}; +const setFolderUrl = async (a: folderProp, url: string) => { + DataStore.get(DATA_STORE_NAME).then(v => { + v = v ?? {} as folderMap; + v[a.folderId] = { + // type: url.endsWith("svg") ? ICON_TYPE.SVG:ICON_TYPE.PNG, + url: url, + }; + DataStore.set(DATA_STORE_NAME, v).then(() => { d = v; }).catch(e => { + handleUpdateError(e); + }); } ) .catch(e => { handleUpdateError(e); }); }; -function ImageModal(a: { folderId: string }){ + +function ImageModal(folderData: folderProp){ let v = ""; return( <> @@ -126,23 +107,34 @@ function ImageModal(a: { folderId: string }){ > +
+ +
); } -function makeContextItem(id: string) { +function makeContextItem(a: folderProp) { return ( { - console.log("menu clicked"); openModalLazy(async () => { return props => ( @@ -154,8 +146,15 @@ function makeContextItem(id: string) { - + +
+ You might have to hover the folder after setting in order for it to refresh. +
); }, diff --git a/src/plugins/customFolderIcons/style.css b/src/plugins/customFolderIcons/style.css deleted file mode 100644 index 26400f314..000000000 --- a/src/plugins/customFolderIcons/style.css +++ /dev/null @@ -1,7 +0,0 @@ -.customFolderDefaultIcon{ - display: flex; - justify-content: center; - align-items: center; - width: var(--custom-folder-item-guild-icon-size); - height: var(--custom-folder-item-guild-icon-size); -}