Skip to content

Commit 07bc8eb

Browse files
committed
Show Cosmo/DMC conversion in table
1 parent 2bbfe35 commit 07bc8eb

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

src/ColorsContext.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {useRequireAnchor} from './RequireAnchorContext'
33
import {useRequireJpCoats} from './RequireJpCoatsContext'
44
import dmcNamedColorCodes from './assets/dmc-color-codes-names.json'
55
import dmcNewJpCoatsColors from './assets/dmc-old-new-jp-coats-colors.json'
6+
import dmcCosmoColors from './assets/dmc-cosmo-colors.json'
67
import {colorCompareFunction, normalizeDmcCode} from './utils'
78
import type {EmbroideryFlossColor} from './types'
89
import {useSort} from './SortContext'
@@ -13,7 +14,7 @@ interface Colors {
1314

1415
const ColorsContext = createContext<Colors | undefined>(undefined)
1516

16-
export const ColorsProvider = ({children}: PropsWithChildren) => {
17+
export function ColorsProvider({children}: PropsWithChildren) {
1718
const {requireAnchor} = useRequireAnchor()
1819
const {requireJpCoats} = useRequireJpCoats()
1920
const {sortOption} = useSort()
@@ -28,8 +29,14 @@ export const ColorsProvider = ({children}: PropsWithChildren) => {
2829
result[key] = {...result[key], ...data}
2930
}
3031
})
32+
dmcCosmoColors.forEach(data => {
33+
const key = normalizeDmcCode(data.dmcCode)
34+
if (result[key]) {
35+
result[key] = {...result[key], ...data}
36+
}
37+
})
3138
return result
32-
}, [dmcNamedColorCodes, dmcNewJpCoatsColors])
39+
}, [])
3340
const colors = useMemo<EmbroideryFlossColor[]>(
3441
() => Object.values(dataByDmcCode),
3542
[dataByDmcCode]
@@ -59,7 +66,7 @@ export const ColorsProvider = ({children}: PropsWithChildren) => {
5966
)
6067
}
6168

62-
export const useColors = () => {
69+
export function useColors() {
6370
const context = useContext(ColorsContext)
6471
if (context === undefined) {
6572
throw new Error('useColors must be used within a ColorsProvider')

src/SortContext.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import type {SortOption} from './types'
99

1010
export const sortOptions: SortOption[] = [
1111
'anchor',
12+
'cosmo',
1213
'dmcCode',
1314
'dmcName',
1415
'jpcNew',
1516
'jpcOld',
1617
]
1718

1819
export const sortLabels: Record<SortOption, string> = {
20+
cosmo: 'Cosmo code',
1921
dmcCode: 'DMC code',
2022
dmcName: 'DMC name',
2123
jpcOld: 'J&P Coats code (old)',
@@ -30,7 +32,7 @@ interface Sort {
3032

3133
const SortContext = createContext<Sort | undefined>(undefined)
3234

33-
export const SortProvider = ({children}: PropsWithChildren) => {
35+
export function SortProvider({children}: PropsWithChildren) {
3436
const [sortOption, setSortOption] = useState<SortOption>('dmcCode')
3537
const contextProps = useMemo(
3638
() => ({sortOption, setSortOption}),
@@ -41,7 +43,7 @@ export const SortProvider = ({children}: PropsWithChildren) => {
4143
)
4244
}
4345

44-
export const useSort = () => {
46+
export function useSort() {
4547
const context = useContext(SortContext)
4648
if (context === undefined)
4749
throw new Error('useSort must be used within a SortProvider')

src/Table.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const Table = ({colors}: TableProps) => {
4949
J&amp;P Coats
5050
</Box>
5151
<th rowSpan={2}>Anchor</th>
52+
<th rowSpan={2}>Cosmo</th>
5253
</tr>
5354
<tr>
5455
<Box as="th" sx={{textAlign: 'left'}}>

src/TableRow.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const emptyCellContent = (
2727
)
2828

2929
export const TableRow = ({
30+
cosmoCode,
3031
dmcCode,
3132
dmcName,
3233
isOdd,
@@ -78,6 +79,7 @@ export const TableRow = ({
7879
{jpCoatsNew ? jpCoatsNew : emptyCellContent}
7980
</Box>
8081
<Box as="td" sx={codeCellStyles}>{anchorCode ? anchorCode : emptyCellContent}</Box>
82+
<Box as="td" sx={codeCellStyles}>{cosmoCode ? cosmoCode : emptyCellContent}</Box>
8183
</Box>
8284
)
8385
}

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface EmbroideryFlossColor {
55
jpCoatsOld?: string
66
jpCoatsNew?: string
77
anchorCode?: string
8+
cosmoCode?: string
89
}
910

10-
export type SortOption = 'dmcCode' | 'dmcName' | 'jpcOld' | 'anchor' | 'jpcNew'
11+
export type SortOption = 'dmcCode' | 'dmcName' | 'jpcOld' | 'anchor' | 'jpcNew' | 'cosmo'

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ const numericishStringCompare = (
4242
return a.localeCompare(b)
4343
}
4444

45+
function cosmoCodeCompare(
46+
{cosmoCode: cosmoCode1}: EmbroideryFlossColor,
47+
{cosmoCode: cosmoCode2}: EmbroideryFlossColor
48+
) {
49+
return numericishStringCompare(cosmoCode1, cosmoCode2)
50+
}
51+
4552
const dmcCodeCompare = (
4653
{dmcCode: dmcCode1}: EmbroideryFlossColor,
4754
{dmcCode: dmcCode2}: EmbroideryFlossColor
@@ -84,5 +91,6 @@ export const colorCompareFunction = (sortOption: SortOption) => {
8491
if (sortOption === 'jpcOld') return jpCoatsOldCompare
8592
if (sortOption === 'anchor') return anchorCodeCompare
8693
if (sortOption === 'jpcNew') return jpCoatsNewCompare
94+
if (sortOption === 'cosmo') return cosmoCodeCompare
8795
return dmcCodeCompare
8896
}

0 commit comments

Comments
 (0)