Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution][Data Quality Dashboard]: migrate styled-component… #205559

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions packages/kbn-babel-preset/styled_components_files.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/kbn-test/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module.exports = {

// A map from regular expressions to paths to transformers
transform: {
'^.+\\.(js|tsx?)$': '<rootDir>/packages/kbn-test/src/jest/transforms/babel.js',
'^.+\\.(js|tsx?)$': '<rootDir>/packages/kbn-test/src/jest/transforms/babel/index.js',
'^.+\\.(txt|html)?$': '<rootDir>/packages/kbn-test/src/jest/transforms/raw.js',
'^.+\\.peggy?$': '<rootDir>/packages/kbn-test/src/jest/transforms/peggy.js',
},
Expand Down
13 changes: 13 additions & 0 deletions packages/kbn-test/src/jest/transforms/babel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

const babelJest = require('babel-jest');
const transformerConfig = require('./transformer_config');

module.exports = babelJest.default.createTransformer(transformerConfig);
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

const babelJest = require('babel-jest');

module.exports = babelJest.default.createTransformer({
module.exports = {
presets: [
[
require.resolve('@kbn/babel-preset/node_preset'),
Expand All @@ -22,4 +20,4 @@ module.exports = babelJest.default.createTransformer({
},
],
],
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EuiIcon, EuiLink } from '@elastic/eui';

import { useDataQualityContext } from '../../data_quality_context';
import { useAddToNewCase } from './hooks/use_add_to_new_case';
import { StyledLinkText } from '../styles';
import { linkTextCss } from '../styles';
import { ADD_TO_NEW_CASE } from '../../translations';

interface Props {
Expand Down Expand Up @@ -42,10 +42,10 @@ const AddToNewCaseActionComponent: React.FC<Props> = ({ markdownComment }) => {
disabled={addToNewCaseDisabled || disableAll}
onClick={addToNewCaseContextMenuOnClick}
>
<StyledLinkText>
<span css={linkTextCss}>
<EuiIcon type="listAdd" />
{ADD_TO_NEW_CASE}
</StyledLinkText>
</span>
</EuiLink>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import React, { FC, useCallback } from 'react';
import { NewChat } from '@kbn/elastic-assistant';
import styled from 'styled-components';
import { css } from '@emotion/react';
import { useEuiTheme } from '@elastic/eui';

import { AssistantIcon } from '@kbn/ai-assistant-icon';
import {
Expand All @@ -19,19 +20,27 @@ import {
import { useDataQualityContext } from '../../data_quality_context';
import { ASK_ASSISTANT } from './translations';

const StyledLinkText = styled.span`
display: flex;
gap: ${({ theme }) => theme.eui.euiSizeXS};
`;
const useStyles = () => {
const { euiTheme } = useEuiTheme();

return {
linkText: css({
display: 'flex',
gap: euiTheme.size.xs,
}),
Comment on lines +27 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using object notation is recommended for 2 reasons:

  1. emotion docs advise to use it for typescript https://emotion.sh/docs/typescript#emotionreact

image

  1. Free Autocomplete of keys and values.

};
};

interface Props {
markdownComment: string;
indexName: string;
}

const ChatActionComponent: FC<Props> = ({ indexName, markdownComment }) => {
const styles = useStyles();
const { isAssistantEnabled } = useDataQualityContext();
const getPromptContext = useCallback(async () => markdownComment, [markdownComment]);

return (
<NewChat
asLink={true}
Expand All @@ -44,10 +53,10 @@ const ChatActionComponent: FC<Props> = ({ indexName, markdownComment }) => {
isAssistantEnabled={isAssistantEnabled}
iconType={null}
>
<StyledLinkText>
<span css={styles.linkText}>
<AssistantIcon />
{ASK_ASSISTANT}
</StyledLinkText>
</span>
</NewChat>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EuiIcon, EuiLink, copyToClipboard } from '@elastic/eui';

import { useDataQualityContext } from '../../data_quality_context';
import { COPIED_RESULTS_TOAST_TITLE, COPY_TO_CLIPBOARD } from '../../translations';
import { StyledLinkText } from '../styles';
import { linkTextCss } from '../styles';

interface Props {
markdownComment: string;
Expand All @@ -33,10 +33,10 @@ const CopyToClipboardActionComponent: React.FC<Props> = ({ markdownComment }) =>
disabled={ilmPhases.length === 0}
onClick={onCopy}
>
<StyledLinkText>
<span css={linkTextCss}>
<EuiIcon type="copyClipboard" />
{COPY_TO_CLIPBOARD}
</StyledLinkText>
</span>
</EuiLink>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* 2.0.
*/

import styled from 'styled-components';
import { UseEuiTheme } from '@elastic/eui';
import { CSSObject } from '@emotion/react';

export const StyledLinkText = styled.span`
display: flex;
gap: ${({ theme }) => theme.eui.euiSizeS};
`;
export const linkTextCss = ({ euiTheme }: UseEuiTheme): CSSObject => ({
display: 'flex',
gap: euiTheme.size.s,
});
Comment on lines +11 to +14
Copy link
Contributor Author

@kapral18 kapral18 Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of defining shared useStyles hooks unnecessarily, I decided to export small nuggets of shared css snippets like this. When passed directly into css={} prop as a function emotion passes in the theme for free so no need for import, also no need to wrap the properties in css() call.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 2.0.
*/

import { EuiEmptyPrompt, EuiSpacer, EuiText, EuiTitle } from '@elastic/eui';
import { EuiEmptyPrompt, EuiSpacer, EuiText, EuiTitle, useEuiTheme } from '@elastic/eui';
import React, { useMemo } from 'react';
import styled from 'styled-components';
import { css } from '@emotion/react';

import {
COLD_DESCRIPTION,
Expand All @@ -23,16 +23,21 @@ import {
} from '../../translations';
import * as i18n from './translations';

const Ul = styled.ul`
text-align: left;
`;

const Li = styled.ul`
margin-bottom: ${({ theme }) => theme.eui.euiSizeS};
text-align: left;
`;
const useStyles = () => {
const { euiTheme } = useEuiTheme();
return {
ul: css({
textAlign: 'left',
}),
li: css({
marginBottom: euiTheme.size.s,
textAlign: 'left',
}),
};
};

const IlmPhasesEmptyPromptComponent: React.FC = () => {
const styles = useStyles();
const title = useMemo(() => <h2>{i18n.TITLE}</h2>, []);
const body = useMemo(() => <p>{i18n.BODY}</p>, []);
const footer = useMemo(
Expand All @@ -44,23 +49,23 @@ const IlmPhasesEmptyPromptComponent: React.FC = () => {

<EuiSpacer size="s" />

<Ul>
<Li>
<ul css={styles.ul}>
<li css={styles.li}>
<strong>{HOT}</strong>
{': '}
{HOT_DESCRIPTION}
</Li>
<Li>
</li>
<li css={styles.li}>
<strong>{WARM}</strong>
{': '}
{WARM_DESCRIPTION}
</Li>
<Li>
</li>
<li css={styles.li}>
<strong>{UNMANAGED}</strong>
{': '}
{UNMANAGED_DESCRIPTION}
</Li>
</Ul>
</li>
</ul>

<EuiSpacer size="m" />

Expand All @@ -74,21 +79,21 @@ const IlmPhasesEmptyPromptComponent: React.FC = () => {

<EuiSpacer size="s" />

<Ul>
<Li>
<ul css={styles.ul}>
<li css={styles.li}>
<strong>{COLD}</strong>
{': '}
{COLD_DESCRIPTION}
</Li>
<Li>
</li>
<li css={styles.li}>
<strong>{FROZEN}</strong>
{': '}
{FROZEN_DESCRIPTION}
</Li>
</Ul>
</li>
</ul>
</div>
),
[]
[styles.li, styles.ul]
);

return <EuiEmptyPrompt body={body} footer={footer} title={title} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@
* 2.0.
*/

import { EuiFlexItem } from '@elastic/eui';
import { EuiFlexItem, useEuiTheme } from '@elastic/eui';
import React, { useState, useCallback, useEffect } from 'react';
import styled from 'styled-components';
import { css } from '@emotion/react';

import { useResultsRollupContext } from '../../contexts/results_rollup_context';
import { Pattern } from './pattern';
import { SelectedIndex } from '../../types';
import { useDataQualityContext } from '../../data_quality_context';
import { useIsHistoricalResultsTourActive } from './hooks/use_is_historical_results_tour_active';

const StyledPatternWrapperFlexItem = styled(EuiFlexItem)`
margin-bottom: ${({ theme }) => theme.eui.euiSize};

&:last-child {
margin-bottom: 0;
}
`;
const useStyles = () => {
const { euiTheme } = useEuiTheme();

return {
patternWrapperFlexItem: css({
marginBottom: euiTheme.size.base,
':last-child': {
marginBottom: 0,
},
}),
};
};

export interface Props {
chartSelectedIndex: SelectedIndex | null;
Expand All @@ -32,6 +37,7 @@ const IndicesDetailsComponent: React.FC<Props> = ({
chartSelectedIndex,
setChartSelectedIndex,
}) => {
const styles = useStyles();
const { patternRollups, patternIndexNames } = useResultsRollupContext();
const { patterns } = useDataQualityContext();

Expand Down Expand Up @@ -73,7 +79,7 @@ const IndicesDetailsComponent: React.FC<Props> = ({
return (
<div data-test-subj="indicesDetails">
{patterns.map((pattern) => (
<StyledPatternWrapperFlexItem grow={false} key={pattern}>
<EuiFlexItem css={styles.patternWrapperFlexItem} grow={false} key={pattern}>
<Pattern
indexNames={patternIndexNames[pattern]}
pattern={pattern}
Expand All @@ -91,7 +97,7 @@ const IndicesDetailsComponent: React.FC<Props> = ({
// when surrounding accordions get toggled and affect the layout
{...(pattern === firstOpenNonEmptyPattern && { openPatternsUpdatedAt })}
/>
</StyledPatternWrapperFlexItem>
</EuiFlexItem>
))}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React, { FC, useEffect, useState } from 'react';
import { EuiButton, EuiButtonEmpty, EuiText, EuiTourStep } from '@elastic/eui';
import styled from 'styled-components';
import { css } from '@emotion/react';

import { HISTORICAL_RESULTS_TOUR_SELECTOR_KEY } from '../constants';
import { CLOSE, INTRODUCING_DATA_QUALITY_HISTORY, TRY_IT, VIEW_PAST_RESULTS } from './translations';
Expand All @@ -20,9 +20,11 @@ export interface Props {
zIndex?: number;
}

const StyledText = styled(EuiText)`
margin-block-start: -10px;
`;
const styles = {
text: css({
marginBlockStart: '-10px',
}),
};

export const HistoricalResultsTour: FC<Props> = ({
anchorSelectorValue,
Expand Down Expand Up @@ -52,9 +54,9 @@ export const HistoricalResultsTour: FC<Props> = ({
return (
<EuiTourStep
content={
<StyledText size="s">
<EuiText css={styles.text} size="s">
<p>{VIEW_PAST_RESULTS}</p>
</StyledText>
</EuiText>
}
data-test-subj="historicalResultsTour"
isStepOpen={isOpen}
Expand Down
Loading
Loading