From d4dd4ea8846b73a3433e6f4161c8d2e2bcd322b6 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 3 May 2025 23:03:54 -0400 Subject: [PATCH] Pick collection color based on existing collections --- app/components/Collection/CollectionForm.tsx | 28 ++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/app/components/Collection/CollectionForm.tsx b/app/components/Collection/CollectionForm.tsx index 8e308d0927..b1b5c3b159 100644 --- a/app/components/Collection/CollectionForm.tsx +++ b/app/components/Collection/CollectionForm.tsx @@ -1,3 +1,4 @@ +import uniq from "lodash/uniq"; import { observer } from "mobx-react"; import * as React from "react"; import { Controller, useForm } from "react-hook-form"; @@ -18,6 +19,7 @@ import Switch from "~/components/Switch"; import Text from "~/components/Text"; import useBoolean from "~/hooks/useBoolean"; import useCurrentTeam from "~/hooks/useCurrentTeam"; +import useStores from "~/hooks/useStores"; import { EmptySelectValue } from "~/types"; const IconPicker = React.lazy(() => import("~/components/IconPicker")); @@ -30,6 +32,26 @@ export interface FormData { permission: CollectionPermission | undefined; } +const useIconColor = (collection?: Collection) => { + const { collections } = useStores(); + const hasMultipleCollections = collections.orderedData.length > 1; + const collectionColors = uniq( + collections.orderedData.map((c) => c.color).filter(Boolean) + ) as string[]; + + const iconColor = React.useMemo( + () => + collection?.color ?? + // If all the existing collections have the same color, use that color, + // otherwise pick a random color from the palette + (hasMultipleCollections && collectionColors.length === 1 + ? collectionColors[0] + : randomElement(colorPalette)), + [collection?.color] + ); + return iconColor; +}; + export const CollectionForm = observer(function CollectionForm_({ handleSubmit, collection, @@ -42,11 +64,7 @@ export const CollectionForm = observer(function CollectionForm_({ const [hasOpenedIconPicker, setHasOpenedIconPicker] = useBoolean(false); - const iconColor = React.useMemo( - () => collection?.color ?? randomElement(colorPalette), - [collection?.color] - ); - + const iconColor = useIconColor(collection); const fallbackIcon = ; const {