mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-11-15 02:24:04 +00:00
refactor(core): ♻️ various minor refactor
This commit is contained in:
parent
3d2ec5300c
commit
434ec2ebaf
|
@ -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 ) {
|
function searchHistory( config ) {
|
||||||
return {
|
return {
|
||||||
data: [],
|
data: [],
|
||||||
/* TODO: Should probably create a separate config */
|
|
||||||
limit: config.wgCitizenMaxSearchResults,
|
limit: config.wgCitizenMaxSearchResults,
|
||||||
|
isValidSearchHistory: function ( arr ) {
|
||||||
|
return Array.isArray( arr ) && arr.length <= this.limit;
|
||||||
|
},
|
||||||
get: function () {
|
get: function () {
|
||||||
// IDK why this.data keeps returning an empty array without init
|
if ( this.data.length === 0 ) {
|
||||||
this.init();
|
this.init();
|
||||||
|
}
|
||||||
return this.data;
|
return this.data;
|
||||||
},
|
},
|
||||||
set: function ( arr ) {
|
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 ) {
|
add: function ( query ) {
|
||||||
if ( typeof query === 'string' ) {
|
if ( typeof query === 'string' ) {
|
||||||
let history = this.data;
|
let history = [ ...this.data ];
|
||||||
history.unshift( query );
|
history.unshift( query );
|
||||||
history = history.filter( ( value, index ) => {
|
history = [ ...new Set( history ) ]; // Remove duplicates
|
||||||
return history.indexOf( value ) === index;
|
|
||||||
} );
|
|
||||||
if ( history.length > this.limit ) {
|
if ( history.length > this.limit ) {
|
||||||
history.splice( this.limit );
|
history.length = this.limit;
|
||||||
}
|
}
|
||||||
this.set( history );
|
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 () {
|
init: function () {
|
||||||
const storedData = mw.storage.get( 'skin-citizen-search-history' );
|
const storedData = mw.storage.get( 'skin-citizen-search-history' );
|
||||||
if ( storedData ) {
|
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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -400,7 +400,7 @@ async function getSuggestions() {
|
||||||
id: 'suggestion',
|
id: 'suggestion',
|
||||||
items: []
|
items: []
|
||||||
};
|
};
|
||||||
results.forEach( ( result ) => {
|
const items = results.map( ( result ) => {
|
||||||
const data = {
|
const data = {
|
||||||
type: 'page',
|
type: 'page',
|
||||||
size: 'md',
|
size: 'md',
|
||||||
|
@ -415,8 +415,9 @@ async function getSuggestions() {
|
||||||
// Thumbnail placeholder icon
|
// Thumbnail placeholder icon
|
||||||
data.icon = 'image';
|
data.icon = 'image';
|
||||||
}
|
}
|
||||||
itemGroupData.items.push( data );
|
return data;
|
||||||
} );
|
} );
|
||||||
|
itemGroupData.items.push( ...items );
|
||||||
fragment.append( htmlHelper.getItemGroupElement( itemGroupData ) );
|
fragment.append( htmlHelper.getItemGroupElement( itemGroupData ) );
|
||||||
} else {
|
} else {
|
||||||
// Update placeholder with no result content
|
// Update placeholder with no result content
|
||||||
|
|
Loading…
Reference in a new issue