Files
discord-decomp/modules/app_launcher/hooks/useAppLauncherFrecents.tsx
T
2026-11-01 12:12:55 +02:00

115 lines
5.3 KiB
TypeScript

// Metro module 11292 (f11293).
import { useEffect, useMemo } from "react";
import AuthorizedAppsStore, { FetchState } from "../../oauth2/AuthorizedAppsStore";
import UserStore from "../../../stores/UserStore";
import AppLauncherLastUsedCommandStore from "../AppLauncherLastUsedCommandStore";
import { WATCH_YOUTUBE_PROD_APP_ID } from "../../activities/Constants";
import { BuiltInSectionId, DISCOVERY_COMMANDS_QUERY_LIMIT } from "../../application_commands/ApplicationCommandConstants";
import { ApplicationCommandType } from "../../../flow/Server";
import { useDiscovery } from "../../application_commands/ApplicationCommandQueryApi";
import { useStateFromStores, useStateFromStoresArray } from "../../../_bundle/Module589";
import AuthorizedAppsActionCreators from "../../oauth2/AuthorizedAppsActionCreators";
import { OAuth2Scopes } from "../../../_bundle/Module8174";
import { useSortApplicationsViaFrecency } from "../../applications/ApplicationFrecencyHooks";
import isApplicationAgeRestricted from "../../applications/utils/isApplicationAgeRestricted";
import { isEmbeddedApp } from "../utils/AppLauncherUtils";
import { queryForPrimaryAppCommand } from "../../application_commands/getPrimaryAppCommand";
let filters, options;
const useFrecentApps = function useFrecentApps({ sectionDescriptors, context, onlyActivityApps, includeAuthorizedAppsAndFetch }) {
let c18717_4, memo, sortApplicationsViaFrecency, stateFromStores2;
c18717_4 = void 0;
memo = void 0;
sortApplicationsViaFrecency = void 0;
stateFromStores2 = void 0;
const stateFromStores = useStateFromStores([AuthorizedAppsStore], () => AuthorizedAppsStore.getFetchState());
useEffect(() => {
let r1 = includeAuthorizedAppsAndFetch;
if (includeAuthorizedAppsAndFetch) {
r1 = stateFromStores === FetchState.NOT_FETCHED;
}
if (r1) {
AuthorizedAppsActionCreators.fetch();
}
}, [includeAuthorizedAppsAndFetch, stateFromStores]);
const stateFromStoresArray = useStateFromStoresArray([AuthorizedAppsStore], () => includeAuthorizedAppsAndFetch ? AuthorizedAppsStore.getNewestTokens().filter((item) => item.scopes.includes(OAuth2Scopes.APPLICATIONS_COMMANDS)) : []);
const r31 = sectionDescriptors.filter((sectionDescriptor) => sectionDescriptor.id !== BuiltInSectionId.FRECENCY && sectionDescriptor.id !== BuiltInSectionId.BUILT_IN);
const r1 = context.type === "contextless";
c18717_4 = r1;
memo = useMemo(() => {
const r1 = [];
if (c18717_4) {
r1.push(WATCH_YOUTUBE_PROD_APP_ID);
}
return r1;
}, [r1]);
sortApplicationsViaFrecency = useSortApplicationsViaFrecency(r31, stateFromStoresArray);
stateFromStores2 = useStateFromStores([UserStore], () => UserStore.getCurrentUser()?.nsfwAllowed);
return useMemo(() => {
const hideAgeRestricted = function hideAgeRestricted(a0) {
return !(stateFromStores2 === false && isApplicationAgeRestricted(a0.id));
};
return onlyActivityApps ? sortApplicationsViaFrecency.filter((item) => item.application != null && isEmbeddedApp(item.application) && queryForPrimaryAppCommand(context, item.id) != null).filter((item) => !memo.includes(item.id)).filter(hideAgeRestricted) : sortApplicationsViaFrecency.filter((item) => !memo.includes(item.id)).filter(hideAgeRestricted);
}, [onlyActivityApps, sortApplicationsViaFrecency, context, memo, stateFromStores2]);
};
filters = {
commandTypes: [ApplicationCommandType.CHAT, ApplicationCommandType.PRIMARY_ENTRY_POINT]
};
options = {
placeholderCount: 0,
limit: DISCOVERY_COMMANDS_QUERY_LIMIT,
includeFrecency: true
};
export default function useAppLauncherFrecentCommandsAndApps({ context: context2, onlyActivityApps: onlyActivityApps2 }) {
let stateFromStores3;
stateFromStores3 = void 0;
const discovery = useDiscovery({
context: context2,
filters,
options,
allowFetch: true
});
const { commands, commandsByActiveSection, sectionDescriptors, filterSection, loading } = discovery;
useEffect(() => {
filterSection(BuiltInSectionId.FRECENCY);
}, [filterSection]);
stateFromStores3 = useStateFromStores([AppLauncherLastUsedCommandStore], () => AppLauncherLastUsedCommandStore.getLastUsedCommandId());
return {
loading,
frecencyCommands: useMemo(() => {
if (onlyActivityApps2) {
return [];
}
let r7 = commandsByActiveSection[0]?.data;
if (r7 == null) {
r7 = [];
}
const r14 = commands.find((command) => command.id === stateFromStores3);
return r14 != null ? [r14, ...r7.filter((item) => item.id !== stateFromStores3)] : r7;
}, [commands, commandsByActiveSection, stateFromStores3, onlyActivityApps2]),
frecentApps: useFrecentApps({
sectionDescriptors,
context: context2,
onlyActivityApps: onlyActivityApps2,
includeAuthorizedAppsAndFetch: true
}),
sectionDescriptors
};
};
export function useAppLauncherFrecentApps({ context: context2, onlyActivityApps: onlyActivityApps3, allowCommandFetch, includeAuthorizedAppsAndFetch: includeAuthorizedAppsAndFetch2 }) {
const discovery = useDiscovery({
context: context2,
filters,
options,
allowFetch: allowCommandFetch
});
return {
loading: discovery.loading,
frecentApps: useFrecentApps({
sectionDescriptors: discovery.sectionDescriptors,
context: context2,
onlyActivityApps: onlyActivityApps3,
includeAuthorizedAppsAndFetch: includeAuthorizedAppsAndFetch2
})
};
}