Skip to content

Commit

Permalink
refactor(core): ♻️ various minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alistair3149 committed May 22, 2024
1 parent 3d2ec53 commit 434ec2e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
40 changes: 28 additions & 12 deletions resources/skins.citizen.search/searchHistory.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
/**
* Represents a search history object that stores search queries and provides methods to manipulate the history.
*
* @param {Object} config - The configuration object containing the maximum number of search results allowed.
* @return {Object} An object with methods to get, set, add, clear, and initialize the search history.
*/
function searchHistory( config ) {
return {
data: [],
/* TODO: Should probably create a separate config */
limit: config.wgCitizenMaxSearchResults,
isValidSearchHistory: function ( arr ) {
return Array.isArray( arr ) && arr.length <= this.limit;
},
get: function () {
// IDK why this.data keeps returning an empty array without init
this.init();
if ( this.data.length === 0 ) {
this.init();
}
return this.data;
},
set: function ( arr ) {
this.data = arr;
if ( arr && this.isValidSearchHistory( arr ) ) {
this.data = arr;
mw.storage.set( 'skin-citizen-search-history', JSON.stringify( this.data ) );
}
},
add: function ( query ) {
if ( typeof query === 'string' ) {
let history = this.data;
let history = [ ...this.data ];
history.unshift( query );
history = history.filter( ( value, index ) => {
return history.indexOf( value ) === index;
} );
history = [ ...new Set( history ) ]; // Remove duplicates
if ( history.length > this.limit ) {
history.splice( this.limit );
history.length = this.limit;
}
this.set( history );
/* NOTE: Should we set an expiry? This data only exists locally though */
mw.storage.set( 'skin-citizen-search-history', JSON.stringify( this.data ) );
}
},
clear: function () {
this.set( [] );
mw.storage.remove( 'skin-citizen-search-history' );
},
init: function () {
const storedData = mw.storage.get( 'skin-citizen-search-history' );
if ( storedData ) {
this.set( JSON.parse( storedData ) );
try {
this.data = JSON.parse( storedData );
} catch ( error ) {
mw.log.error( `[Citizen] Error parsing search history. Stored data: ${ storedData }`, error );
}
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions resources/skins.citizen.search/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ async function getSuggestions() {
id: 'suggestion',
items: []
};
results.forEach( ( result ) => {
const items = results.map( ( result ) => {
const data = {
type: 'page',
size: 'md',
Expand All @@ -415,8 +415,9 @@ async function getSuggestions() {
// Thumbnail placeholder icon
data.icon = 'image';
}
itemGroupData.items.push( data );
return data;
} );
itemGroupData.items.push( ...items );
fragment.append( htmlHelper.getItemGroupElement( itemGroupData ) );
} else {
// Update placeholder with no result content
Expand Down

0 comments on commit 434ec2e

Please sign in to comment.