<commit_before>import React from 'dom-chef';
import select from 'select-dom';
import {CheckIcon} from '@primer/octicons-react';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';

import features from '.';
import SearchQuery from '../github-helpers/search-query';

function addMergeLink(): void {
	if (!pageDetect.isPRList()) {
		return;
	}

	// The links in `.table-list-header-toggle` are either:
	//   1 Open | 1 Closed
	//   1 Total            // Apparently appears with is:merged/is:unmerged
	for (const lastLink of select.all('.table-list-header-toggle.states a:last-child')) {
		const lastLinkQuery = SearchQuery.from(lastLink);

		if (lastLinkQuery.includes('is:merged')) {
			// It's a "Total" link for "is:merged"
			lastLink.lastChild!.textContent = lastLink.lastChild!.textContent!.replace('Total', 'Merged');
			continue;
		}

		if (lastLinkQuery.includes('is:unmerged')) {
			// It's a "Total" link for "is:unmerged"
			lastLink.lastChild!.textContent = lastLink.lastChild!.textContent!.replace('Total', 'Unmerged');
			continue;
		}

		// In this case, `lastLink` is expected to be a "Closed" link
		const mergeLink = lastLink.cloneNode(true);
		mergeLink.textContent = 'Merged';
		mergeLink.classList.toggle('selected', SearchQuery.from(location).includes('is:merged'));
		mergeLink.href = SearchQuery.from(mergeLink).replace('is:closed', 'is:merged').href;
		lastLink.after(' ', mergeLink);
	}
}

function togglableFilters(): void {
	for (const link of select.all('.table-list-header-toggle.states a')) {
		select('.octicon', link)?.remove();
		if (link.classList.contains('selected')) {
			link.prepend(<CheckIcon/>);
			link.href = SearchQuery
				.from(link)
				.remove(
					'is:open',
					'is:closed',
					'is:merged',
					'is:unmerged',
				)
				.href;
		}
	}
}

async function init(): Promise<void | false> {
	await elementReady('.table-list-filters');

	addMergeLink();
	togglableFilters();
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isConversationList,
	],
	awaitDomReady: false,
	deduplicate: 'has-rgh-inner',
	init,
});<commit_msg>Fix duplicated "Merged" filter in global conversation list (#5295)<commit_after>import React from 'dom-chef';
import select from 'select-dom';
import {CheckIcon} from '@primer/octicons-react';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';

import features from '.';
import SearchQuery from '../github-helpers/search-query';

function addMergeLink(): void {
	if (!pageDetect.isPRList()) {
		return;
	}

	// The links in `.table-list-header-toggle` are either:
	//   1 Open | 1 Closed
	//   1 Total            // Apparently appears with is:merged/is:unmerged
	for (const lastLink of select.all('.table-list-header-toggle.states a:last-child')) {
		const lastLinkQuery = SearchQuery.from(lastLink);

		if (lastLinkQuery.includes('is:merged')) {
			// It's a "Total" link for "is:merged"
			lastLink.lastChild!.textContent = lastLink.lastChild!.textContent!.replace('Total', 'Merged');
			continue;
		}

		if (lastLinkQuery.includes('is:unmerged')) {
			// It's a "Total" link for "is:unmerged"
			lastLink.lastChild!.textContent = lastLink.lastChild!.textContent!.replace('Total', 'Unmerged');
			continue;
		}

		// In this case, `lastLink` is expected to be a "Closed" link
		const mergeLink = lastLink.cloneNode(true);
		mergeLink.textContent = 'Merged';
		mergeLink.classList.toggle('selected', SearchQuery.from(location).includes('is:merged'));
		mergeLink.href = SearchQuery.from(mergeLink).replace('is:closed', 'is:merged').href;
		lastLink.after(' ', mergeLink);
	}
}

function togglableFilters(): void {
	for (const link of select.all('.table-list-header-toggle.states a')) {
		select('.octicon', link)?.remove();
		if (link.classList.contains('selected')) {
			link.prepend(<CheckIcon/>);
			link.href = SearchQuery
				.from(link)
				.remove(
					'is:open',
					'is:closed',
					'is:merged',
					'is:unmerged',
				)
				.href;
		}
	}
}

async function init(): Promise<void | false> {
	await elementReady('.table-list-filters');

	addMergeLink();
	togglableFilters();
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isRepoConversationList,
	],
	awaitDomReady: false,
	deduplicate: 'has-rgh-inner',
	init,
}, {
	include: [
		pageDetect.isGlobalConversationList,
	],
	awaitDomReady: false,
	init,
});
{'fraction_non_alphanumeric': 0.1604994324631101, 'fraction_numerical': 0.0022701475595913734, 'mean_word_length': 4.189634864546525, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624712', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1453, 'n_words': 307}