Files
cinny/src/app/state/recentSearches.ts
T
jared 1ee0f0b57a feat(search): has:image/file/video filters + recent searches
- Add three msgtype toggle chips (Images/Files/Video) to the search filter
  bar, mirroring the existing "Has link" chip. The Matrix search API can't
  filter by msgtype server-side, so results are post-filtered client-side
  (union match on event.content.msgtype, dropping now-empty groups); the
  server request is unchanged. Visible count may be lower than the server
  total — inherent to client-side filtering.
- Recent searches: last 10 distinct terms persisted via a new
  state/recentSearches.ts (atomWithStorage, error-safe, mirrors
  scheduledMessages). Shown as clickable chips when the search input is
  focused + empty, with a Clear affordance; clicking re-runs the search.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 14:22:08 -04:00

39 lines
1.3 KiB
TypeScript

import { atom } from 'jotai';
import { atomWithStorage, createJSONStorage } from 'jotai/utils';
const STORAGE_KEY = 'cinny_recent_searches_v1';
const MAX_RECENT_SEARCHES = 10;
// Internal atom persists as a plain string[] (JSON-serializable).
const internalAtom = atomWithStorage<string[]>(
STORAGE_KEY,
[],
createJSONStorage(() => localStorage),
);
/**
* Global atom: string[] of the most recent distinct, non-empty search terms.
* Most-recent first, deduped, capped at MAX_RECENT_SEARCHES.
* Backed by localStorage so recent searches survive page refreshes.
*/
export const recentSearchesAtom = atom(
(get): string[] => get(internalAtom),
(_get, set, updater: string[] | ((prev: string[]) => string[])) => {
set(internalAtom, (prev) => {
const prevList = Array.isArray(prev) ? prev : [];
const next = typeof updater === 'function' ? updater(prevList) : updater;
return next;
});
},
);
/**
* Prepend a search term: dedupes (case-sensitive), drops empties, caps at 10.
*/
export const addRecentSearch = (prev: string[], term: string): string[] => {
const trimmed = term.trim();
if (!trimmed) return prev;
const withoutDupe = prev.filter((t) => t !== trimmed);
return [trimmed, ...withoutDupe].slice(0, MAX_RECENT_SEARCHES);
};