1- import AdminForth , { AdminForthPlugin , Filters , suggestIfTypo , AdminForthDataTypes , RAMLock } from "adminforth" ;
1+ import AdminForth , { AdminForthPlugin , Filters , suggestIfTypo , AdminForthDataTypes , RAMLock , filtersTools , AdminForthFilterOperators } from "adminforth" ;
22import type { IAdminForth , IHttpServer , AdminForthComponentDeclaration , AdminForthResourceColumn , AdminForthResource , BeforeLoginConfirmationFunction , AdminForthConfigMenuItem , AdminUser } from "adminforth" ;
33import type { PluginOptions , SupportedLanguage } from './types.js' ;
44import iso6391 from 'iso-639-1' ;
@@ -481,12 +481,11 @@ export default class I18nPlugin extends AdminForthPlugin {
481481 if ( ! resourceConfig . options . pageInjections . list ) {
482482 resourceConfig . options . pageInjections . list = { } ;
483483 }
484- if ( ! resourceConfig . options . pageInjections . list . beforeActionButtons ) {
485- resourceConfig . options . pageInjections . list . beforeActionButtons = [ ] ;
484+ if ( ! resourceConfig . options . pageInjections . list . threeDotsDropdownItems ) {
485+ resourceConfig . options . pageInjections . list . threeDotsDropdownItems = [ ] ;
486486 }
487487
488- ( resourceConfig . options . pageInjections . list . beforeActionButtons as AdminForthComponentDeclaration [ ] ) . push ( pageInjection ) ;
489-
488+ ( resourceConfig . options . pageInjections . list . threeDotsDropdownItems as AdminForthComponentDeclaration [ ] ) . push ( pageInjection ) ;
490489
491490 // if there is menu item with resourceId, add .badge function showing number of untranslated strings
492491 const addBadgeCountToMenuItem = ( menuItem : AdminForthConfigMenuItem ) => {
@@ -658,7 +657,7 @@ export default class I18nPlugin extends AdminForthPlugin {
658657
659658 }
660659
661- async translateToLang (
660+ async getTranslateToLangTasks (
662661 langIsoCode : SupportedLanguage ,
663662 strings : { en_string : string , category : string } [ ] ,
664663 plurals = false ,
@@ -754,8 +753,9 @@ export default class I18nPlugin extends AdminForthPlugin {
754753 \`\`\`` ;
755754 const stringBanchCopy = [ ...stringBanch ] ;
756755 generationTasksInitialData . push (
757- {
756+ {
758757 state : {
758+ taskName : `Translate ${ strings . length } strings` ,
759759 prompt : promptToGenerate ,
760760 strings : strings . filter ( s => stringBanchCopy . includes ( s . en_string ) ) ,
761761 translations : translations . filter ( t => stringBanchCopy . includes ( t . en_string ) ) ,
@@ -826,10 +826,10 @@ export default class I18nPlugin extends AdminForthPlugin {
826826 async ( [ lang , strings ] : [ SupportedLanguage , { en_string : string , category : string } [ ] ] ) => {
827827 // first translate without plurals
828828 const stringsWithoutPlurals = strings . filter ( s => ! s . en_string . includes ( '|' ) ) ;
829- const noPluralTranslationsTasks = await this . translateToLang ( lang , stringsWithoutPlurals , false , translations , updateStrings , needToTranslateByLang , adminUser ) ;
829+ const noPluralTranslationsTasks = await this . getTranslateToLangTasks ( lang , stringsWithoutPlurals , false , translations , updateStrings , needToTranslateByLang , adminUser ) ;
830830
831831 const stringsWithPlurals = strings . filter ( s => s . en_string . includes ( '|' ) ) ;
832- const pluralTranslationsTasks = await this . translateToLang ( lang , stringsWithPlurals , true , translations , updateStrings , needToTranslateByLang , adminUser ) ;
832+ const pluralTranslationsTasks = await this . getTranslateToLangTasks ( lang , stringsWithPlurals , true , translations , updateStrings , needToTranslateByLang , adminUser ) ;
833833 generationTasksInitialData = generationTasksInitialData . concat ( noPluralTranslationsTasks || [ ] ) . concat ( pluralTranslationsTasks || [ ] ) ;
834834 }
835835 )
@@ -914,6 +914,7 @@ export default class I18nPlugin extends AdminForthPlugin {
914914 //handler function
915915 handler : async ( { jobId, setTaskStateField, getTaskStateField } ) => {
916916 const initialState : {
917+ taskName : string ,
917918 prompt ?: string ,
918919 strings ?: { en_string : string , category : string } [ ] ,
919920 translations ?: any [ ] ,
@@ -938,16 +939,19 @@ export default class I18nPlugin extends AdminForthPlugin {
938939 ) ;
939940 }
940941 afLogger . debug ( `Translation task for language ${ initialState . lang } completed.` ) ;
941- if ( initialState . failedToTranslate . length > 0 ) {
942- afLogger . error ( `Failed to translate some strings for language ${ initialState . lang } in plugin ${ this . constructor . name } :, ${ initialState . failedToTranslate } ` ) ;
943- }
942+
944943 const stateToSave = {
945- strings : initialState . strings ,
944+ taskName : initialState . taskName ,
946945 lang : initialState . lang ,
947946 failedToTranslate : initialState . failedToTranslate ,
948947 }
949-
950948 await setTaskStateField ( stateToSave ) ;
949+
950+ this . adminforth . websocket . publish ( '/translation_progress' , { } ) ;
951+ if ( initialState . failedToTranslate . length > 0 ) {
952+ afLogger . error ( `Failed to translate some strings for language ${ initialState . lang } in plugin ${ this . constructor . name } :, ${ initialState . failedToTranslate } ` ) ;
953+ throw new Error ( `Failed to translate some strings for language ${ initialState . lang } , check job details for more info` ) ;
954+ }
951955 } ,
952956 //limit of tasks, that are running in parallel
953957 parallelLimit : this . options . parallelTranslationLimit || 20 ,
@@ -1276,6 +1280,68 @@ export default class I18nPlugin extends AdminForthPlugin {
12761280 }
12771281 } ) ;
12781282
1283+ server . endpoint ( {
1284+ method : 'POST' ,
1285+ path : `/plugin/${ this . pluginInstanceId } /get_filtered_ids` ,
1286+ handler : async ( { body, adminUser, headers, query, cookies, requestUrl } ) => {
1287+ const resource = this . resourceConfig ;
1288+
1289+ for ( const hook of resource . hooks ?. list ?. beforeDatasourceRequest || [ ] ) {
1290+ const filterTools = filtersTools . get ( body ) ;
1291+ body . filtersTools = filterTools ;
1292+ const resp = await hook ( {
1293+ resource,
1294+ query : body ,
1295+ adminUser,
1296+ //@ts -ignore
1297+ filtersTools : filterTools ,
1298+ extra : {
1299+ body, query, headers, cookies, requestUrl
1300+ } ,
1301+ adminforth : this . adminforth ,
1302+ } ) ;
1303+ if ( ! resp || ( ! resp . ok && ! resp . error ) ) {
1304+ throw new Error ( `Hook must return object with {ok: true} or { error: 'Error' } ` ) ;
1305+ }
1306+ if ( resp . error ) {
1307+ return { error : resp . error } ;
1308+ }
1309+ }
1310+ const filters = body . filters ;
1311+
1312+ const normalizedFilters = { operator : AdminForthFilterOperators . AND , subFilters : [ ] } ;
1313+ if ( filters ) {
1314+ if ( typeof filters !== 'object' ) {
1315+ throw new Error ( `Filter should be an array or an object` ) ;
1316+ }
1317+ if ( Array . isArray ( filters ) ) {
1318+ // if filters are an array, they will be connected with "AND" operator by default
1319+ normalizedFilters . subFilters = filters ;
1320+ } else if ( filters . field ) {
1321+ // assume filter is a SingleFilter
1322+ normalizedFilters . subFilters = [ filters ] ;
1323+ } else if ( filters . subFilters ) {
1324+ // assume filter is a AndOr filter
1325+ normalizedFilters . operator = filters . operator ;
1326+ normalizedFilters . subFilters = filters . subFilters ;
1327+ } else {
1328+ // wrong filter
1329+ throw new Error ( `Wrong filter object value: ${ JSON . stringify ( filters ) } ` ) ;
1330+ }
1331+ }
1332+
1333+ const records = await this . adminforth . resource ( this . resourceConfig . resourceId ) . list ( normalizedFilters ) ;
1334+ if ( ! records ) {
1335+ return { ok : true , recordIds : [ ] } ;
1336+ }
1337+ const primaryKeyColumn = this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) ;
1338+
1339+ const recordIds = records . map ( record => record [ primaryKeyColumn . name ] ) ;
1340+
1341+ return { ok : true , recordIds }
1342+ }
1343+ } ) ;
1344+
12791345 }
12801346
12811347}
0 commit comments