Files
discord-decomp/modules/search/native/SearchPlatformUtils.tsx
T
2026-11-01 12:12:55 +02:00

519 lines
17 KiB
TypeScript

// Metro module 11452 (f11453).
import SearchMessageStore from "../SearchMessageStore";
import SearchQueryStore from "./stores/SearchQueryStore";
import { SEARCH_TABS_TO_SEARCH_QUERY_LIMITS, MAX_SEARCH_RESULTS_LIMIT, SEARCH_INITIAL_MESSAGE_TABS, CHANNEL_SEARCH_INITIAL_MESSAGE_TABS, MESSAGE_SEARCH_RESULT_TABS, SearchMediaTypes, SearchFileTypes, SearchLinkTypes } from "../SearchConstants";
import { SEARCH_TEXT_INPUT_DEBOUNCE_TIME, PLATFORM_REGEX_ICON_PAIRS } from "./SearchPlatformConstants";
import { SearchTypes, SearchModes, MessageFlags } from "../../../Constants";
import { getSearchTabFetchId, tokenizeQuery, getSelectionScope, getGuildIdFromSearchContext } from "../SearchUtils";
import { isThumbnailAttachment, isValidImageAttachment, isValidVideoAttachment, extractMediaFromAttachment, isValidImageEmbed, isValidVideoEmbed, extractMediaFromEmbed, extractMediaFromMessageComponents } from "../../media_viewer/native/MediaSourceUtil";
import * as _Module1384 from "../../../_bundle/Module1384";
import { getKeyboardIsOpen } from "../../keyboard/native/useKeyboardIsOpen";
import { isIOS } from "../../../utils/PlatformUtils";
import { dismissGlobalKeyboard } from "../../../utils/native/KeyboardManagerUtils";
import { LinkIcon } from "../../../design/components/Icon/native/redesign/generated/LinkIcon";
import URLUtils from "../../../utils/URLUtils";
import { ClydeIcon } from "../../../design/components/Icon/native/redesign/generated/ClydeIcon";
import Module712 from "../../../_bundle/Module712";
import { SearchResultExactCountEnabled } from "../../user_settings/UserSettings";
import Tracking from "./tracking/Tracking";
import SearchPlatformActionCreators from "./SearchPlatformActionCreators";
import SearchActionCreators from "../SearchActionCreators";
import Module12 from "../../../_bundle/Module12";
import { HermesBuiltin } from "../../../_hermes/prelude";
function $mk87255($env) {
return function callback() {
const r2 = $env;
const r3 = $env.$parent;
const r9 = (0, r2[1])(r3[4].getManager(r2[0]));
if (!(r2[3] != null && (0, r3[1])(r3[2][20])(r2[3], r9))) {
const r17 = r2[3];
r2[3] = r9;
(0, r2[2])(r9, r17);
}
};
}
function $mk87256($env) {
return () => $env.$parent[4].removeChangeListener($env[4]);
}
let getGridItemBorderStyles, getInitialFetchLimit, getNextFetchLimit, onInitialFetchMessagesSuccess, onFetchMessagesStart, fetchInitialMessages;
let env7797 = {};
export function performKeyboardAwareNavigation(a0) {
if (getKeyboardIsOpen() && isIOS()) {
dismissGlobalKeyboard();
setTimeout(() => a0(), 100);
} else {
a0();
}
}
export function delayUntilNavigationComplete(c18852_0) {
setTimeout(() => c18852_0(), 200);
}
export function getUrlIcon(a0) {
if (a0 == null) {
return LinkIcon;
}
if (URLUtils.safeParseWithQuery(a0) == null) {
return LinkIcon;
}
if (URLUtils.isDiscordUrl(a0)) {
return ClydeIcon;
}
const length = PLATFORM_REGEX_ICON_PAIRS.length;
let r0 = 0;
if (0 < length) {
do {
const r22 = PLATFORM_REGEX_ICON_PAIRS[r0];
const { REGEX, Icon } = r22;
const r25 = REGEX.exec(a0);
const r1 = r0;
if (r25 != null) {
return Icon;
}
r0 = r1 + 1;
} while (r0 < PLATFORM_REGEX_ICON_PAIRS.length);
}
return LinkIcon;
}
getGridItemBorderStyles = function getGridItemBorderStyles(a0) {
let r22;
L12: {
const { itemIndex, numItems, numColumns } = a0;
const r14 = Math.ceil(numItems / numColumns);
const r18 = Math.floor(itemIndex / numColumns);
const r3 = r14 - 1;
if (itemIndex === 0) {
r22 = {
borderTopLeftRadius: Module712.radii.lg
};
} else if (itemIndex === numColumns - 1) {
r22 = {
borderTopRightRadius: Module712.radii.lg
};
} else {
if (itemIndex % numColumns == 0 && r18 === r3) {
r22 = {
borderBottomLeftRadius: Module712.radii.lg
};
break L12;
}
r22 = itemIndex === r14 * numColumns - 1 ? {
borderBottomRightRadius: Module712.radii.lg
} : void 0;
}
}
return r22;
};
export function getMediaGridItemStyles({ itemIndex, numItems, numColumns, spacing }) {
const r13 = {
...getGridItemBorderStyles({
itemIndex,
numItems,
numColumns
})
};
const marginEnd = spacing * (numColumns - 1) / numColumns;
const r18 = itemIndex % numColumns;
HermesBuiltin.copyDataProperties(r13, r18 === 0 ? {
marginEnd
} : numColumns - 1 === r18 ? {
marginStart: marginEnd
} : {
marginHorizontal: marginEnd / 2
});
return r13;
}
export function getGridItemSpacingStyles({ itemIndex, spacing, numColumns }) {
const marginEnd = spacing * (numColumns - 1) / numColumns;
const r9 = itemIndex % numColumns;
if (r9 === 0) {
return {
marginEnd
};
}
if (numColumns - 1 === r9) {
return {
marginStart: marginEnd
};
}
return {
marginHorizontal: marginEnd / 2
};
}
export function toSearchBarTag(a0) {
return {
id: a0.text,
text: a0.text
};
}
getInitialFetchLimit = function getInitialFetchLimit(a0) {
return Math.min(SEARCH_TABS_TO_SEARCH_QUERY_LIMITS[a0], MAX_SEARCH_RESULTS_LIMIT);
};
getNextFetchLimit = function getNextFetchLimit(a0) {
return Math.min(2 * SEARCH_TABS_TO_SEARCH_QUERY_LIMITS[a0], MAX_SEARCH_RESULTS_LIMIT);
};
onInitialFetchMessagesSuccess = function onInitialFetchMessagesSuccess(a0) {
const searchContext = a0.searchContext;
if (a0.tabEntries.every((tabEntry) => {
const [, r14] = tabEntry;
return r14.total_results === 0 || r14.total_results == null;
})) {
Tracking.trackSearchEmptyMessageResult({
searchContext
});
}
};
onFetchMessagesStart = function onFetchMessagesStart({ searchContext, searchQueryString }) {
SearchPlatformActionCreators.updateSearchQuery(searchContext, (a0) => a0.setSearchResultsQuery(searchQueryString));
};
fetchInitialMessages = function fetchInitialMessages(a0) {
let searchTabs;
let c18854_0 = a0;
const isInitialSearchQuery = SearchQueryStore.isInitialSearchQuery(a0);
const queryString = SearchQueryStore.getQueryString(a0);
if (isInitialSearchQuery) {
const type = a0.type;
searchTabs = SearchTypes.GUILD_CHANNEL !== type && SearchTypes.CHANNEL !== type ? SEARCH_INITIAL_MESSAGE_TABS : CHANNEL_SEARCH_INITIAL_MESSAGE_TABS;
} else {
searchTabs = MESSAGE_SEARCH_RESULT_TABS;
}
const searchTabFetchId = getSearchTabFetchId(a0, searchTabs[0], queryString);
if (!SearchMessageStore.getIsFetching(searchTabFetchId)) {
SearchActionCreators.clearAllSearchMesssages();
Tracking.trackSearchStarted({
searchContext: a0
});
let cursor = SearchMessageStore.getCursor(searchTabFetchId);
if (cursor == null) {
cursor = null;
}
SearchActionCreators.fetchTabMessages({
searchContext: a0,
searchTabs,
searchQueryString: queryString,
getId: (a0) => getSearchTabFetchId(c18854_0, a0, queryString),
getLimit: getInitialFetchLimit,
onFetchStart: onFetchMessagesStart,
onFetchSuccess: onInitialFetchMessagesSuccess,
pagination: {
cursor
},
trackExactTotalHits: SearchResultExactCountEnabled.getSetting(),
searchMode: SearchModes.NEWEST
});
}
};
const syncAutocomplete = function syncAutocomplete(a0) {
const queryString3 = SearchQueryStore.getQueryString(a0, true);
const tokens = tokenizeQuery(queryString3);
SearchActionCreators.updateAutocompleteQuery({
searchContext: a0,
tokens,
cursorScope: getSelectionScope(tokens, queryString3.length - 1, queryString3.length - 1),
queryString: queryString3
});
};
const r52 = {
performKeyboardAwareNavigation,
delayUntilNavigationComplete,
getUrlIcon,
getGridItemBorderStyles,
getMediaGridItemStyles,
getGridItemSpacingStyles,
toSearchBarTag,
fetchInitialMessages,
fetchInitialMessagesDebounced: Module12.debounce(fetchInitialMessages, SEARCH_TEXT_INPUT_DEBOUNCE_TIME),
fetchNextMessages: function fetchNextMessages(a0, a1, a2) {
let r44, cursor, messages, totalCount;
let c18856_0 = a0;
const queryString2 = SearchQueryStore.getQueryString(a0);
const searchTabFetchId2 = getSearchTabFetchId(a0, a1, queryString2);
const r28 = Math.min(SEARCH_TABS_TO_SEARCH_QUERY_LIMITS[a1], MAX_SEARCH_RESULTS_LIMIT);
const searchTabFetchId = getSearchTabFetchId(a0, a1, queryString2);
const isInitialFetchComplete = SearchMessageStore.getIsInitialFetchComplete(searchTabFetchId);
let r1 = true;
if (isInitialFetchComplete) {
L12: {
L5: {
cursor = SearchMessageStore.getCursor(searchTabFetchId);
totalCount = SearchMessageStore.getTotalCount(searchTabFetchId);
messages = SearchMessageStore.getMessages(searchTabFetchId);
if (!SearchResultExactCountEnabled.getSetting()) {
if (totalCount != null && totalCount < r28) {
break L5;
}
r44 = cursor != null;
break L12;
}
}
r44 = cursor != null && totalCount != null && messages != null && messages.length < totalCount;
}
r1 = r44;
}
const isFetching = SearchMessageStore.getIsFetching(searchTabFetchId2);
const isInitialFetchComplete2 = SearchMessageStore.getIsInitialFetchComplete(searchTabFetchId2);
const r50 = (r1 ? !isInitialFetchComplete2 : !r1) || isFetching;
let r51 = !r50;
if (!r50) {
let cursor2 = SearchMessageStore.getCursor(searchTabFetchId2);
if (cursor2 == null) {
cursor2 = null;
}
r51 = SearchActionCreators.fetchTabMessages({
searchContext: a0,
searchTabs: [a1],
searchQueryString: queryString2,
getLimit: getNextFetchLimit,
getId: (a0) => getSearchTabFetchId(c18856_0, a0, queryString2),
onFetchStart: onFetchMessagesStart,
onFetchSuccess: a2,
pagination: {
cursor: cursor2
},
trackExactTotalHits: SearchResultExactCountEnabled.getSetting(),
searchMode: SearchModes.NEWEST
});
}
return r51;
},
syncAutocomplete,
syncAutocompleteDebounced: Module12.debounce(syncAutocomplete, SEARCH_TEXT_INPUT_DEBOUNCE_TIME),
navigateToSearchWithPrefetch: function navigateToSearchWithPrefetch(a0, a1) {
SearchActionCreators.initializeAutocomplete(a1);
SearchPlatformActionCreators.initializeSearchQuery(a1);
fetchInitialMessages(a1);
a0.navigate("search", {
searchContext: a1
});
},
subscribeSearchQueryState: function subscribeSearchQueryState(a0, a1, a2, a3) {
let env18855;
const r2 = env18855 = {
$parent: env7797
};
env18855[0] = a0;
env18855[1] = a1;
env18855[2] = a2;
let r3 = a3;
const r5 = $mk87255(r2);
env18855[4] = r5;
const r12 = a1(SearchQueryStore.getManager(a0));
env18855[3] = r12;
if (a3) {
r3 = r12 != null;
}
if (r3) {
a2(env18855[3], void 0);
}
SearchQueryStore.addChangeListener(r5);
return $mk87256(r2);
},
subscribeTextInputValue: function subscribeTextInputValue(a0, a1, a2) {
let env18858;
let c18857_0 = a1;
let r6 = a2;
const r2 = env18858 = {
$parent: env7797
};
env18858[0] = a0;
env18858[1] = (a0) => ({
textInputValue: a0.getTextInputValue(),
textInputChangedFromInput: a0.getTextValueChangedFromInput()
});
env18858[2] = (a0, a1) => {
c18857_0(a0.textInputValue, a1?.textInputValue, a0.textInputChangedFromInput);
};
const r5 = $mk87255(r2);
env18858[4] = r5;
const manager = SearchQueryStore.getManager(a0);
const r16 = {
textInputValue: manager.getTextInputValue(),
textInputChangedFromInput: manager.getTextValueChangedFromInput()
};
env18858[3] = r16;
if (a2) {
r6 = true;
}
if (r6) {
a1(r16.textInputValue, void 0, r16.textInputChangedFromInput);
}
SearchQueryStore.addChangeListener(r5);
return $mk87256(r2);
}
};
export default r52;
export function getMedia(a0, a1) {
let r10 = getGuildIdFromSearchContext(a0);
if (r10 == null) {
r10 = void 0;
}
let c18859_0 = r10;
const r12 = [];
let c18859_1 = r12;
a1.forEach((a0) => {
let c27068_0 = a0;
let mediaIndex = 0;
const contentMessage3 = a0.getContentMessage();
const attachments = contentMessage3.attachments;
if (attachments != null) {
attachments.forEach((a0, index) => {
L5: {
if (!isThumbnailAttachment(a0)) {
if (!isValidImageAttachment(a0) && !isValidVideoAttachment(a0)) {
break L5;
}
const sources = extractMediaFromAttachment(a0, c27068_0, index, c18859_0);
if (sources != null) {
c18859_1.push({
type: SearchMediaTypes.ATTACHMENT,
attachment: a0,
messageId: c27068_0.id,
channelId: c27068_0.channel_id,
author: c27068_0.author,
mediaIndex,
sources
});
mediaIndex = mediaIndex + 1;
}
}
}
});
}
const embeds = contentMessage3.embeds;
if (embeds != null) {
embeds.forEach((a0, index) => {
if (!(!isValidImageEmbed(a0) && !isValidVideoEmbed(a0))) {
const sources = extractMediaFromEmbed(a0, c27068_0, contentMessage3, index, c18859_0);
if (sources != null) {
c18859_1.push({
type: SearchMediaTypes.EMBED,
embed: a0,
messageId: c27068_0.id,
channelId: c27068_0.channel_id,
author: c27068_0.author,
mediaIndex,
sources
});
mediaIndex = mediaIndex + 1;
}
}
});
}
for (const item of extractMediaFromMessageComponents(a0, contentMessage3, c18859_0)) {
c18859_1.push({
type: SearchMediaTypes.COMPONENT,
messageId: a0.id,
channelId: a0.channel_id,
author: a0.author,
mediaIndex,
sources: item.sources,
unfurledMediaItem: item.unfurledMediaItem
});
mediaIndex = mediaIndex + 1;
}
if (_Module1384.hasFlag(contentMessage3.flags, MessageFlags.IS_VOICE_MESSAGE)) {
c18859_1.push({
type: SearchMediaTypes.AUDIO,
messageId: a0.id,
channelId: a0.channel_id,
author: a0.author,
mediaIndex
});
mediaIndex = mediaIndex + 1;
}
});
return r12;
}
export function getFiles(a0) {
let c18860_0 = a0;
const r1 = [];
let c18860_1 = r1;
const contentMessage = a0.getContentMessage();
const attachments = contentMessage.attachments;
if (attachments != null) {
attachments.forEach((a0, index) => {
L6: {
if (!isValidImageAttachment(a0) && !isValidVideoAttachment(a0)) {
const hasFlag = _Module1384.hasFlag(contentMessage.flags, MessageFlags.IS_VOICE_MESSAGE);
const r26 = {
type: null,
messageId: null,
channelId: null,
author: null,
fileIndex: null,
attachment: null
};
if (hasFlag) {
r26.type = SearchFileTypes.AUDIO;
r26.messageId = c18860_0.id;
r26.channelId = c18860_0.channel_id;
r26.author = c18860_0.author;
r26.fileIndex = index;
r26.attachment = a0;
c18860_1.push(r26);
break L6;
}
r26.type = SearchFileTypes.ATTACHMENT;
r26.messageId = c18860_0.id;
r26.channelId = c18860_0.channel_id;
r26.author = c18860_0.author;
r26.fileIndex = index;
r26.attachment = a0;
c18860_1.push(r26);
} else {
c18860_1.push({
type: SearchFileTypes.MEDIA_ATTACHMENT,
messageId: c18860_0.id,
channelId: c18860_0.channel_id,
author: c18860_0.author,
fileIndex: index,
attachment: a0
});
}
}
});
}
return r1;
}
export function getLinks(a0, a1) {
let r12 = getGuildIdFromSearchContext(a0);
if (r12 == null) {
r12 = void 0;
}
let c18861_1 = r12;
const r13 = [];
let c18861_2 = r13;
const contentMessage2 = a1.getContentMessage();
const linkIndex = 0;
const embeds = contentMessage2.embeds;
if (embeds != null) {
embeds.forEach((a0, index) => {
if (!(!isValidImageEmbed(a0) && !isValidVideoEmbed(a0))) {
const sources = extractMediaFromEmbed(a0, a1, contentMessage2, index, c18861_1);
if (sources != null) {
c18861_2.push({
type: SearchLinkTypes.EMBED,
messageId: a1.id,
channelId: a1.channel_id,
author: a1.author,
linkIndex,
sources,
embed: a0
});
linkIndex = linkIndex + 1;
}
}
});
}
if (r13.length === 0) {
r13.push({
type: SearchLinkTypes.TEXT,
messageId: a1.id,
channelId: a1.channel_id,
author: a1.author,
linkIndex: 0
});
}
return r13;
}
export { getGridItemBorderStyles };