Skip to content

Commit

Permalink
feat: enhance species settings functionality with caching and improve…
Browse files Browse the repository at this point in the history
…d predictions

- Introduced caching for species data using localStorage to optimize loading times and reduce unnecessary API calls.
- Updated the `init` and `loadSpeciesData` methods to handle species data initialization and caching based on the app version.
- Refactored the `updatePredictions` method to utilize cached data, improving responsiveness when filtering species.
- Added a new `filteredSpecies` array to manage included species more effectively.

These changes improve the performance and user experience of the species settings component.
  • Loading branch information
tphakala committed Jan 14, 2025
1 parent 9a37d82 commit 14e6a68
Showing 1 changed file with 74 additions and 23 deletions.
97 changes: 74 additions & 23 deletions views/settings/speciesSettings.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,58 @@
},
newIncludeSpecies: '',
showTooltip: null,
hasChanges: false,
hasChanges: false,
speciesSettingsOpen: false,
showActionsModal: false,
currentSpecies: '',
predictions: [],
allSpecies: [],
filteredSpecies: [],
async init() {
this.allSpecies = {{getAllSpecies | toJSON}};
this.allSpecies = [];
this.filteredSpecies = [];
// Use app version for cache busting
const appVersion = '{{.Settings.Version}}';
const cached = localStorage.getItem('allSpecies');
const cachedFiltered = localStorage.getItem('filteredSpecies');
const cachedVersion = localStorage.getItem('allSpeciesVersion');
if (cached && cachedVersion === appVersion) {
this.allSpecies = JSON.parse(cached);
}
if (cachedFiltered && cachedVersion === appVersion) {
this.filteredSpecies = JSON.parse(cachedFiltered);
}
},
async loadSpeciesData() {
if (this.allSpecies.length === 0) {
this.allSpecies = {{getAllSpecies | toJSON}};
// Cache with app version
localStorage.setItem('allSpecies', JSON.stringify(this.allSpecies));
localStorage.setItem('allSpeciesVersion', '{{.Settings.Version}}');
}
if (this.filteredSpecies.length === 0) {
this.filteredSpecies = {{getIncludedSpecies | toJSON}};
// Cache filtered species
localStorage.setItem('filteredSpecies', JSON.stringify(this.filteredSpecies));
}
},
async updatePredictions(input, listType) {
if (!input) {
this.predictions = [];
return;
}
await this.loadSpeciesData();
const sourceList = listType === 'Include' ? this.allSpecies : this.filteredSpecies;
this.predictions = sourceList
.filter(species =>
species.toLowerCase().includes(input.toLowerCase())
)
.slice(0, 5);
},
speciesSettingsOpen: false,
showActionsModal: false,
currentSpecies: '',
resetChanges() {
this.hasChanges = false;
},
Expand All @@ -37,21 +80,6 @@
this.speciesSettings[list] = this.speciesSettings[list].filter(s => s !== species);
this.hasChanges = true;
},
updatePredictions(input, listType) {
if (!input) {
this.predictions = [];
return;
}
// Use different species list based on whether we're including or excluding
const sourceList = this.allSpecies;
this.predictions = sourceList
.filter(species =>
species.toLowerCase().includes(input.toLowerCase())
)
.slice(0, 5);
},
}"
x-init="init(); $watch('speciesSettings', () => { hasChanges = true }, { deep: true })"
x-cloak>
Expand Down Expand Up @@ -137,7 +165,19 @@
predictions: [],
filteredSpecies: [],
async init() {
this.filteredSpecies = {{getIncludedSpecies | toJSON}};
// Use app version for cache busting
const appVersion = '{{.Settings.Version}}';
const cachedFiltered = localStorage.getItem('filteredSpecies');
const cachedVersion = localStorage.getItem('allSpeciesVersion');
if (cachedFiltered && cachedVersion === appVersion) {
this.filteredSpecies = JSON.parse(cachedFiltered);
} else {
this.filteredSpecies = {{getIncludedSpecies | toJSON}};
// Cache filtered species
localStorage.setItem('filteredSpecies', JSON.stringify(this.filteredSpecies));
localStorage.setItem('allSpeciesVersion', '{{.Settings.Version}}');
}
},
resetChanges() {
this.hasChanges = false;
Expand Down Expand Up @@ -249,8 +289,19 @@
allSpecies: [],
filteredSpecies: [],
async init() {
this.allSpecies = {{getAllSpecies | toJSON}};
this.filteredSpecies = {{getIncludedSpecies | toJSON}};
// Use app version for cache busting
const appVersion = '{{.Settings.Version}}';
const cachedFiltered = localStorage.getItem('filteredSpecies');
const cachedVersion = localStorage.getItem('allSpeciesVersion');
if (cachedFiltered && cachedVersion === appVersion) {
this.filteredSpecies = JSON.parse(cachedFiltered);
} else {
this.filteredSpecies = {{getIncludedSpecies | toJSON}};
// Cache filtered species
localStorage.setItem('filteredSpecies', JSON.stringify(this.filteredSpecies));
localStorage.setItem('allSpeciesVersion', '{{.Settings.Version}}');
}
},
speciesSettingsOpen: false,
showActionsModal: false,
Expand Down

0 comments on commit 14e6a68

Please sign in to comment.