@@ -27,9 +27,14 @@ export const COLORS_LIST = [
27
27
] ;
28
28
29
29
// @ts -expect-error
30
- export const getRandColor = ( usedColors ) => {
30
+ export const getRandColor = ( usedColors ? ) => {
31
31
const availableColors = COLORS_LIST . filter (
32
- ( color ) => ! usedColors . includes ( color )
32
+ ( color ) => {
33
+ if ( usedColors . length > 0 ) {
34
+ return ! usedColors . includes ( color ) ;
35
+ }
36
+ return true ;
37
+ }
33
38
) ;
34
39
return (
35
40
availableColors [ Math . floor ( Math . random ( ) * availableColors . length ) ] ||
@@ -105,46 +110,52 @@ const getClosestGradeEmoji = (subjectName) => {
105
110
106
111
// @ts -expect-error
107
112
export const getSubjectData = ( entry ) => {
108
- const state = useCurrentAccount . getState ( ) ;
109
- const { account, mutateProperty } = state ;
110
- const subject = entry
111
- . trim ( )
112
- . toLowerCase ( )
113
- . normalize ( "NFD" )
114
- . replace ( / [ \u0300 - \u036f ] / g, "" ) ;
115
-
116
- if ( ! subject ) {
117
- return { color : "#888888" , pretty : "Matière inconnue" , emoji : "❓" } ;
118
- }
119
-
120
- const allSubjects = account ?. personalization ?. subjects || { } ;
121
-
122
- // Check if the subject already exists
123
- const existingSubject = allSubjects [ subject ] ;
124
- if ( existingSubject ) {
125
- return existingSubject ;
113
+ try {
114
+ const state = useCurrentAccount . getState ( ) ;
115
+ const { account, mutateProperty } = state ;
116
+ const subject = entry
117
+ . trim ( )
118
+ . toLowerCase ( )
119
+ . normalize ( "NFD" )
120
+ . replace ( / [ \u0300 - \u036f ] / g, "" ) ;
121
+
122
+ if ( ! subject ) {
123
+ return { color : "#888888" , pretty : "Matière inconnue" , emoji : "❓" } ;
124
+ }
125
+
126
+ const allSubjects = account ?. personalization ?. subjects || { } ;
127
+
128
+ // Check if the subject already exists
129
+ const existingSubject = allSubjects [ subject ] ;
130
+ if ( existingSubject ) {
131
+ return existingSubject ;
132
+ }
133
+
134
+ const formattedCoursName = findObjectByPronoteString ( subject ) ;
135
+ const usedColors = new Set (
136
+ Object . values ( allSubjects ) . map ( ( subj ) => subj . color )
137
+ ) ;
138
+ const color = getRandColor ( Array . from ( usedColors ) ) ;
139
+ const emoji = getClosestGradeEmoji ( subject ) ;
140
+
141
+ const newSubject = { color, pretty : formattedCoursName . pretty , emoji } ;
142
+
143
+ // Check for existing subject with the same pretty name
144
+ const existing = Object . values ( allSubjects ) . find (
145
+ ( subj ) => subj . pretty === formattedCoursName . pretty
146
+ ) ;
147
+ if ( existing ) {
148
+ return existing ;
149
+ }
150
+
151
+ mutateProperty ( "personalization" , {
152
+ subjects : { ...allSubjects , [ subject ] : newSubject } ,
153
+ } ) ;
154
+
155
+ return newSubject ;
126
156
}
127
-
128
- const formattedCoursName = findObjectByPronoteString ( subject ) ;
129
- const usedColors = new Set (
130
- Object . values ( allSubjects ) . map ( ( subj ) => subj . color )
131
- ) ;
132
- const color = getRandColor ( Array . from ( usedColors ) ) ;
133
- const emoji = getClosestGradeEmoji ( subject ) ;
134
-
135
- const newSubject = { color, pretty : formattedCoursName . pretty , emoji } ;
136
-
137
- // Check for existing subject with the same pretty name
138
- const existing = Object . values ( allSubjects ) . find (
139
- ( subj ) => subj . pretty === formattedCoursName . pretty
140
- ) ;
141
- if ( existing ) {
142
- return existing ;
157
+ catch ( error ) {
158
+ console . error ( "Error in getSubjectData:" , error ) ;
159
+ return { color : getRandColor ( ) , pretty : entry . toString ( ) , emoji : getClosestGradeEmoji ( entry ) } ;
143
160
}
144
-
145
- mutateProperty ( "personalization" , {
146
- subjects : { ...allSubjects , [ subject ] : newSubject } ,
147
- } ) ;
148
-
149
- return newSubject ;
150
161
} ;
0 commit comments