Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions apps/color-picker/src/components/SwatchEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ interface SwatchEditorProps {
swatch: Color;
onChange: (swatch: Color) => void;
onRemove: (swatch: Color) => void;
validationMessage?: string;
}

export default function SwatchEditor({ swatch, onChange, onRemove }: SwatchEditorProps) {
export default function SwatchEditor({
swatch,
onChange,
onRemove,
validationMessage,
}: SwatchEditorProps) {
const {
attributes,
listeners,
Expand All @@ -39,7 +45,7 @@ export default function SwatchEditor({ swatch, onChange, onRemove }: SwatchEdito

return (
<div ref={setNodeRef} style={rowStyle}>
<FormControl marginBottom="spacingM">
<FormControl marginBottom="spacingM" isInvalid={Boolean(validationMessage)}>
<Flex gap={tokens.spacingXs} alignItems="center">
<DragHandle
as="button"
Expand Down Expand Up @@ -79,6 +85,9 @@ export default function SwatchEditor({ swatch, onChange, onRemove }: SwatchEdito
icon={<DeleteIcon variant="muted" />}
/>
</Flex>
{validationMessage ? (
<FormControl.ValidationMessage>{validationMessage}</FormControl.ValidationMessage>
) : null}
</FormControl>
</div>
);
Expand Down
40 changes: 37 additions & 3 deletions apps/color-picker/src/locations/ConfigScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ExternalLinkIcon, PlusIcon } from '@contentful/f36-icons';
import tokens from '@contentful/f36-tokens';
import { useSDK } from '@contentful/react-apps-toolkit';
import { css } from 'emotion';
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import SwatchEditor from '../components/SwatchEditor';
import { AppInstallationParameters, Color } from '../types';

Expand All @@ -49,6 +49,29 @@ const styles = {
}),
};

const DUPLICATE_COLOR_MESSAGE = 'Each color value must be unique.';

function normalizeHexValue(value: string) {
return value.trim().toLowerCase();
}

function getDuplicateColorIds(colors: Color[]) {
const idsByValue = new Map<string, string[]>();

colors.forEach((color) => {
const normalizedValue = normalizeHexValue(color.value);
const ids = idsByValue.get(normalizedValue) ?? [];
ids.push(color.id);
idsByValue.set(normalizedValue, ids);
});

return new Set(
Array.from(idsByValue.values())
.filter((ids) => ids.length > 1)
.flat()
);
}

const ConfigScreen = () => {
const [isInstalled, setIsInstalled] = useState(false);
const [parameters, setParameters] = useState<AppInstallationParameters>({
Expand All @@ -61,6 +84,9 @@ const ConfigScreen = () => {
],
});
const sdk = useSDK<ConfigAppSDK>();
const colors = parameters.themes[0].colors;

const duplicateColorIds = useMemo(() => getDuplicateColorIds(colors), [colors]);

const sensors = useSensors(
useSensor(PointerSensor, {
Expand Down Expand Up @@ -136,13 +162,18 @@ const ConfigScreen = () => {
};

const onConfigure = useCallback(async () => {
if (duplicateColorIds.size > 0) {
sdk.notifier.error('Duplicate color values are not allowed');
return false;
}

const currentState = await sdk.app.getCurrentState();

return {
parameters,
targetState: currentState,
};
}, [parameters, sdk]);
}, [duplicateColorIds, parameters, sdk]);

useEffect(() => {
// its configuration.
Expand Down Expand Up @@ -205,12 +236,15 @@ const ConfigScreen = () => {
<SortableContext
items={parameters.themes[0].colors.map((c) => c.id)}
strategy={verticalListSortingStrategy}>
{parameters.themes[0].colors.map((swatch) => (
{colors.map((swatch) => (
<SwatchEditor
key={swatch.id}
swatch={swatch}
onChange={updateSwatch}
onRemove={removeSwatch}
validationMessage={
duplicateColorIds.has(swatch.id) ? DUPLICATE_COLOR_MESSAGE : undefined
}
/>
))}
</SortableContext>
Expand Down
Loading