2023-08-04 23:49:02 +00:00
|
|
|
function searchHistory( config ) {
|
|
|
|
return {
|
|
|
|
data: [],
|
|
|
|
/* TODO: Should probably create a separate config */
|
|
|
|
limit: config.wgCitizenMaxSearchResults,
|
|
|
|
get: function () {
|
2023-08-24 00:44:01 +00:00
|
|
|
// IDK why this.data keeps returning an empty array without init
|
|
|
|
this.init();
|
2023-08-04 23:49:02 +00:00
|
|
|
return this.data;
|
|
|
|
},
|
2023-08-24 00:44:01 +00:00
|
|
|
set: function ( arr ) {
|
|
|
|
this.data = arr;
|
|
|
|
},
|
2023-08-04 23:49:02 +00:00
|
|
|
add: function ( query ) {
|
|
|
|
if ( typeof query === 'string' ) {
|
|
|
|
let history = this.data;
|
|
|
|
history.unshift( query );
|
|
|
|
history = history.filter( ( value, index ) => {
|
|
|
|
return history.indexOf( value ) === index;
|
|
|
|
} );
|
|
|
|
if ( history.length > this.limit ) {
|
|
|
|
history.splice( this.limit );
|
|
|
|
}
|
2023-08-24 00:44:01 +00:00
|
|
|
this.set( history );
|
2023-08-04 23:49:02 +00:00
|
|
|
/* NOTE: Should we set an expiry? This data only exists locally though */
|
|
|
|
mw.storage.set( 'skin-citizen-search-history', JSON.stringify( this.data ) );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
init: function () {
|
|
|
|
const storedData = mw.storage.get( 'skin-citizen-search-history' );
|
2023-08-24 00:44:01 +00:00
|
|
|
if ( storedData ) {
|
|
|
|
this.set( JSON.parse( storedData ) );
|
|
|
|
}
|
2023-08-04 23:49:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @module searchHistory */
|
|
|
|
module.exports = searchHistory;
|