fix(striker-ui): simplify sensitive text

main
Tsu-ba-me 9 months ago
parent d79599083f
commit 47d0d63a54
  1. 8
      striker-ui/components/FormSummary.tsx
  2. 2
      striker-ui/components/ManageFence/ManageFencePanel.tsx
  3. 116
      striker-ui/components/Text/SensitiveText.tsx
  4. 3
      striker-ui/types/SensitiveText.d.ts

@ -13,13 +13,7 @@ const renderEntryValueWithPassword: RenderFormValueFunction = (args) => {
const { entry, key } = args;
return /passw/i.test(key) ? (
<SensitiveText
revealButtonProps={{ sx: { marginRight: 0, padding: 0 } }}
monospaced
textLineHeight={null}
>
{String(entry)}
</SensitiveText>
<SensitiveText monospaced>{String(entry)}</SensitiveText>
) : (
renderEntryValueWithMono(args)
);

@ -348,7 +348,7 @@ const ManageFencePanel: FC = () => {
current = /passw/i.test(parameterId) ? (
<>
{current}
<SensitiveText inline>{parameterValue}</SensitiveText>
<SensitiveText>{parameterValue}</SensitiveText>
</>
) : (
<>

@ -1,116 +1,50 @@
import { Button, styled } from '@mui/material';
import {
createElement,
FC,
ReactElement,
ReactNode,
useCallback,
useMemo,
useState,
} from 'react';
import { Box, styled } from '@mui/material';
import { createElement, FC, useMemo, useState } from 'react';
import { BORDER_RADIUS, EERIE_BLACK } from '../../lib/consts/DEFAULT_THEME';
import BodyText from './BodyText';
import FlexBox from '../FlexBox';
import IconButton from '../IconButton';
import MonoText from './MonoText';
const InlineButton = styled(Button)({
const BaseStyle = styled(Box)({
backgroundColor: EERIE_BLACK,
borderRadius: BORDER_RADIUS,
minWidth: 'initial',
color: EERIE_BLACK,
display: 'inline-flex',
padding: '0 .6em',
textTransform: 'none',
width: 'fit-content',
':hover': {
backgroundColor: `${EERIE_BLACK}F0`,
':focus': {
color: 'unset',
},
});
const SensitiveText: FC<SensitiveTextProps> = ({
children,
inline: isInline = false,
monospaced: isMonospaced = false,
revealButtonProps,
revealInitially: isRevealInitially = false,
textLineHeight = 2.8,
monospaced = false,
revealInitially = false,
textProps,
}) => {
const [isReveal, setIsReveal] = useState<boolean>(isRevealInitially);
const [reveal, setReveal] = useState<boolean>(revealInitially);
const clickEventHandler = useCallback(() => {
setIsReveal((previous) => !previous);
}, []);
const content = useMemo(() => {
if (typeof children !== 'string') return children;
const textSxLineHeight = useMemo<number | string | undefined>(
() => (isInline ? undefined : textLineHeight || undefined),
[isInline, textLineHeight],
);
const textElementType = useMemo(
() => (isMonospaced ? MonoText : BodyText),
[isMonospaced],
);
const contentElement = useMemo(() => {
let content: ReactNode;
const elementType = monospaced ? MonoText : BodyText;
if (isReveal) {
content =
typeof children === 'string'
? createElement(
textElementType,
{
sx: {
lineHeight: textSxLineHeight,
maxWidth: '20em',
overflowY: 'scroll',
whiteSpace: 'nowrap',
},
...textProps,
},
children,
)
: children;
} else {
content = createElement(
textElementType,
{
sx: {
lineHeight: textSxLineHeight,
},
...textProps,
},
'*****',
);
}
return createElement(elementType, textProps, children);
}, [children, monospaced, textProps]);
return content;
}, [children, isReveal, textElementType, textProps, textSxLineHeight]);
const rootElement = useMemo<ReactElement>(
() =>
isInline ? (
<InlineButton onClick={clickEventHandler}>
{contentElement}
</InlineButton>
) : (
<FlexBox row spacing=".5em">
{contentElement}
<IconButton
edge="end"
mapPreset="visibility"
onClick={clickEventHandler}
state={String(isReveal)}
sx={{ marginRight: '-.2em', padding: '.2em' }}
variant="normal"
{...revealButtonProps}
/>
</FlexBox>
),
[clickEventHandler, contentElement, revealButtonProps, isInline, isReveal],
return (
<BaseStyle
component="div"
onBlur={() => setReveal(false)}
onFocus={() => setReveal(true)}
tabIndex={0}
>
{reveal ? content : '*****'}
</BaseStyle>
);
return rootElement;
};
export default SensitiveText;

@ -1,9 +1,6 @@
type SensitiveTextOptionalProps = {
inline?: boolean;
monospaced?: boolean;
revealButtonProps?: import('../components/IconButton').IconButtonProps;
revealInitially?: boolean;
textLineHeight?: number | null;
textProps?: import('../components/Text').BodyTextProps;
};

Loading…
Cancel
Save