1
|
import React, { createContext, useContext, useEffect, useState } from 'react';
|
2
|
import { SubTagInfo, TagCategoryInfo, TagInfo } from '../api';
|
3
|
import { tagController } from '../controllers';
|
4
|
import { LoggedUserContext } from './LoggedUserContext';
|
5
|
import Annotation from '../pages/annotation/[id]';
|
6
|
import { AnnotationContext } from './AnnotationContext';
|
7
|
|
8
|
/**
|
9
|
* Interface of a tag context provider.
|
10
|
*/
|
11
|
interface ITagCategoryContextProvider {
|
12
|
/**
|
13
|
* Categories managed by the context.
|
14
|
*/
|
15
|
tagCategories: TagCategoryInfo[] | null;
|
16
|
|
17
|
/**
|
18
|
* Selected tag managed by the context.
|
19
|
*/
|
20
|
selectedTag: TagInfo | null;
|
21
|
|
22
|
/**
|
23
|
* Selected sub tag managed by the context.
|
24
|
*/
|
25
|
selectedSubTag: SubTagInfo | null;
|
26
|
|
27
|
/**
|
28
|
* Sets new tags.
|
29
|
* @param newTags An array of new tags.
|
30
|
*/
|
31
|
setTagCategories: (newTagCategories: TagCategoryInfo[] | null) => void;
|
32
|
|
33
|
/**
|
34
|
* Selects a tag.
|
35
|
* @param tag Tag.
|
36
|
* @param subTag Sub tag.
|
37
|
*/
|
38
|
selectTag: (tag: TagInfo, subTag: SubTagInfo | null) => void;
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* The tag context that manages available tags.
|
43
|
*/
|
44
|
export const TagCategoryContext = createContext<ITagCategoryContextProvider>({
|
45
|
/**
|
46
|
* Default categories.
|
47
|
*/
|
48
|
tagCategories: null,
|
49
|
|
50
|
/**
|
51
|
* Default selected tag.
|
52
|
*/
|
53
|
selectedTag: null,
|
54
|
|
55
|
/**
|
56
|
* Default selected sub tag.
|
57
|
*/
|
58
|
selectedSubTag: null,
|
59
|
|
60
|
/**
|
61
|
* Default implementation of setTags method.
|
62
|
* @param c Array of new tags.
|
63
|
*/
|
64
|
setTagCategories: (c) => {
|
65
|
return;
|
66
|
},
|
67
|
|
68
|
/**
|
69
|
* Default implementation of selectTag.
|
70
|
* @param t Tag.
|
71
|
* @param s Sub tag.
|
72
|
*/
|
73
|
selectTag: (t: TagInfo, s: SubTagInfo | null) => {
|
74
|
return;
|
75
|
},
|
76
|
});
|
77
|
|
78
|
/**
|
79
|
* Provider of the tag context.
|
80
|
* @param props Children that should have access to the tag context.
|
81
|
* @returns Prepared html of the provider.
|
82
|
*/
|
83
|
const TagCategoryProvider = (props: { children: React.ReactNode }) => {
|
84
|
const { markSelectedText } = useContext(AnnotationContext);
|
85
|
|
86
|
/**
|
87
|
* Tags managed by the context.
|
88
|
*/
|
89
|
const [tagCategories, setTagCategories] = useState<TagCategoryInfo[] | null>(null);
|
90
|
|
91
|
/**
|
92
|
* Selected tag.
|
93
|
*/
|
94
|
const [selectedTag, setSelectedTag] = useState<TagInfo | null>(null);
|
95
|
|
96
|
/**
|
97
|
* Selected sub tag.
|
98
|
*/
|
99
|
const [selectedSubTag, setSelectedSubTag] = useState<SubTagInfo | null>(null);
|
100
|
|
101
|
/**
|
102
|
* Selects a tag.
|
103
|
*/
|
104
|
const selectTag = (tag: TagInfo, subTag: SubTagInfo | null) => {
|
105
|
setSelectedTag(tag);
|
106
|
setSelectedSubTag(subTag);
|
107
|
|
108
|
if (!tag.id) {
|
109
|
console.log('invalid selection');
|
110
|
return;
|
111
|
}
|
112
|
|
113
|
markSelectedText(tag.id, subTag?.id ?? null, null);
|
114
|
};
|
115
|
|
116
|
/**
|
117
|
* Initializes the context.
|
118
|
*/
|
119
|
useEffect(() => {
|
120
|
tagController.tagsGet().then((tagTree) => {
|
121
|
if (typeof tagTree.data.tagCategories != 'undefined') {
|
122
|
setTagCategories(tagTree.data.tagCategories);
|
123
|
}
|
124
|
});
|
125
|
}, []);
|
126
|
|
127
|
return (
|
128
|
<TagCategoryContext.Provider
|
129
|
value={{
|
130
|
tagCategories,
|
131
|
selectedTag,
|
132
|
selectedSubTag,
|
133
|
setTagCategories,
|
134
|
selectTag,
|
135
|
}}
|
136
|
>
|
137
|
{props.children}
|
138
|
</TagCategoryContext.Provider>
|
139
|
);
|
140
|
};
|
141
|
|
142
|
export default TagCategoryProvider;
|