1
1
import { Trans } from '@lingui/macro' ;
2
- import { Box , Checkbox , Pill , TagsInput , Text } from '@mantine/core' ;
2
+ import {
3
+ Badge ,
4
+ Box ,
5
+ Checkbox ,
6
+ Group ,
7
+ Pill ,
8
+ TagsInput ,
9
+ Text ,
10
+ } from '@mantine/core' ;
3
11
import { TagFieldType } from '@postybirb/form-builder' ;
4
- import { Tag , TagGroupDto , TagValue } from '@postybirb/types' ;
12
+ import { Tag , TagConverterDto , TagGroupDto , TagValue } from '@postybirb/types' ;
13
+ import { IconArrowRight } from '@tabler/icons-react' ;
5
14
import { flatten , uniq } from 'lodash' ;
15
+ import { useWebsites } from '../../../hooks/account/use-websites' ;
16
+ import { TagConverterStore } from '../../../stores/tag-converter-store' ;
6
17
import { TagGroupStore } from '../../../stores/tag-group-store' ;
7
18
import { useStore } from '../../../stores/use-store' ;
8
19
import { useDefaultOption } from '../hooks/use-default-option' ;
@@ -16,16 +27,45 @@ function containsAllTagsInGroup(tags: Tag[], group: TagGroupDto): boolean {
16
27
return group . tags . every ( ( tag ) => tags . includes ( tag ) ) ;
17
28
}
18
29
19
- // TODO - figure out some way to support indicating a tag converter is being used
30
+ function getTagConversion (
31
+ website : string ,
32
+ tagConverters : TagConverterDto [ ] ,
33
+ tag : Tag ,
34
+ ) : Tag {
35
+ const matchingConverter = tagConverters . find (
36
+ ( converter ) => converter . tag === tag ,
37
+ ) ;
38
+ if ( ! matchingConverter ) {
39
+ return tag ;
40
+ }
41
+
42
+ return (
43
+ matchingConverter . convertTo [ website ] ??
44
+ matchingConverter . convertTo . default ??
45
+ tag
46
+ ) ;
47
+ }
48
+
20
49
export function TagField ( props : FormFieldProps < TagFieldType > ) : JSX . Element {
21
50
const { field, form, propKey, option } = props ;
22
51
const { state : tagGroups } = useStore ( TagGroupStore ) ;
23
52
const defaultOption = useDefaultOption < TagValue > ( props ) ;
24
53
const validations = useValidations ( props ) ;
25
-
54
+ const { state : tagConverters } = useStore ( TagConverterStore ) ;
55
+ const { accounts } = useWebsites ( ) ;
56
+ const account = accounts . find ( ( acc ) => acc . id === option . account ) ;
26
57
const overrideProps = form . getInputProps ( `${ propKey } .overrideDefault` ) ;
27
58
const tagsProps = form . getInputProps ( `${ propKey } .tags` ) ;
28
59
const tagValue = tagsProps . defaultValue as Tag [ ] ;
60
+ const allTags = [ ...tagValue , ...( defaultOption ?. tags || [ ] ) ] ;
61
+ const convertedTags = (
62
+ account
63
+ ? allTags . map ( ( tag ) => [
64
+ tag ,
65
+ getTagConversion ( account . website , tagConverters , tag ) ,
66
+ ] )
67
+ : [ ]
68
+ ) . filter ( ( [ tag , converted ] ) => converted !== tag ) ;
29
69
30
70
const tagGroupsOptions = tagGroups . map ( ( tagGroup ) => ( {
31
71
label : `${ TAG_GROUP_LABEL } ${ JSON . stringify ( tagGroup ) } ` ,
@@ -48,7 +88,7 @@ export function TagField(props: FormFieldProps<TagFieldType>): JSX.Element {
48
88
49
89
const totalTags : number = overrideProps . defaultValue
50
90
? tagValue . length
51
- : [ ... tagValue , ... ( defaultOption ?. tags || [ ] ) ] . length ;
91
+ : allTags . length ;
52
92
53
93
return (
54
94
< Box >
@@ -63,6 +103,25 @@ export function TagField(props: FormFieldProps<TagFieldType>): JSX.Element {
63
103
}
64
104
/>
65
105
) }
106
+ { convertedTags . length > 0 && (
107
+ < Box >
108
+ < Text display = "inline-block" size = "sm" c = "green" >
109
+ < Trans > Converted:</ Trans >
110
+ </ Text >
111
+ < Group display = "inline-block" ml = "4" >
112
+ { convertedTags . map ( ( [ tag , convertedTag ] ) => (
113
+ < Badge size = "xs" variant = "light" >
114
+ { tag } { ' ' }
115
+ < IconArrowRight
116
+ size = "0.75rem"
117
+ style = { { verticalAlign : 'middle' } }
118
+ /> { ' ' }
119
+ { convertedTag }
120
+ </ Badge >
121
+ ) ) }
122
+ </ Group >
123
+ </ Box >
124
+ ) }
66
125
< TagsInput
67
126
inputWrapperOrder = { [ 'label' , 'input' , 'description' , 'error' ] }
68
127
clearable
0 commit comments