<commit_before>import * as React from 'react'
import { UserQueryEntity, UserQueryMessage, QueryOrderEmbedded, QueryColumnEmbedded } from '../Signum.Entities.UserQueries'
import { FormGroup, ValueLine, EntityLine, EntityTable } from '@framework/Lines'
import * as Finder from '@framework/Finder'
import { SubTokensOptions } from '@framework/FindOptions'
import { getQueryNiceName } from '@framework/Reflection'
import { TypeContext } from '@framework/TypeContext'
import QueryTokenEmbeddedBuilder from '../../UserAssets/Templates/QueryTokenEmbeddedBuilder'
import FilterBuilderEmbedded from '../../UserAssets/Templates/FilterBuilderEmbedded';
import { useAPI, useForceUpdate } from '@framework/Hooks'
import { QueryTokenEmbedded } from '../../UserAssets/Signum.Entities.UserAssets'
import { SearchMessage } from '@framework/Signum.Entities'

const CurrentEntityKey = "[CurrentEntity]";

export default function UserQuery(p: { ctx: TypeContext<UserQueryEntity> }) {

  const forceUpdate = useForceUpdate();

  const query = p.ctx.value.query;
  const ctx = p.ctx;
  const ctxxs = ctx.subCtx({ formSize: "ExtraSmall" });

  const canAggregate = ctx.value.groupResults ? SubTokensOptions.CanAggregate : 0;

  return (
    <div>
      <EntityLine ctx={ctx.subCtx(e => e.owner)} />
      <ValueLine ctx={ctx.subCtx(e => e.displayName)} />
      <FormGroup ctx={ctx.subCtx(e => e.query)}>
        {
          query && (
            Finder.isFindable(query.key, true) ?
              <a className="form-control-static" href={Finder.findOptionsPath({ queryName: query.key })}>{getQueryNiceName(query.key)}</a> :
              <span>{getQueryNiceName(query.key)}</span>)
        }
      </FormGroup>

      {query &&
        (<div>
          <EntityLine ctx={ctx.subCtx(e => e.entityType)} readOnly={ctx.value.appendFilters} onChange={() => forceUpdate()}
            helpText={UserQueryMessage.MakesTheUserQueryAvailableAsAQuickLinkOf0.niceToString(ctx.value.entityType?.toStr ?? UserQueryMessage.TheSelected0.niceToString(ctx.niceName(a => a.entityType)))} />
          {
            p.ctx.value.entityType &&
            <div className="row">
              <div className="col-sm-4 offset-sm-2">
                <ValueLine ctx={ctx.subCtx(e => e.hideQuickLink)} inlineCheckbox />
              </div>
              <div className="col-sm-4">
                {UserQueryMessage.Use0ToFilterCurrentEntity.niceToString().formatHtml(<pre style={{ display: "inline" }}><strong>{CurrentEntityKey}</strong></pre>)}
              </div>
            </div>
        }
        <ValueLine ctx={ctx.subCtx(e => e.appendFilters)} readOnly={ctx.value.entityType != null} onChange={() => forceUpdate()}
            helpText={UserQueryMessage.MakesTheUserQueryAvailableInContextualMenuWhenGrouping0.niceToString(query?.key)} />
          <ValueLine ctx={ctx.subCtx(e => e.includeDefaultFilters)} valueColumns={2} />
          <ValueLine ctx={ctx.subCtx(e => e.groupResults)} />
          <div>
            <FilterBuilderEmbedded ctx={ctxxs.subCtx(e => e.filters)}
              subTokenOptions={SubTokensOptions.CanAnyAll | SubTokensOptions.CanElement | canAggregate}
              queryKey={ctxxs.value.query!.key}
              showPinnedFilterOptions={true} />
            <ValueLine ctx={ctxxs.subCtx(e => e.columnsMode)} />
            <EntityTable ctx={ctxxs.subCtx(e => e.columns)} columns={EntityTable.typedColumns<QueryColumnEmbedded>([
              {
                property: a => a.token,
                template: (ctx, row) =>
                  <div>
                    <QueryTokenEmbeddedBuilder
                      ctx={ctx.subCtx(a => a.token, { formGroupStyle: "SrOnly" })}
                      queryKey={p.ctx.value.query!.key}
                      onTokenChanged={() => { ctx.value.summaryToken = null; ctx.value.modified = true; row.forceUpdate(); }}
                      subTokenOptions={SubTokensOptions.CanElement | canAggregate} />

                    <div className="row">
                      <div className="col-sm-6">
                      </div>
                      <div className="col-sm-6">
                      </div>
                    </div>


                    <div className="d-flex">
                      <label className="col-form-label col-form-label-xs mr-2" style={{ minWidth: "140px" }}>
                        <input type="checkbox" disabled={ctx.value.token == null} checked={ctx.value.summaryToken != null} onChange={() => {
                          ctx.value.summaryToken = ctx.value.summaryToken == null ? QueryTokenEmbedded.New(ctx.value.token) : null;
                          row.forceUpdate();
                        }} /> {SearchMessage.SummaryHeader.niceToString()}
                      </label>
                      <div className="flex-grow-1">
                        {ctx.value.summaryToken &&
                          <QueryTokenEmbeddedBuilder
                            ctx={ctx.subCtx(a => a.summaryToken, { formGroupStyle: "SrOnly" })}
                            queryKey={p.ctx.value.query!.key}
                            subTokenOptions={SubTokensOptions.CanElement | SubTokensOptions.CanAggregate} />
                        }
                      </div>
                    </div>
                  </div>
              },
              { property: a => a.displayName, template: ctx => <ValueLine ctx={ctx.subCtx(a => a.displayName)} valueHtmlAttributes={{ placeholder: ctx.value.token?.token?.niceName }} /> },
            ])} />
            <EntityTable ctx={ctxxs.subCtx(e => e.orders)} columns={EntityTable.typedColumns<QueryOrderEmbedded>([
              {
                property: a => a.token,
                template: ctx => <QueryTokenEmbeddedBuilder<commit_msg>fix summaryToken in UserQuery.tsx<commit_after>import * as React from 'react'
import { UserQueryEntity, UserQueryMessage, QueryOrderEmbedded, QueryColumnEmbedded } from '../Signum.Entities.UserQueries'
import { FormGroup, ValueLine, EntityLine, EntityTable } from '@framework/Lines'
import * as Finder from '@framework/Finder'
import { SubTokensOptions } from '@framework/FindOptions'
import { getQueryNiceName } from '@framework/Reflection'
import { TypeContext } from '@framework/TypeContext'
import QueryTokenEmbeddedBuilder from '../../UserAssets/Templates/QueryTokenEmbeddedBuilder'
import FilterBuilderEmbedded from '../../UserAssets/Templates/FilterBuilderEmbedded';
import { useAPI, useForceUpdate } from '@framework/Hooks'
import { QueryTokenEmbedded } from '../../UserAssets/Signum.Entities.UserAssets'
import { SearchMessage } from '@framework/Signum.Entities'

const CurrentEntityKey = "[CurrentEntity]";

export default function UserQuery(p: { ctx: TypeContext<UserQueryEntity> }) {

  const forceUpdate = useForceUpdate();

  const query = p.ctx.value.query;
  const ctx = p.ctx;
  const ctxxs = ctx.subCtx({ formSize: "ExtraSmall" });

  const canAggregate = ctx.value.groupResults ? SubTokensOptions.CanAggregate : 0;

  return (
    <div>
      <EntityLine ctx={ctx.subCtx(e => e.owner)} />
      <ValueLine ctx={ctx.subCtx(e => e.displayName)} />
      <FormGroup ctx={ctx.subCtx(e => e.query)}>
        {
          query && (
            Finder.isFindable(query.key, true) ?
              <a className="form-control-static" href={Finder.findOptionsPath({ queryName: query.key })}>{getQueryNiceName(query.key)}</a> :
              <span>{getQueryNiceName(query.key)}</span>)
        }
      </FormGroup>

      {query &&
        (<div>
          <EntityLine ctx={ctx.subCtx(e => e.entityType)} readOnly={ctx.value.appendFilters} onChange={() => forceUpdate()}
            helpText={UserQueryMessage.MakesTheUserQueryAvailableAsAQuickLinkOf0.niceToString(ctx.value.entityType?.toStr ?? UserQueryMessage.TheSelected0.niceToString(ctx.niceName(a => a.entityType)))} />
          {
            p.ctx.value.entityType &&
            <div className="row">
              <div className="col-sm-4 offset-sm-2">
                <ValueLine ctx={ctx.subCtx(e => e.hideQuickLink)} inlineCheckbox />
              </div>
              <div className="col-sm-4">
                {UserQueryMessage.Use0ToFilterCurrentEntity.niceToString().formatHtml(<pre style={{ display: "inline" }}><strong>{CurrentEntityKey}</strong></pre>)}
              </div>
            </div>
        }
        <ValueLine ctx={ctx.subCtx(e => e.appendFilters)} readOnly={ctx.value.entityType != null} onChange={() => forceUpdate()}
            helpText={UserQueryMessage.MakesTheUserQueryAvailableInContextualMenuWhenGrouping0.niceToString(query?.key)} />
          <ValueLine ctx={ctx.subCtx(e => e.includeDefaultFilters)} valueColumns={2} />
          <ValueLine ctx={ctx.subCtx(e => e.groupResults)} />
          <div>
            <FilterBuilderEmbedded ctx={ctxxs.subCtx(e => e.filters)}
              subTokenOptions={SubTokensOptions.CanAnyAll | SubTokensOptions.CanElement | canAggregate}
              queryKey={ctxxs.value.query!.key}
              showPinnedFilterOptions={true} />
            <ValueLine ctx={ctxxs.subCtx(e => e.columnsMode)} />
            <EntityTable ctx={ctxxs.subCtx(e => e.columns)} columns={EntityTable.typedColumns<QueryColumnEmbedded>([
              {
                property: a => a.token,
                template: (ctx, row) =>
                  <div>
                    <QueryTokenEmbeddedBuilder
                      ctx={ctx.subCtx(a => a.token, { formGroupStyle: "SrOnly" })}
                      queryKey={p.ctx.value.query!.key}
                      onTokenChanged={() => { ctx.value.summaryToken = null; ctx.value.modified = true; row.forceUpdate(); }}
                      subTokenOptions={SubTokensOptions.CanElement | canAggregate} />

                    <div className="row">
                      <div className="col-sm-6">
                      </div>
                      <div className="col-sm-6">
                      </div>
                    </div>


                    <div className="d-flex">
                      <label className="col-form-label col-form-label-xs mr-2" style={{ minWidth: "140px" }}>
                        <input type="checkbox" disabled={ctx.value.token == null} checked={ctx.value.summaryToken != null} onChange={() => {
                          ctx.value.summaryToken = ctx.value.summaryToken == null ? QueryTokenEmbedded.New(ctx.value.token) : null;
                          ctx.value.modified = true;
                          row.forceUpdate();
                        }} /> {SearchMessage.SummaryHeader.niceToString()}
                      </label>
                      <div className="flex-grow-1">
                        {ctx.value.summaryToken &&
                          <QueryTokenEmbeddedBuilder
                            ctx={ctx.subCtx(a => a.summaryToken, { formGroupStyle: "SrOnly" })}
                            queryKey={p.ctx.value.query!.key}
                            subTokenOptions={SubTokensOptions.CanElement | SubTokensOptions.CanAggregate} />
                        }
                      </div>
                    </div>
                  </div>
              },
              { property: a => a.displayName, template: ctx => <ValueLine ctx={ctx.subCtx(a => a.displayName)} valueHtmlAttributes={{ placeholder: ctx.value.token?.token?.niceName }} /> },
            ])} />
            <EntityTable ctx={ctxxs.subCtx(e => e.orders)} columns={EntityTable.typedColumns<QueryOrderEmbedded>([
              {
                property: a => a.token,
                template: ctx => <QueryTokenEmbeddedBuilder
{'fraction_non_alphanumeric': 0.13660351241456872, 'fraction_numerical': 0.0027684055714162123, 'mean_word_length': 2.1898454746136866, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 155, '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': '6343613', 'n_tokens_mistral': 3186, 'n_tokens_neox': 3169, 'n_words': 520}