mirror of
https://github.com/Vendicated/Vencord.git
synced 2025-02-24 15:35:11 +00:00
remove useless code
This commit is contained in:
parent
25dd64dd11
commit
0a96e1a508
1 changed files with 99 additions and 186 deletions
|
@ -46,8 +46,6 @@ const SearchHandler = findByCodeLazy("createSearchContext", "setLimit");
|
|||
|
||||
const convertItem = findByCodeLazy("GROUP_DM:return{", "GUILD_VOICE:case");
|
||||
const loadFrecency = findByCodeLazy(".frecencyWithoutFetchingLatest)");
|
||||
const navigatorWrapper = findByCodeLazy("useMemo(()=>({onKeyDown:");
|
||||
const createNavigator = findByCodeLazy(".keyboardModeEnabled)", "useCallback(()=>new Promise(", "Number.MAX_SAFE_INTEGER");
|
||||
const getChannelLabel = findByCodeLazy("recipients.map(", "getNickname(");
|
||||
|
||||
const ChannelIcon = findComponentByCodeLazy("channelGuildIcon,");
|
||||
|
@ -158,27 +156,6 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
searchType?: ("USERS" | "CHANNELS" | "GUILDS")[] | "USERS" | "CHANNELS" | "GUILDS" | "ALL";
|
||||
subText?: string
|
||||
}) {
|
||||
|
||||
const callbacks = new Map();
|
||||
|
||||
function registerCallback(key: string, callback: (...args: any[]) => void): () => void {
|
||||
let currentCallbacks = callbacks.get(key);
|
||||
if (!currentCallbacks) {
|
||||
currentCallbacks = new Set();
|
||||
callbacks.set(key, currentCallbacks);
|
||||
}
|
||||
|
||||
currentCallbacks.add(callback);
|
||||
|
||||
return () => {
|
||||
currentCallbacks.delete(callback);
|
||||
if (currentCallbacks.size === 0) {
|
||||
callbacks.delete(key);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
const UserIcon = React.memo(function ({
|
||||
user,
|
||||
size = SearchBarModule.AvatarSizes.SIZE_32,
|
||||
|
@ -204,8 +181,6 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
|
||||
const [selected, setSelected] = useState<DestinationItem[]>([]);
|
||||
|
||||
const refCounter = useRef(0);
|
||||
|
||||
const Row = (props: SpecificRowProps) => {
|
||||
const {
|
||||
destination,
|
||||
|
@ -219,8 +194,11 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
...rest
|
||||
} = props;
|
||||
|
||||
const interactionProps = generateRowData(destination.id);
|
||||
|
||||
const interactionProps = {
|
||||
role: "listitem",
|
||||
"data-list-item-id": `NO_LIST___${destination.id}`,
|
||||
tabIndex: -1,
|
||||
};
|
||||
const handlePress = useCallback(() => {
|
||||
onPressDestination?.(destination);
|
||||
}, [onPressDestination, destination]);
|
||||
|
@ -228,7 +206,7 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
return (
|
||||
<SearchBarModule.Clickable
|
||||
className={cl("destination-row")}
|
||||
onClick={e => { e.stopPropagation(); e.preventDefault(); handlePress(); }}
|
||||
onClick={handlePress}
|
||||
aria-selected={isSelected}
|
||||
{...interactionProps}
|
||||
{...rest}
|
||||
|
@ -250,6 +228,8 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
</div>
|
||||
</div>
|
||||
<SearchBarModule.Checkbox
|
||||
onClick={e => console.log(e)}
|
||||
onChange={e => console.log(e)}
|
||||
type={SearchBarModule.Checkbox.Types.INVERTED}
|
||||
displayOnly={true}
|
||||
size={24}
|
||||
|
@ -375,138 +355,9 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
);
|
||||
}
|
||||
|
||||
const navigatorContext = React.createContext({
|
||||
id: "NO_LIST",
|
||||
onKeyDown() {
|
||||
},
|
||||
orientation: "vertical",
|
||||
ref: React.createRef(),
|
||||
tabIndex: -1
|
||||
});
|
||||
|
||||
function generateNavigatorData() {
|
||||
const { id: id, onKeyDown, ref, tabIndex } = React.useContext(navigatorContext);
|
||||
return {
|
||||
role: "list",
|
||||
tabIndex,
|
||||
"data-list-id": id,
|
||||
onKeyDown: onKeyDown,
|
||||
ref: ref,
|
||||
};
|
||||
}
|
||||
|
||||
function navigatorData(e: { children: (data: ReturnType<typeof generateNavigatorData>) => React.ReactNode }): React.ReactNode {
|
||||
const { children } = e;
|
||||
return children(generateNavigatorData());
|
||||
}
|
||||
|
||||
|
||||
function generateRowData(rowId: string) {
|
||||
const [tabIndex, setTabIndex] = useState(-1);
|
||||
const id = "NO_LIST";
|
||||
React.useLayoutEffect(() => {
|
||||
return registerCallback(id, (tabIndex: string, id: string) => {
|
||||
setTabIndex(id && tabIndex === rowId ? 0 : -1);
|
||||
});
|
||||
}, [rowId, id]);
|
||||
|
||||
return {
|
||||
role: "listitem",
|
||||
"data-list-item-id": `${id}___${rowId}`,
|
||||
tabIndex,
|
||||
};
|
||||
}
|
||||
|
||||
const [searchText, setSearchText] = useState<string>(input || "");
|
||||
const ref = {};
|
||||
|
||||
function getSearchHandler(searchOptions: Record<string, any>): { search: (e: { query: string, resultTypes: string[] }) => void, results: Result[], query: string } {
|
||||
const [results, setResults] = useState<{ results: Result[], query: string }>({
|
||||
results: [],
|
||||
query: ""
|
||||
});
|
||||
|
||||
function getRef<T>(e: () => T): T {
|
||||
const ref_ = useRef<T>(ref as T);
|
||||
if (ref_.current === ref)
|
||||
ref_.current = e();
|
||||
return ref_.current;
|
||||
}
|
||||
|
||||
const searchHandler: InstanceType<typeof SearchHandler> = getRef(() => {
|
||||
const searchHandler = new SearchHandler((r: Result[], q: string) => {
|
||||
setResults({
|
||||
results: r,
|
||||
query: q
|
||||
});
|
||||
}
|
||||
);
|
||||
searchHandler.setLimit(20);
|
||||
searchHandler.search("");
|
||||
return searchHandler;
|
||||
}
|
||||
);
|
||||
React.useEffect(() => () => searchHandler.destroy(), [searchHandler]);
|
||||
React.useEffect(() => {
|
||||
searchOptions != null && searchOptions !== searchHandler.options && searchHandler.setOptions(searchOptions);
|
||||
}, [searchHandler, searchOptions]
|
||||
);
|
||||
return {
|
||||
search: useCallback(e => {
|
||||
const { query, resultTypes } = e;
|
||||
if (searchHandler.resultTypes == null || !(resultTypes.length === searchHandler.resultTypes.size && resultTypes.every(e => searchHandler.resultTypes.has(e)))) {
|
||||
searchHandler.setResultTypes(resultTypes);
|
||||
searchHandler.setLimit(resultTypes.length === 1 ? 50 : 20);
|
||||
}
|
||||
searchHandler.search(query.trim() === "" ? "" : query);
|
||||
}
|
||||
, [searchHandler]),
|
||||
...results
|
||||
};
|
||||
}
|
||||
|
||||
function generateResults({ selectedDestinations }: { selectedDestinations: DestinationItem[] }) {
|
||||
const { search, query, results } = getSearchHandler({
|
||||
blacklist: null,
|
||||
frecencyBoosters: !0,
|
||||
userFilters: null
|
||||
});
|
||||
|
||||
const [queryData, setQueryData] = useState("");
|
||||
|
||||
const updateSearch = useCallback((e: string) => setQueryData(e), [setQueryData]);
|
||||
|
||||
if (queryData === "" && searchText !== "") {
|
||||
updateSearch(searchText);
|
||||
}
|
||||
|
||||
const [pinned, setPinned] = useState(selectedDestinations != null ? selectedDestinations : []);
|
||||
React.useLayoutEffect(() => {
|
||||
search({
|
||||
query: queryData,
|
||||
resultTypes: resultTypes,
|
||||
});
|
||||
setPinned(selectedDestinations != null ? selectedDestinations : []);
|
||||
}
|
||||
, [search, queryData]);
|
||||
|
||||
loadFrecency();
|
||||
|
||||
const frequentChannels: Channel[] = useStateFromStores([FrecencyStore], () => FrecencyStore.getFrequentlyWithoutFetchingLatest());
|
||||
const channelHistory: string[] = useStateFromStores([QuickSwitcherStore], () => QuickSwitcherStore.getChannelHistory());
|
||||
const guilds: GuildResult[] = useStateFromStores([GuildStore], () => Object.values(GuildStore.getGuilds()).map(
|
||||
guild => {
|
||||
return {
|
||||
type: TextTypes.GUILD,
|
||||
record: guild,
|
||||
score: 0,
|
||||
comparator: guild.name
|
||||
};
|
||||
}
|
||||
));
|
||||
|
||||
const hasQuery = query !== "";
|
||||
|
||||
function getItem(e: DestinationItem): Result {
|
||||
if (e.type === "guild") {
|
||||
const guild = GuildStore.getGuild(e.id);
|
||||
|
@ -541,7 +392,6 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
results: Result[];
|
||||
hasQuery: boolean;
|
||||
frequentChannels: Channel[];
|
||||
pinnedDestinations: DestinationItem[];
|
||||
channelHistory: string[];
|
||||
guilds: GuildResult[]
|
||||
}): Result[] {
|
||||
|
@ -558,7 +408,7 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
return clean;
|
||||
};
|
||||
|
||||
const { results, hasQuery, frequentChannels, pinnedDestinations, channelHistory, guilds } = props;
|
||||
const { results, hasQuery, frequentChannels, channelHistory, guilds } = props;
|
||||
if (hasQuery) return filterItems(results);
|
||||
|
||||
const recentDestinations = filterItems([
|
||||
|
@ -568,27 +418,108 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
]);
|
||||
|
||||
return removeDuplicates(
|
||||
[...(pinnedDestinations.length > 0 ? pinnedDestinations.map(e => getItem(e)) : []),
|
||||
[...(selected.length > 0 ? selected.map(e => getItem(e)) : []),
|
||||
...recentDestinations
|
||||
]);
|
||||
}
|
||||
|
||||
function getRef<T>(e: () => T): T {
|
||||
const ref_ = useRef<T>(ref as T);
|
||||
if (ref_.current === ref)
|
||||
ref_.current = e();
|
||||
return ref_.current;
|
||||
}
|
||||
|
||||
function getSearchHandler(searchOptions: Record<string, any>): { search: (e: { query: string, resultTypes: string[] }) => void, results: Result[], query: string } {
|
||||
const [results, setResults] = useState<{ results: Result[], query: string }>({
|
||||
results: [],
|
||||
query: ""
|
||||
});
|
||||
|
||||
const searchHandler: InstanceType<typeof SearchHandler> = getRef(() => {
|
||||
const searchHandler = new SearchHandler((r: Result[], q: string) => {
|
||||
setResults({
|
||||
results: r,
|
||||
query: q
|
||||
});
|
||||
}
|
||||
);
|
||||
searchHandler.setLimit(20);
|
||||
searchHandler.search("");
|
||||
return searchHandler;
|
||||
}
|
||||
);
|
||||
React.useEffect(() => () => searchHandler.destroy(), [searchHandler]);
|
||||
React.useEffect(() => {
|
||||
searchOptions != null && searchOptions !== searchHandler.options && searchHandler.setOptions(searchOptions);
|
||||
}, [searchHandler, searchOptions]
|
||||
);
|
||||
return {
|
||||
search: useCallback(e => {
|
||||
const { query, resultTypes } = e;
|
||||
if (searchHandler.resultTypes == null || !(resultTypes.length === searchHandler.resultTypes.size && resultTypes.every(e => searchHandler.resultTypes.has(e)))) {
|
||||
searchHandler.setResultTypes(resultTypes);
|
||||
searchHandler.setLimit(resultTypes.length === 1 ? 50 : 20);
|
||||
}
|
||||
searchHandler.search(query.trim() === "" ? "" : query);
|
||||
}
|
||||
, [searchHandler]),
|
||||
...results
|
||||
};
|
||||
}
|
||||
|
||||
function generateResults() {
|
||||
const { search, query, results } = getSearchHandler({
|
||||
blacklist: null,
|
||||
frecencyBoosters: !0,
|
||||
userFilters: null
|
||||
});
|
||||
|
||||
const [queryData, setQueryData] = useState("");
|
||||
|
||||
const updateSearch = useCallback((e: string) => setQueryData(e), [setQueryData]);
|
||||
|
||||
if (queryData === "" && searchText !== "") {
|
||||
updateSearch(searchText);
|
||||
}
|
||||
|
||||
React.useLayoutEffect(() => {
|
||||
search({
|
||||
query: queryData,
|
||||
resultTypes: resultTypes,
|
||||
});
|
||||
}, [search, queryData]);
|
||||
|
||||
loadFrecency();
|
||||
|
||||
const frequentChannels: Channel[] = useStateFromStores([FrecencyStore], () => FrecencyStore.getFrequentlyWithoutFetchingLatest());
|
||||
const channelHistory: string[] = useStateFromStores([QuickSwitcherStore], () => QuickSwitcherStore.getChannelHistory());
|
||||
const guilds: GuildResult[] = useStateFromStores([GuildStore], () => Object.values(GuildStore.getGuilds()).map(
|
||||
guild => {
|
||||
return {
|
||||
type: TextTypes.GUILD,
|
||||
record: guild,
|
||||
score: 0,
|
||||
comparator: guild.name
|
||||
};
|
||||
}
|
||||
));
|
||||
|
||||
const hasQuery = query !== "";
|
||||
|
||||
return {
|
||||
results: useMemo(() => filterResults({
|
||||
results: results,
|
||||
hasQuery: hasQuery,
|
||||
frequentChannels: frequentChannels,
|
||||
pinnedDestinations: pinned,
|
||||
channelHistory: channelHistory,
|
||||
guilds: guilds
|
||||
}), [results, hasQuery, frequentChannels, pinned, channelHistory, guilds]),
|
||||
}), [results, hasQuery, frequentChannels, channelHistory, guilds]),
|
||||
updateSearchText: updateSearch
|
||||
};
|
||||
}
|
||||
|
||||
const { results, updateSearchText } = generateResults({
|
||||
selectedDestinations: selected,
|
||||
});
|
||||
const { results, updateSearchText } = generateResults();
|
||||
|
||||
const selectedDestinationKeys = useMemo(() => {
|
||||
return selected?.map(destination => `${destination.type}-${destination.id}`) || [];
|
||||
|
@ -615,7 +546,6 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
|
||||
const key = `${destination.type}-${destination.id}`;
|
||||
|
||||
|
||||
const rowProps: UnspecificRowProps = {
|
||||
key,
|
||||
destination,
|
||||
|
@ -636,23 +566,12 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
if (type === "GUILD")
|
||||
return generateGuildItem(record, rowProps);
|
||||
else throw new Error("Unknown type " + type);
|
||||
}, [results, selectedDestinationKeys, handleToggleDestination]);
|
||||
const navRef = useRef(null);
|
||||
const nav = createNavigator(cl("nav"), navRef);
|
||||
}, [results]);
|
||||
|
||||
return navigatorWrapper({
|
||||
navigator: nav,
|
||||
children: navigatorData({
|
||||
children: e => {
|
||||
const { ref, ...data } = e;
|
||||
return <SearchBarModule.ModalListContent
|
||||
scrollerRef={
|
||||
elem => {
|
||||
navRef.current = elem;
|
||||
ref.current = elem?.getScrollerNode() ?? null;
|
||||
}
|
||||
}
|
||||
{...data}
|
||||
tabIndex={-1}
|
||||
data-list-id="NO_LIST"
|
||||
role="list"
|
||||
paddingBottom={paddingBottom}
|
||||
paddingTop={paddingTop}
|
||||
sections={sectionCount}
|
||||
|
@ -660,9 +579,6 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
renderRow={callback}
|
||||
rowHeight={rowHeight}/>;
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const setSelectedCallback = useCallback((e: DestinationItem) => {
|
||||
|
@ -673,11 +589,9 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
});
|
||||
|
||||
if (index === -1) {
|
||||
refCounter.current += 1;
|
||||
return [e, ...currentSelected];
|
||||
}
|
||||
|
||||
refCounter.current += 1;
|
||||
currentSelected.splice(index, 1);
|
||||
return [...currentSelected];
|
||||
});
|
||||
|
@ -713,7 +627,6 @@ export default function SearchModal({ modalProps, onSubmit, input, searchType =
|
|||
setSearchText("");
|
||||
updateSearchText("");
|
||||
}}
|
||||
autoFocus={true}
|
||||
/>
|
||||
</ModalHeader>
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue