Revert "fix"
This reverts commit d9ac3c2cc51586e48520c9a5c343a069aef836aa.
This commit is contained in:
parent
4a9f6764d9
commit
931b3642ea
2 changed files with 71 additions and 90 deletions
|
|
@ -1,69 +1,35 @@
|
||||||
import { withCacheBuster } from './utils.js';
|
import { withCacheBuster } from './utils.js';
|
||||||
import { extractMetadataFromBlob } from './metadata.js';
|
import { extractMetadataFromBlob } from './metadata.js';
|
||||||
|
|
||||||
const GALLERY_STATE_KEY = 'gemini-app-gallery-state';
|
const FILTER_STORAGE_KEY = 'gemini-app-history-filter';
|
||||||
const FILTER_STORAGE_KEY = 'gemini-app-history-filter'; // legacy (single-state)
|
const SEARCH_STORAGE_KEY = 'gemini-app-history-search';
|
||||||
const SEARCH_STORAGE_KEY = 'gemini-app-history-search'; // legacy (single-state)
|
|
||||||
const SOURCE_STORAGE_KEY = 'gemini-app-history-source';
|
const SOURCE_STORAGE_KEY = 'gemini-app-history-source';
|
||||||
const VALID_SOURCES = ['generated', 'uploads'];
|
const VALID_SOURCES = ['generated', 'uploads'];
|
||||||
|
|
||||||
export function createGallery({ galleryGrid, onSelect }) {
|
export function createGallery({ galleryGrid, onSelect }) {
|
||||||
|
let currentFilter = 'all';
|
||||||
|
let searchQuery = '';
|
||||||
let currentSource = 'generated';
|
let currentSource = 'generated';
|
||||||
let allImages = [];
|
let allImages = [];
|
||||||
let favorites = [];
|
let favorites = [];
|
||||||
let perSourceState = {
|
let showOnlyFavorites = false; // New toggle state
|
||||||
generated: { filter: 'all', search: '', favoritesOnly: false },
|
|
||||||
uploads: { filter: 'all', search: '', favoritesOnly: false },
|
|
||||||
};
|
|
||||||
|
|
||||||
const persistState = () => {
|
// Load saved filter and search from localStorage
|
||||||
try {
|
|
||||||
localStorage.setItem(GALLERY_STATE_KEY, JSON.stringify({
|
|
||||||
sourceState: perSourceState,
|
|
||||||
source: currentSource,
|
|
||||||
}));
|
|
||||||
} catch (e) {
|
|
||||||
console.warn('Failed to persist gallery state', e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Load saved state (with legacy fallback)
|
|
||||||
try {
|
try {
|
||||||
const saved = localStorage.getItem(GALLERY_STATE_KEY);
|
const savedFilter = localStorage.getItem(FILTER_STORAGE_KEY);
|
||||||
if (saved) {
|
if (savedFilter) currentFilter = savedFilter;
|
||||||
const parsed = JSON.parse(saved);
|
|
||||||
if (parsed?.sourceState) {
|
const savedSearch = localStorage.getItem(SEARCH_STORAGE_KEY);
|
||||||
perSourceState = {
|
if (savedSearch) searchQuery = savedSearch;
|
||||||
generated: { filter: 'all', search: '', favoritesOnly: false, ...(parsed.sourceState.generated || {}) },
|
|
||||||
uploads: { filter: 'all', search: '', favoritesOnly: false, ...(parsed.sourceState.uploads || {}) },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (parsed?.source && VALID_SOURCES.includes(parsed.source)) {
|
|
||||||
currentSource = parsed.source;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Legacy fallback (single state)
|
|
||||||
const savedFilter = localStorage.getItem(FILTER_STORAGE_KEY);
|
|
||||||
const savedSearch = localStorage.getItem(SEARCH_STORAGE_KEY);
|
|
||||||
if (savedFilter) perSourceState.generated.filter = savedFilter;
|
|
||||||
if (savedSearch) perSourceState.generated.search = savedSearch;
|
|
||||||
}
|
|
||||||
|
|
||||||
const savedSource = localStorage.getItem(SOURCE_STORAGE_KEY);
|
const savedSource = localStorage.getItem(SOURCE_STORAGE_KEY);
|
||||||
if (savedSource && VALID_SOURCES.includes(savedSource)) {
|
if (savedSource && VALID_SOURCES.includes(savedSource)) {
|
||||||
currentSource = savedSource;
|
currentSource = savedSource;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Failed to load gallery state', e);
|
console.warn('Failed to load history filter/search', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
const getState = (source = currentSource) => {
|
|
||||||
if (!perSourceState[source]) {
|
|
||||||
perSourceState[source] = { filter: 'all', search: '', favoritesOnly: false };
|
|
||||||
}
|
|
||||||
return perSourceState[source];
|
|
||||||
};
|
|
||||||
|
|
||||||
// Load favorites from backend
|
// Load favorites from backend
|
||||||
async function loadFavorites() {
|
async function loadFavorites() {
|
||||||
try {
|
try {
|
||||||
|
|
@ -152,29 +118,27 @@ export function createGallery({ galleryGrid, onSelect }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchesSearch(imageUrl) {
|
function matchesSearch(imageUrl) {
|
||||||
const { search } = getState();
|
if (!searchQuery) return true;
|
||||||
if (!search) return true;
|
|
||||||
const filename = imageUrl.split('/').pop().split('?')[0];
|
const filename = imageUrl.split('/').pop().split('?')[0];
|
||||||
return filename.toLowerCase().includes(search.toLowerCase());
|
return filename.toLowerCase().includes(searchQuery.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldShowImage(imageUrl) {
|
function shouldShowImage(imageUrl) {
|
||||||
const state = getState();
|
|
||||||
// First check text search
|
// First check text search
|
||||||
if (!matchesSearch(imageUrl)) return false;
|
if (!matchesSearch(imageUrl)) return false;
|
||||||
|
|
||||||
// Check favorites toggle - if enabled, only show favorites
|
// Check favorites toggle - if enabled, only show favorites
|
||||||
if (state.favoritesOnly && !isFavorite(imageUrl)) {
|
if (showOnlyFavorites && !isFavorite(imageUrl)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then check date filter
|
// Then check date filter
|
||||||
if (state.filter === 'all') return true;
|
if (currentFilter === 'all') return true;
|
||||||
|
|
||||||
const timestamp = getFileTimestamp(imageUrl);
|
const timestamp = getFileTimestamp(imageUrl);
|
||||||
if (!timestamp) return state.filter === 'all';
|
if (!timestamp) return currentFilter === 'all';
|
||||||
|
|
||||||
switch (state.filter) {
|
switch (currentFilter) {
|
||||||
case 'today':
|
case 'today':
|
||||||
return isToday(timestamp);
|
return isToday(timestamp);
|
||||||
case 'week':
|
case 'week':
|
||||||
|
|
@ -341,33 +305,35 @@ export function createGallery({ galleryGrid, onSelect }) {
|
||||||
|
|
||||||
function setFilter(filterType) {
|
function setFilter(filterType) {
|
||||||
if (currentFilter === filterType) return;
|
if (currentFilter === filterType) return;
|
||||||
const state = getState();
|
currentFilter = filterType;
|
||||||
state.filter = filterType;
|
|
||||||
persistState();
|
// Save to localStorage
|
||||||
|
try {
|
||||||
|
localStorage.setItem(FILTER_STORAGE_KEY, filterType);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Failed to save history filter', e);
|
||||||
|
}
|
||||||
|
|
||||||
renderGallery();
|
renderGallery();
|
||||||
}
|
}
|
||||||
|
|
||||||
function setSearch(query) {
|
function setSearch(query) {
|
||||||
const state = getState();
|
searchQuery = query || '';
|
||||||
state.search = query || '';
|
|
||||||
persistState();
|
// Save to localStorage
|
||||||
|
try {
|
||||||
|
localStorage.setItem(SEARCH_STORAGE_KEY, searchQuery);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Failed to save history search', e);
|
||||||
|
}
|
||||||
|
|
||||||
renderGallery();
|
renderGallery();
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleFavorites() {
|
function toggleFavorites() {
|
||||||
const state = getState();
|
showOnlyFavorites = !showOnlyFavorites;
|
||||||
state.favoritesOnly = !state.favoritesOnly;
|
|
||||||
persistState();
|
|
||||||
renderGallery();
|
renderGallery();
|
||||||
return state.favoritesOnly;
|
return showOnlyFavorites;
|
||||||
}
|
|
||||||
|
|
||||||
function setFavoritesActive(active) {
|
|
||||||
const state = getState();
|
|
||||||
state.favoritesOnly = Boolean(active);
|
|
||||||
persistState();
|
|
||||||
renderGallery();
|
|
||||||
return state.favoritesOnly;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setSource(source, { resetFilters = false } = {}) {
|
function setSource(source, { resetFilters = false } = {}) {
|
||||||
|
|
@ -379,22 +345,25 @@ export function createGallery({ galleryGrid, onSelect }) {
|
||||||
console.warn('Failed to save history source', e);
|
console.warn('Failed to save history source', e);
|
||||||
}
|
}
|
||||||
if (resetFilters) {
|
if (resetFilters) {
|
||||||
const state = getState();
|
currentFilter = 'all';
|
||||||
state.filter = 'all';
|
showOnlyFavorites = false;
|
||||||
state.favoritesOnly = false;
|
searchQuery = '';
|
||||||
state.search = '';
|
try {
|
||||||
persistState();
|
localStorage.setItem(FILTER_STORAGE_KEY, currentFilter);
|
||||||
|
localStorage.setItem(SEARCH_STORAGE_KEY, searchQuery);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Failed to reset history filters', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
persistState();
|
|
||||||
return load();
|
return load();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentFilter() {
|
function getCurrentFilter() {
|
||||||
return getState().filter;
|
return currentFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSearchQuery() {
|
function getSearchQuery() {
|
||||||
return getState().search;
|
return searchQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentSource() {
|
function getCurrentSource() {
|
||||||
|
|
@ -402,7 +371,13 @@ export function createGallery({ galleryGrid, onSelect }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isFavoritesActive() {
|
function isFavoritesActive() {
|
||||||
return getState().favoritesOnly;
|
return showOnlyFavorites;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setFavoritesActive(active) {
|
||||||
|
showOnlyFavorites = Boolean(active);
|
||||||
|
renderGallery();
|
||||||
|
return showOnlyFavorites;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setSearchQuery(value) {
|
function setSearchQuery(value) {
|
||||||
|
|
|
||||||
|
|
@ -1713,24 +1713,30 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||||
b.classList.toggle('active', active);
|
b.classList.toggle('active', active);
|
||||||
b.setAttribute('aria-pressed', String(active));
|
b.setAttribute('aria-pressed', String(active));
|
||||||
});
|
});
|
||||||
await gallery.setSource(targetSource, { resetFilters: false });
|
await gallery.setSource(targetSource, { resetFilters: true });
|
||||||
|
|
||||||
// Sync UI with persisted state (filter, favorites, search)
|
// Reset filters UI to show all when switching source
|
||||||
const activeFilter = gallery.getCurrentFilter ? gallery.getCurrentFilter() : 'all';
|
|
||||||
historyFilterBtns.forEach(b => {
|
historyFilterBtns.forEach(b => {
|
||||||
if (!b.classList.contains('history-favorites-btn')) {
|
if (!b.classList.contains('history-favorites-btn')) {
|
||||||
const isFilterActive = b.dataset.filter === activeFilter;
|
b.classList.toggle('active', b.dataset.filter === 'all');
|
||||||
b.classList.toggle('active', isFilterActive);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (historyFavoritesBtn && gallery.isFavoritesActive) {
|
// Disable favorites toggle on source change
|
||||||
historyFavoritesBtn.classList.toggle('active', gallery.isFavoritesActive());
|
if (historyFavoritesBtn) {
|
||||||
|
historyFavoritesBtn.classList.remove('active');
|
||||||
|
}
|
||||||
|
if (gallery.setFavoritesActive) {
|
||||||
|
gallery.setFavoritesActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear search box
|
||||||
const historySearchInputEl = document.getElementById('history-search-input');
|
const historySearchInputEl = document.getElementById('history-search-input');
|
||||||
if (historySearchInputEl && gallery.getSearchQuery) {
|
if (historySearchInputEl) {
|
||||||
historySearchInputEl.value = gallery.getSearchQuery();
|
historySearchInputEl.value = '';
|
||||||
|
}
|
||||||
|
if (gallery.setSearchQuery) {
|
||||||
|
gallery.setSearchQuery('');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue