File size: 1,399 Bytes
f1b4bef
 
8201795
 
 
 
0dd9f5a
8201795
f1b4bef
 
 
 
 
0dd9f5a
 
 
 
8201795
 
f1b4bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8201795
f1b4bef
8201795
f1b4bef
8201795
f1b4bef
8201795
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Box, Chip } from "@mui/material";
import { useGame } from "../contexts/GameContext";

/**
 * A component that renders text with styled words (bold, italic, etc.)
 * It automatically handles spacing between words and styled segments
 * It also removes any text enclosed in [ ] or ( )
 */
export function StyledText({ text, ...props }) {
  const { heroName } = useGame();

  if (!text || !heroName) return text;

  // Remove text enclosed in [ ] or ( )
  const cleanedText = text.replace(/\[.*?\]|\(.*?\)/g, "");

  const parts = cleanedText.split(new RegExp(`(${heroName})`, "i"));

  return (
    <Box component="span" sx={{ display: "inline", ...props.sx }}>
      {parts.map((part, index) => {
        if (part.toLowerCase() === heroName.toLowerCase()) {
          return (
            <Chip
              key={index}
              label={part}
              size="small"
              sx={{
                mx: 0.1,
                height: "auto",
                padding: "0px 2px",
                "& .MuiChip-label": {
                  padding: "0 2px",
                  fontSize: "inherit",
                  lineHeight: "inherit",
                  fontWeight: "900",
                },
                backgroundColor: "rgba(255, 255, 255, 0.1)",
              }}
            />
          );
        }
        return part;
      })}
    </Box>
  );
}

export default StyledText;