<commit_before>import "./DataFinder.scss";
import React from 'react';
// import {olap} from '../olap/olap'
import { CubeData, Filter, SelectedFilters, GroupInfo } from '../typings/CubeData';
import * as CubeHelpers from './helpers/CubeHelpers';
import * as ParticipantGroupHelpers from './helpers/ParticipantGroup_new';
import { toggleFilter, setAndOr } from './helpers/SelectedFilters';
import { ContentDropdown, AndOrDropdown, FilterDropdownContent } from './components/FilterDropdown'
import { Flag } from './components/FilterIndicator'
import { Barplot } from './components/Barplot'
import { HeatmapSelectorDropdown } from './components/HeatmapSelector';
import  { DataFinderTabs } from "./components/Tabs";
import { Banner, GroupSummary, ManageGroupsDropdown } from "./components/Banner";
import { CubeMdx } from "../typings/Cube";
import whyDidYouRender from "@welldone-software/why-did-you-render";

interface DataFinderControllerProps {
    mdx:  CubeMdx,
    studyInfo: SelectRowsResponse
}

whyDidYouRender(React, {
    onlyLogs: true,
    titleColor: "green",
    diffNameColor: "darkturquoise"
});

const BannerMemo = React.memo(Banner)

const DataFinderController = React.memo<DataFinderControllerProps>(({mdx, studyInfo}) => {
    // Constants -------------------------------------
    const cd = new CubeData({})
    const loadedStudiesArray = CubeHelpers.createLoadedStudies(studyInfo)

    // State ---------------------------------------------
    // ----- Data (updated by API calls) -----
    const [groupSummary, setGroupSummaryState] = React.useState<GroupSummary>({label: "", id: 0, isSaved: true})
    const setGroupSummary = (gs) => {console.log("setting groupSummary state"); console.log(gs); setGroupSummaryState(gs)}
    // Set on page load only
    const [filterCategories, setFilterCategoriesState] = React.useState(null)
    const setFilterCategories = (fc) => {console.log("setting filter categories state"); setFilterCategoriesState(fc)}
    const [studyDict, setStudyDictState] = React.useState(null); // this should only be loaded once
    const setStudyDict = (sd) => {console.log("setting studyDict state"); setStudyDictState(sd)}
    // Updated on "apply": 
    const [cubeData, setCubeDataState] = React.useState<CubeData>(cd)
    const setCubeData = (cd) => {console.log("setting cubeData state"); setCubeDataState(cd)}
    
    const [availableGroups, setAvailableGroups] = React.useState<GroupInfo[]>([])
    // Updated every time a filter is changed: 
    // ----- State set by user ------
    // Groups
    // Filters 
    const [selectedFilters, setSelectedFiltersState] = React.useState<SelectedFilters>(new SelectedFilters())
    const setSelectedFilters = (sf) => {console.log("setting SelectedFilters state"); setSelectedFiltersState(sf)}

    // Effects  -------------------------------------

    // Setup (only run on first render) ----- 
    React.useEffect(() => {

        const groupId = LABKEY.ActionURL.getParameter("groupId")
        if (groupId === undefined) {
            ParticipantGroupHelpers.getSessionParticipantGroup().then((data) => {
            if (data.filters) {
                const sf = new SelectedFilters(JSON.parse(data.filters));
                const newGroupSummary = JSON.parse(data.description) || {
                    id: data.rowId,
                    label: data.label,
                    isSaved: true
                }
                setSelectedFilters(sf)
                setGroupSummary(newGroupSummary)
                applyFilters(sf)
            } else {
                applyFilters(selectedFilters)
            }
            
        })
        } else {
            ParticipantGroupHelpers.getGroupInfoById(groupId).then((data) => {
                if (data) {
                    const groupInfo = {
                        id: data.id,
                        label: data.label,
                        selected: true,
                        filters: JSON.parse(data.filters)
                    }
                    loadParticipantGroup(groupInfo)
                } else {<commit_msg>Fix "sending" group<commit_after>import "./DataFinder.scss";
import React from 'react';
// import {olap} from '../olap/olap'
import { CubeData, Filter, SelectedFilters, GroupInfo } from '../typings/CubeData';
import * as CubeHelpers from './helpers/CubeHelpers';
import * as ParticipantGroupHelpers from './helpers/ParticipantGroup_new';
import { toggleFilter, setAndOr } from './helpers/SelectedFilters';
import { ContentDropdown, AndOrDropdown, FilterDropdownContent } from './components/FilterDropdown'
import { Flag } from './components/FilterIndicator'
import { Barplot } from './components/Barplot'
import { HeatmapSelectorDropdown } from './components/HeatmapSelector';
import  { DataFinderTabs } from "./components/Tabs";
import { Banner, GroupSummary, ManageGroupsDropdown } from "./components/Banner";
import { CubeMdx } from "../typings/Cube";
import whyDidYouRender from "@welldone-software/why-did-you-render";

interface DataFinderControllerProps {
    mdx:  CubeMdx,
    studyInfo: SelectRowsResponse
}

whyDidYouRender(React, {
    onlyLogs: true,
    titleColor: "green",
    diffNameColor: "darkturquoise"
});

const BannerMemo = React.memo(Banner)

const DataFinderController = React.memo<DataFinderControllerProps>(({mdx, studyInfo}) => {
    // Constants -------------------------------------
    const cd = new CubeData({})
    const loadedStudiesArray = CubeHelpers.createLoadedStudies(studyInfo)

    // State ---------------------------------------------
    // ----- Data (updated by API calls) -----
    const [groupSummary, setGroupSummaryState] = React.useState<GroupSummary>({label: "", id: 0, isSaved: true})
    const setGroupSummary = (gs) => {console.log("setting groupSummary state"); console.log(gs); setGroupSummaryState(gs)}
    // Set on page load only
    const [filterCategories, setFilterCategoriesState] = React.useState(null)
    const setFilterCategories = (fc) => {console.log("setting filter categories state"); setFilterCategoriesState(fc)}
    const [studyDict, setStudyDictState] = React.useState(null); // this should only be loaded once
    const setStudyDict = (sd) => {console.log("setting studyDict state"); setStudyDictState(sd)}
    // Updated on "apply": 
    const [cubeData, setCubeDataState] = React.useState<CubeData>(cd)
    const setCubeData = (cd) => {console.log("setting cubeData state"); setCubeDataState(cd)}
    
    const [availableGroups, setAvailableGroups] = React.useState<GroupInfo[]>([])
    // Updated every time a filter is changed: 
    // ----- State set by user ------
    // Groups
    // Filters 
    const [selectedFilters, setSelectedFiltersState] = React.useState<SelectedFilters>(new SelectedFilters())
    const setSelectedFilters = (sf) => {console.log("setting SelectedFilters state"); setSelectedFiltersState(sf)}

    // Effects  -------------------------------------

    // Setup (only run on first render) ----- 
    React.useEffect(() => {

        const groupId = LABKEY.ActionURL.getParameter("groupId")
        if (groupId === undefined) {
            ParticipantGroupHelpers.getSessionParticipantGroup().then((data) => {
            if (data.filters) {
                const sf = new SelectedFilters(JSON.parse(data.filters));
                const newGroupSummary = JSON.parse(data.description) || {
                    id: data.rowId,
                    label: data.label,
                    isSaved: true
                }
                setSelectedFilters(sf)
                setGroupSummary(newGroupSummary)
                applyFilters(sf)
            } else {
                applyFilters(selectedFilters)
            }
            
        })
        } else {
            ParticipantGroupHelpers.getGroupInfoById(groupId).then((data) => {
                if (data) {
                    const groupInfo = {
                        id: data.id,
                        label: data.label,
                        selected: true,
                        filters: JSON.parse(data.filters)
                    }
                    const filterInfo = ParticipantGroupHelpers.getParticipantGroupFilters(groupInfo.filters)
                    const gs = {
                        label: groupInfo.label,
                        id: groupInfo.id,
                        isSaved: false
                    }
                    setSelectedFilters(filterInfo.sf)
                    applyFilters(filterInfo.sf).then(({ pids, countsList }) => {
                            ParticipantGroupHelpers.updateSessionGroup(pids, countsList, filterInfo.sf, gs, studyDict)
                    })
                    setGroupSummary(gs)
                } else {
{'fraction_non_alphanumeric': 0.1417295813315031, 'fraction_numerical': 0.00022878059940517045, 'mean_word_length': 2.296757164404223, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 32, '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': '667206', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2289, 'n_words': 514}