81 lines
1.7 KiB
JavaScript
81 lines
1.7 KiB
JavaScript
import { derived, writable } from "svelte/store";
|
|
|
|
const KNOWN_SCOPES = new Set(["files", "movies", "tv"]);
|
|
|
|
const initialState = {
|
|
scope: "files",
|
|
terms: {
|
|
files: "",
|
|
movies: "",
|
|
tv: ""
|
|
}
|
|
};
|
|
|
|
export const searchState = writable(initialState);
|
|
|
|
export const activeScope = derived(searchState, ($state) => $state.scope);
|
|
|
|
export const activeSearchTerm = derived(
|
|
searchState,
|
|
($state) => $state.terms[$state.scope] || ""
|
|
);
|
|
|
|
const PLACEHOLDERS = {
|
|
files: "Dosya ara...",
|
|
movies: "Film ara...",
|
|
tv: "Dizi ara..."
|
|
};
|
|
|
|
export const activePlaceholder = derived(
|
|
searchState,
|
|
($state) => PLACEHOLDERS[$state.scope] || "Ara..."
|
|
);
|
|
|
|
export function setSearchScope(scope) {
|
|
const normalized = KNOWN_SCOPES.has(scope) ? scope : "files";
|
|
searchState.update((state) => {
|
|
const hasScope = Object.prototype.hasOwnProperty.call(state.terms, normalized);
|
|
const terms = hasScope ? state.terms : { ...state.terms, [normalized]: "" };
|
|
if (state.scope === normalized && terms === state.terms) {
|
|
return state;
|
|
}
|
|
return {
|
|
scope: normalized,
|
|
terms
|
|
};
|
|
});
|
|
}
|
|
|
|
export function updateSearchTerm(term) {
|
|
searchState.update((state) => {
|
|
const scope = state.scope;
|
|
const nextTerms = {
|
|
...state.terms,
|
|
[scope]: term
|
|
};
|
|
if (nextTerms[scope] === state.terms[scope]) {
|
|
return state;
|
|
}
|
|
return {
|
|
scope,
|
|
terms: nextTerms
|
|
};
|
|
});
|
|
}
|
|
|
|
export function clearSearch(scope) {
|
|
searchState.update((state) => {
|
|
const targetScope = scope && KNOWN_SCOPES.has(scope) ? scope : state.scope;
|
|
if ((state.terms[targetScope] || "") === "") {
|
|
return state;
|
|
}
|
|
return {
|
|
scope: state.scope,
|
|
terms: {
|
|
...state.terms,
|
|
[targetScope]: ""
|
|
}
|
|
};
|
|
});
|
|
}
|