2020-06-20 00:18:03 +00:00
|
|
|
// @flow
|
2021-03-13 00:41:26 +00:00
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { CollectionIcon } from "outline-icons";
|
2020-08-09 05:53:59 +00:00
|
|
|
import { getLuminance } from "polished";
|
|
|
|
import * as React from "react";
|
2020-06-20 20:59:15 +00:00
|
|
|
import Collection from "models/Collection";
|
|
|
|
import { icons } from "components/IconPicker";
|
2021-03-13 00:41:26 +00:00
|
|
|
import useStores from "hooks/useStores";
|
2020-06-20 00:18:03 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
collection: Collection,
|
|
|
|
expanded?: boolean,
|
|
|
|
size?: number,
|
|
|
|
};
|
|
|
|
|
2021-03-13 00:41:26 +00:00
|
|
|
function ResolvedCollectionIcon({ collection, expanded, size }: Props) {
|
|
|
|
const { ui } = useStores();
|
|
|
|
|
2020-06-20 00:18:03 +00:00
|
|
|
// If the chosen icon color is very dark then we invert it in dark mode
|
|
|
|
// otherwise it will be impossible to see against the dark background.
|
|
|
|
const color =
|
2020-09-07 18:51:09 +00:00
|
|
|
ui.resolvedTheme === "dark" && collection.color !== "currentColor"
|
2021-10-13 06:02:14 +00:00
|
|
|
? getLuminance(collection.color) > 0.09
|
2020-06-20 00:18:03 +00:00
|
|
|
? collection.color
|
2020-06-20 20:59:15 +00:00
|
|
|
: "currentColor"
|
2020-06-20 00:18:03 +00:00
|
|
|
: collection.color;
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
if (collection.icon && collection.icon !== "collection") {
|
2020-06-20 00:18:03 +00:00
|
|
|
try {
|
|
|
|
const Component = icons[collection.icon].component;
|
|
|
|
return <Component color={color} size={size} />;
|
|
|
|
} catch (error) {
|
2020-06-20 20:59:15 +00:00
|
|
|
console.warn("Failed to render custom icon " + collection.icon);
|
2020-06-20 00:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return <CollectionIcon color={color} expanded={expanded} size={size} />;
|
|
|
|
}
|
|
|
|
|
2021-03-13 00:41:26 +00:00
|
|
|
export default observer(ResolvedCollectionIcon);
|