arama özelliği eklendi

This commit is contained in:
2025-11-01 15:39:01 +03:00
parent ebe00fb6f7
commit 1eef5d232f
6 changed files with 225 additions and 30 deletions

View File

@@ -0,0 +1,80 @@
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]: ""
}
};
});
}