Skip to content

Commit cd3b62f

Browse files
committed
add support for text color for text component event
1 parent 41bde70 commit cd3b62f

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

backend/models/event.go

+36-4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ func (ts TextSize) IsValid() bool {
6060
}
6161
}
6262

63+
// TextColor Represents text color for the text component.
64+
type TextColor string
65+
66+
const (
67+
TextNormal TextColor = "NORMAL"
68+
TextMuted TextColor = "MUTED"
69+
TextWarning TextColor = "WARNING"
70+
TextError TextColor = "ERROR"
71+
TextSuccess TextColor = "SUCCESS"
72+
)
73+
74+
func (tc TextColor) IsValid() bool {
75+
switch tc {
76+
case TextNormal, TextMuted, TextWarning, TextError, TextSuccess:
77+
return true
78+
default:
79+
return false
80+
}
81+
}
82+
6383
type SpacerSize string
6484

6585
const (
@@ -127,8 +147,9 @@ func (bc BadgeColor) IsValid() bool {
127147

128148
// ComponentText represents the component text in event components.
129149
type ComponentText struct {
130-
Text string `json:"text"`
131-
TextSize TextSize `json:"textSize"`
150+
Text string `json:"text"`
151+
TextSize TextSize `json:"textSize"`
152+
TextColor TextColor `json:"textColor"`
132153
}
133154

134155
// UnmarshalJSON implements the json.Unmarshaler interface for ComponentText.
@@ -138,8 +159,9 @@ type ComponentText struct {
138159
// Returns an error if JSON unmarshal fails or if textSize is invalid.
139160
func (ct *ComponentText) UnmarshalJSON(data []byte) error {
140161
type Aux struct {
141-
Text string `json:"text"`
142-
TextSize string `json:"textSize"`
162+
Text string `json:"text"`
163+
TextSize string `json:"textSize"`
164+
TextColor string `json:"textColor"`
143165
}
144166

145167
var aux Aux
@@ -158,6 +180,16 @@ func (ct *ComponentText) UnmarshalJSON(data []byte) error {
158180
}
159181
ct.TextSize = ts
160182
}
183+
// Set default value to 'NORMAL' if textColor is empty
184+
if aux.TextColor == "" {
185+
ct.TextColor = TextNormal
186+
} else {
187+
tc := TextColor(aux.TextColor)
188+
if !tc.IsValid() {
189+
return fmt.Errorf("invalid textColor value: %s", aux.TextColor)
190+
}
191+
ct.TextColor = tc
192+
}
161193
return nil
162194
}
163195

frontend/src/components/event/components.tsx

+23-4
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,36 @@ import Markdown from "react-markdown";
1919

2020
type TextSize = "L" | "M" | "S" | "XS";
2121

22+
type TextColor = "ERROR" | "MUTED" | "NORMAL" | "SUCCESS" | "WARNING";
23+
2224
interface ComponentTextProps {
2325
text: string;
26+
textColor?: TextColor;
2427
textSize?: TextSize;
2528
}
2629

27-
// Renders markdown text component.
28-
export function ComponentText({ text, textSize = "S" }: ComponentTextProps) {
30+
// Renders Markdown text component.
31+
export function ComponentText({
32+
text,
33+
textColor = "NORMAL",
34+
textSize = "S",
35+
}: ComponentTextProps) {
36+
2937
const sizeMap: Record<TextSize, string> = {
3038
L: "text-lg",
3139
M: "text-base",
3240
S: "text-sm",
3341
XS: "text-xs",
3442
};
3543

44+
const colorMap: Record<TextColor, string> = {
45+
ERROR: "text-red-600 dark:text-red-400",
46+
MUTED: "text-muted-foreground",
47+
NORMAL: "text-gray-900 dark:text-gray-100",
48+
SUCCESS: "text-green-600 dark:text-green-400",
49+
WARNING: "text-yellow-600 dark:text-yellow-400",
50+
};
51+
3652
return (
3753
<Markdown
3854
components={{
@@ -46,7 +62,11 @@ export function ComponentText({ text, textSize = "S" }: ComponentTextProps) {
4662
h3: ({ children }) => <h3 className="text-base">{children}</h3>,
4763
h4: ({ children }) => <h4 className="text-base">{children}</h4>,
4864
p: ({ children }) => (
49-
<p className={sizeMap[textSize] || "text-sm"}>{children}</p>
65+
<p
66+
className={cn(sizeMap[textSize] || "text-sm", colorMap[textColor])}
67+
>
68+
{children}
69+
</p>
5070
),
5171
pre: ({ children }) => (
5272
<pre className="max-w-full overflow-auto whitespace-pre-wrap break-words">
@@ -259,7 +279,6 @@ export function RenderComponents({ components = [] }: RenderComponentsProps) {
259279
console.warn(`Unknown component type: ${componentType}`);
260280
return null;
261281
}
262-
263282
return <Component key={index} {...props} />;
264283
};
265284

frontend/src/db/schema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export type PatResponse = z.infer<typeof patResponseSchema>;
207207
const ComponentText = z.object({
208208
componentText: z.object({
209209
text: z.string(),
210+
textColor: z.string(),
210211
textSize: z.string(),
211212
}),
212213
});

0 commit comments

Comments
 (0)