Skip to content

Commit 9bb1bb8

Browse files
committed
Added support for displaying a split tag if you use key_value or key:value format
1 parent 4f2eac6 commit 9bb1bb8

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed
Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
1+
import { useMemo } from "react";
12
import tagLeftPath from "./tag-left.svg";
23

3-
type Tag = string;
4+
type Tag = string | { key: string; value: string };
45

5-
export function RunTag({ tag }: { tag: Tag }) {
6-
return (
7-
<span className="flex h-6 items-stretch">
8-
<img src={tagLeftPath} alt="" className="block h-full w-[0.5625rem]" />
9-
<span className="flex items-center rounded-r-sm border-y border-r border-charcoal-700 bg-charcoal-800 pr-1.5 text-text-dimmed">
10-
{tag}
6+
export function RunTag({ tag }: { tag: string }) {
7+
const tagResult = useMemo(() => splitTag(tag), [tag]);
8+
9+
if (typeof tagResult === "string") {
10+
return (
11+
<span className="flex h-6 items-stretch">
12+
<img src={tagLeftPath} alt="" className="block h-full w-[0.5625rem]" />
13+
<span className="flex items-center rounded-r-sm border-y border-r border-charcoal-700 bg-charcoal-800 pr-1.5 text-text-dimmed">
14+
{tag}
15+
</span>
16+
</span>
17+
);
18+
} else {
19+
return (
20+
<span className="flex h-6 items-stretch">
21+
<img src={tagLeftPath} alt="" className="block h-full w-[0.5625rem]" />
22+
<span className="flex items-center border-y border-r border-charcoal-700 bg-charcoal-800 pr-1.5 text-text-dimmed">
23+
{tagResult.key}
24+
</span>
25+
<span className="flex items-center rounded-r-sm border-y border-r border-charcoal-700 bg-charcoal-750 px-1.5 text-text-dimmed">
26+
{tagResult.value}
27+
</span>
1128
</span>
12-
</span>
13-
);
29+
);
30+
}
31+
}
32+
33+
/** Takes a string and turns it into a tag
34+
*
35+
* If the string has 12 or fewer alpha characters followed by an underscore or colon then we return an object with a key and value
36+
* Otherwise we return the original string
37+
*/
38+
function splitTag(tag: string): Tag {
39+
if (tag.match(/^[a-zA-Z]{1,12}[_:]/)) {
40+
const components = tag.split(/[_:]/);
41+
if (components.length !== 2) {
42+
return tag;
43+
}
44+
return { key: components[0], value: components[1] };
45+
}
46+
47+
return tag;
1448
}

0 commit comments

Comments
 (0)