import React from 'react';

import { PageFooter } from 'strapi-helper-plugin';

import { generatePageFromStart, generateStartFromPage } from '../../utils';
import Filters from '../Filters';
import Flex from '../Flex';
import List from '../List';
import ListEmpty from '../ListEmpty';
import Padded from '../Padded';
import SelectAll from '../SelectAll';
import SortPicker from '../SortPicker';
import useModalContext from '../../hooks/useModalContext';
import Wrapper from './Wrapper';

const BrowseAssets = () => {
  const {
    count,
    files,
    goTo,
    handleAllFilesSelection,
    handleFileSelection,
    multiple,
    params,
    removeFilter,
    selectedFiles,
    setParam,
    sort,
  } = useModalContext();

  const handleChangeParams = ({ target: { name, value } }) => {
    setParam({ name, value });
  };

  const handleChangeListParams = ({ target: { name, value } }) => {
    if (name.includes('_page')) {
      handleChangeParams({
        target: { name: '_start', value: generateStartFromPage(value, params._limit) },
      });
    } else {
      handleChangeParams({ target: { name: '_limit', value } });
    }
  };

  const handleDeleteFilter = index => {
    removeFilter(index);
  };

  const handleGoToUpload = () => {
    goTo('browse');
  };

  const limit = parseInt(params._limit, 10) || 10;
  const start = parseInt(params._start, 10) || 0;

  const paginationParams = {
    _limit: parseInt(params._limit, 10) || 10,
    _page: generatePageFromStart(start, limit),
  };

  const areAllCheckboxesSelected = files.length === selectedFiles.length;
  const hasSomeCheckboxSelected = selectedFiles.length > 0;
  const canSelectFile = multiple === true || (selectedFiles.length < 1 && !multiple);

  return (
    <Wrapper top size="sm">
      <Padded top bottom>
        <Flex flexWrap="wrap">
          {multiple && (
            <Padded right size="sm">
              <SelectAll
                checked={areAllCheckboxesSelected}
                onChange={handleAllFilesSelection}
                someChecked={hasSomeCheckboxSelected && !areAllCheckboxesSelected}
              />
            </Padded>
          )}
          <SortPicker onChange={handleChangeParams} value={sort} />
          <Padded left size="sm" />
          <Filters
            filters={params.filters}
            onChange={handleChangeParams}
            onClick={handleDeleteFilter}
          />
        </Flex>
      </Padded>
      {!files || files.length === 0 ? (
        <ListEmpty numberOfRows={2} onClick={handleGoToUpload} />
      ) : (
        <>
          <List
            canSelect={canSelectFile}
            data={files}
            onChange={handleFileSelection}
            selectedItems={selectedFiles}
          />
          <Padded left right>
            <PageFooter
              context={{ emitEvent: () => {} }}
              count={count}
              onChangeParams={handleChangeListParams}
              params={paginationParams}
            />
          </Padded>
        </>
      )}
    </Wrapper>
  );
};

export default BrowseAssets;

@@ -1508,30 +1508,31 @@
   const 
-areAll
+hasSome
 Checkbox
 esSelect
@@ -1515,34 +1515,32 @@
  hasSomeCheckbox
-es
 Selected = files
@@ -1544,39 +1544,92 @@
 les.
-length === selectedFiles.length
+some(file =%3E%0A    selectedFiles.find(selectedFile =%3E file.id === selectedFile.id)%0A  )
 ;%0A  
@@ -1630,39 +1630,38 @@
  );%0A  const 
-hasSome
+areAll
 Checkbox
 Selected = s
@@ -1640,32 +1640,34 @@
 t areAllCheckbox
+es
 Selected = selec
@@ -1664,33 +1664,128 @@
 ed =
- selectedFiles.length %3E 0
+%0A    files.every(file =%3E selectedFiles.find(selectedFile =%3E file.id === selectedFile.id)) &&%0A    hasSomeCheckboxSelected
 ;%0A  

Fix select all