mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-20 10:30:16 -04:00
added useTagsHandler custom hook to consolidate getTagColor and presetOptions state into one function
This commit is contained in:
parent
3a2dd69332
commit
598b9dd784
|
@ -7,34 +7,18 @@ import { Bars2Icon, BookmarkIcon } from "@heroicons/react/24/solid";
|
||||||
import { ColumnDef, createColumnHelper } from "@tanstack/react-table";
|
import { ColumnDef, createColumnHelper } from "@tanstack/react-table";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { DataPoint } from "@/components/Table/Table";
|
import { DataPoint } from "@/components/Table/Table";
|
||||||
|
import useTagsHandler from "@/components/TagsInput/TagsHandler";
|
||||||
|
|
||||||
export function ResourceTable({ resources }: { resources: Resource[] }) {
|
export function ResourceTable({ resources }: { resources: Resource[] }) {
|
||||||
const columnHelper = createColumnHelper<Resource>();
|
const columnHelper = createColumnHelper<Resource>();
|
||||||
const [data, setData] = useState<DataPoint[]>([...resources]);
|
const [data, setData] = useState<DataPoint[]>([...resources]);
|
||||||
|
|
||||||
const [presetOptions, setPresetOptions] = useState([
|
const { presetOptions, setPresetOptions, getTagColor } = useTagsHandler([
|
||||||
"administrator",
|
"administrator",
|
||||||
"volunteer",
|
"volunteer",
|
||||||
"employee",
|
"employee",
|
||||||
]);
|
])
|
||||||
const [tagColors, setTagColors] = useState(new Map());
|
|
||||||
|
|
||||||
const getTagColor = (tag: string) => {
|
|
||||||
if (!tagColors.has(tag)) {
|
|
||||||
const colors = [
|
|
||||||
"bg-cyan-100",
|
|
||||||
"bg-blue-100",
|
|
||||||
"bg-green-100",
|
|
||||||
"bg-yellow-100",
|
|
||||||
"bg-purple-100",
|
|
||||||
];
|
|
||||||
const randomColor =
|
|
||||||
colors[Math.floor(Math.random() * colors.length)];
|
|
||||||
setTagColors(new Map(tagColors).set(tag, randomColor));
|
|
||||||
}
|
|
||||||
return tagColors.get(tag);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleRowUpdate = (updatedRow: Resource) => {
|
const handleRowUpdate = (updatedRow: Resource) => {
|
||||||
const dataIndex = data.findIndex((row) => row.id === updatedRow.id);
|
const dataIndex = data.findIndex((row) => row.id === updatedRow.id);
|
||||||
if (dataIndex !== -1) {
|
if (dataIndex !== -1) {
|
||||||
|
|
27
compass/components/TagsInput/TagsHandler.tsx
Normal file
27
compass/components/TagsInput/TagsHandler.tsx
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
export default function useTagsHandler(initialOptions: string[]) {
|
||||||
|
const [presetOptions, setPresetOptions] = useState(initialOptions);
|
||||||
|
const [tagColors, setTagColors] = useState(new Map<string, string>());
|
||||||
|
|
||||||
|
const getTagColor = (tag: string): string => {
|
||||||
|
if (!tagColors.has(tag)) {
|
||||||
|
const colors = [
|
||||||
|
"bg-cyan-100",
|
||||||
|
"bg-blue-100",
|
||||||
|
"bg-green-100",
|
||||||
|
"bg-yellow-100",
|
||||||
|
"bg-purple-100",
|
||||||
|
];
|
||||||
|
const randomColor =
|
||||||
|
colors[Math.floor(Math.random() * colors.length)];
|
||||||
|
setTagColors(new Map(tagColors).set(tag, randomColor));
|
||||||
|
return randomColor;
|
||||||
|
}
|
||||||
|
// Since we populate any missing keys, .get will never return undefined,
|
||||||
|
// so we are safe to typecast to prevent a type error
|
||||||
|
return tagColors.get(tag) as string;
|
||||||
|
};
|
||||||
|
|
||||||
|
return { presetOptions, setPresetOptions, getTagColor }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user