|
| 1 | +import { useMemo } from "react"; |
1 | 2 | import tagLeftPath from "./tag-left.svg";
|
2 | 3 |
|
3 |
| -type Tag = string; |
| 4 | +type Tag = string | { key: string; value: string }; |
4 | 5 |
|
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> |
11 | 28 | </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; |
14 | 48 | }
|
0 commit comments