<commit_before>/**
 * ModulesList component.
 *
 * Site Kit by Google, Copyright 2019 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Internal dependencies
 */
import Link from './link';
/**
 * External dependencies
 */
import data from 'SiteKitCore/components/data';
import {
	refreshAuthentication,
	getReAuthUrl,
	activateOrDeactivateModule,
	showErrorNotification,
	moduleIcon,
} from 'GoogleUtil';
import GenericError from 'GoogleComponents/notifications/generic-error';
import ModuleSettingsWarning from 'GoogleComponents/notifications/module-settings-warning';

const { Component } = wp.element;
const { __, sprintf } = wp.i18n;
const { sortBy, filter, map } = lodash;

class ModulesList extends Component {
	constructor( props ) {
		super( props );

		this.setupModuleClick = this.setupModuleClick.bind( this );
	}

	/**
	 * Handle setup module click event.
	 *
	 * @param {string} slug Module slug.
	 */
	async setupModuleClick( slug ) {
		try {
			this.setState( { isSaving: true } );
			await activateOrDeactivateModule( data, slug, true );

			await refreshAuthentication();

			// Redirect to ReAuthentication URL
			window.location = getReAuthUrl( slug, true );
		} catch ( err ) {
			showErrorNotification( GenericError, {
				id: 'setup-module-error',
				title: __( 'Internal Server Error', 'google-site-kit' ),
				description: err.message,
				format: 'small',
				type: 'win-error',
			} );
			this.setState( { isSaving: false } );
		}
	}

	render() {
		// Filter out internal modules.
		const modules = filter( window.googlesitekit.modules || [], ( module ) => ! module.internal );

		// Map of slug => name for every module that is active and completely set up.
		const completedModuleNames = {};

		// Sort Modules by sort order.
		let sortedModules = sortBy( modules, ( module ) => {
			if ( module.active && module.setupComplete ) {
				completedModuleNames[ module.slug ] = module.name;
			}
			return module.sort;
		} );

		// Prevent modules with dependencies from displaying (Optimize and Tag Manager).
		// Logic still in place below in case we want to add blocked modules back.
		sortedModules = filter( sortedModules, ( module ) => 0 === module.required.length );

		return (
			<div className="googlesitekit-modules-list">
				{ map( sortedModules, ( module ) => {
					let blockedByParentModule = false;<commit_msg>Simplify Modules list code and use native array functions.<commit_after>/**
 * ModulesList component.
 *
 * Site Kit by Google, Copyright 2019 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Internal dependencies
 */
import Link from './link';
/**
 * External dependencies
 */
import data from 'SiteKitCore/components/data';
import {
	refreshAuthentication,
	getReAuthUrl,
	activateOrDeactivateModule,
	showErrorNotification,
	moduleIcon,
} from 'GoogleUtil';
import GenericError from 'GoogleComponents/notifications/generic-error';
import ModuleSettingsWarning from 'GoogleComponents/notifications/module-settings-warning';

const { Component } = wp.element;
const { __, sprintf } = wp.i18n;
const { map } = lodash;

class ModulesList extends Component {
	constructor( props ) {
		super( props );

		this.setupModuleClick = this.setupModuleClick.bind( this );
	}

	/**
	 * Handle setup module click event.
	 *
	 * @param {string} slug Module slug.
	 */
	async setupModuleClick( slug ) {
		try {
			this.setState( { isSaving: true } );
			await activateOrDeactivateModule( data, slug, true );

			await refreshAuthentication();

			// Redirect to ReAuthentication URL
			window.location = getReAuthUrl( slug, true );
		} catch ( err ) {
			showErrorNotification( GenericError, {
				id: 'setup-module-error',
				title: __( 'Internal Server Error', 'google-site-kit' ),
				description: err.message,
				format: 'small',
				type: 'win-error',
			} );
			this.setState( { isSaving: false } );
		}
	}

	render() {
		// Filter out internal modules.
		const modules = Object.values( window.googlesitekit.modules || {} ).filter( ( module ) => ! module.internal );

		// Map of slug => name for every module that is active and completely set up.
		const completedModuleNames = modules
			.filter( ( module ) => module.active && module.setupComplete )
			.reduce( ( completed, module ) => {
				completed[ module.slug ] = module.name;
				return completed;
			}, {} );

		// Sort modules and exclude those that required other modules to be set up.
		// Logic still in place below in case we want to add blocked modules back.
		const sortedModules = modules
			.filter( ( module ) => 0 === module.required.length )
			.sort( ( module1, module2 ) => module1.sort - module2.sort );

		return (
			<div className="googlesitekit-modules-list">
				{ map( sortedModules, ( module ) => {
					let blockedByParentModule = false;
{'fraction_non_alphanumeric': 0.11010118333047504, 'fraction_numerical': 0.004458926427713943, 'mean_word_length': 3.6066350710900474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5199186', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1607, 'n_words': 560}