feat: refactor tag color logic
This commit is contained in:
parent
169c846cdb
commit
f6aec6138e
3 changed files with 40 additions and 31 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { Item, Tag } from "../api";
|
import { Item, Tag } from "../api";
|
||||||
|
import { getTagColor } from "../utils/tagColors";
|
||||||
|
|
||||||
interface ItemRowProps {
|
interface ItemRowProps {
|
||||||
item: Item;
|
item: Item;
|
||||||
|
|
@ -10,19 +11,6 @@ interface ItemRowProps {
|
||||||
onDeleteItem: (itemId: string) => void;
|
onDeleteItem: (itemId: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TAG_COLORS = [
|
|
||||||
"bg-blue-200 text-blue-900",
|
|
||||||
"bg-green-200 text-green-900",
|
|
||||||
"bg-yellow-200 text-yellow-900",
|
|
||||||
"bg-pink-200 text-pink-900",
|
|
||||||
"bg-purple-200 text-purple-900",
|
|
||||||
"bg-orange-200 text-orange-900",
|
|
||||||
"bg-teal-200 text-teal-900",
|
|
||||||
"bg-red-200 text-red-900",
|
|
||||||
"bg-indigo-200 text-indigo-900",
|
|
||||||
"bg-lime-200 text-lime-900",
|
|
||||||
];
|
|
||||||
|
|
||||||
export default function ItemRow({
|
export default function ItemRow({
|
||||||
item,
|
item,
|
||||||
allTags,
|
allTags,
|
||||||
|
|
@ -106,7 +94,11 @@ export default function ItemRow({
|
||||||
{item.tags.map((tag) => (
|
{item.tags.map((tag) => (
|
||||||
<span
|
<span
|
||||||
key={tag.id}
|
key={tag.id}
|
||||||
className={`${getTagColor(tag.id)} text-sm rounded px-1 py-0.5 flex items-center`}
|
className={
|
||||||
|
tag.mandatory
|
||||||
|
? "border-2 border-yellow-400 text-sm rounded px-1 py-0.5 flex items-center"
|
||||||
|
: `${getTagColor(tag.id)} text-sm rounded px-1 py-0.5 flex items-center`
|
||||||
|
}
|
||||||
>
|
>
|
||||||
#{tag.name}
|
#{tag.name}
|
||||||
{hover && (
|
{hover && (
|
||||||
|
|
@ -185,12 +177,3 @@ export default function ItemRow({
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTagColor(tagId: string) {
|
|
||||||
// Simple hash: sum char codes
|
|
||||||
let hash = 0;
|
|
||||||
for (let i = 0; i < tagId.length; i++) {
|
|
||||||
hash = (hash + tagId.charCodeAt(i)) % TAG_COLORS.length;
|
|
||||||
}
|
|
||||||
return TAG_COLORS[hash];
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import {
|
||||||
} from "../api";
|
} from "../api";
|
||||||
import ItemList from "../components/ItemList";
|
import ItemList from "../components/ItemList";
|
||||||
import TagFilter from "../components/TagFilter";
|
import TagFilter from "../components/TagFilter";
|
||||||
|
import { getTagColor } from "../utils/tagColors";
|
||||||
|
|
||||||
async function fetchTrips(): Promise<{ id: string; name: string }[]> {
|
async function fetchTrips(): Promise<{ id: string; name: string }[]> {
|
||||||
const res = await fetch("http://localhost:8000/trips/");
|
const res = await fetch("http://localhost:8000/trips/");
|
||||||
|
|
@ -179,19 +180,15 @@ export default function ItemsPage() {
|
||||||
<span
|
<span
|
||||||
key={tag.id}
|
key={tag.id}
|
||||||
className={
|
className={
|
||||||
"bg-gray-200 text-sm rounded px-1 py-0.5 flex items-center cursor-pointer " +
|
tag.mandatory
|
||||||
(newItemTags.includes(tag.id)
|
? "border-2 border-yellow-400 text-sm rounded px-1 py-0.5 flex items-center cursor-pointer"
|
||||||
? "bg-blue-200 text-blue-900 font-bold"
|
: `${getTagColor(tag.id)} text-sm rounded px-1 py-0.5 flex items-center cursor-pointer`
|
||||||
: "") +
|
+ (newItemTags.includes(tag.id) ? " ring-2 ring-blue-400 font-bold" : "")
|
||||||
(tag.mandatory ? " border-2 border-yellow-400" : "")
|
|
||||||
}
|
}
|
||||||
onClick={() => toggleNewItemTag(tag.id)}
|
onClick={() => toggleNewItemTag(tag.id)}
|
||||||
title={tag.mandatory ? "Pflicht-Tag (mandatory)" : ""}
|
title={tag.mandatory ? "Pflicht-Tag (mandatory)" : ""}
|
||||||
>
|
>
|
||||||
#{tag.name}
|
#{tag.name}
|
||||||
{tag.mandatory && (
|
|
||||||
<span className="ml-1 text-yellow-500 font-bold" title="Pflicht-Tag">*</span>
|
|
||||||
)}
|
|
||||||
</span>
|
</span>
|
||||||
))}
|
))}
|
||||||
{/* Hinweis, falls Trip gewählt */}
|
{/* Hinweis, falls Trip gewählt */}
|
||||||
|
|
|
||||||
29
frontend/src/utils/tagColors.ts
Normal file
29
frontend/src/utils/tagColors.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
export const TAG_COLORS = [
|
||||||
|
"bg-blue-200 text-blue-900",
|
||||||
|
"bg-green-200 text-green-900",
|
||||||
|
"bg-yellow-200 text-yellow-900",
|
||||||
|
"bg-pink-200 text-pink-900",
|
||||||
|
"bg-purple-200 text-purple-900",
|
||||||
|
"bg-orange-200 text-orange-900",
|
||||||
|
"bg-teal-200 text-teal-900",
|
||||||
|
"bg-red-200 text-red-900",
|
||||||
|
"bg-indigo-200 text-indigo-900",
|
||||||
|
"bg-lime-200 text-lime-900",
|
||||||
|
"bg-cyan-200 text-cyan-900",
|
||||||
|
"bg-amber-200 text-amber-900",
|
||||||
|
"bg-fuchsia-200 text-fuchsia-900",
|
||||||
|
"bg-rose-200 text-rose-900",
|
||||||
|
"bg-emerald-200 text-emerald-900",
|
||||||
|
"bg-violet-200 text-violet-900",
|
||||||
|
"bg-sky-200 text-sky-900",
|
||||||
|
"bg-orange-100 text-orange-800",
|
||||||
|
"bg-green-100 text-green-800",
|
||||||
|
];
|
||||||
|
|
||||||
|
export function getTagColor(tagId: string) {
|
||||||
|
let hash = 0;
|
||||||
|
for (let i = 0; i < tagId.length; i++) {
|
||||||
|
hash = (hash + tagId.charCodeAt(i)) % TAG_COLORS.length;
|
||||||
|
}
|
||||||
|
return TAG_COLORS[hash];
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue