Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.
Rust downloadmanagerguihacktoberfesthomebrewlightweightlinuxlinux-distributionlocal-firstmacosmacos-packagemedia-toolboxopen-sourcerustsveltetauritrendingtrending-repositorieswindowswindows-desktopytdlp
feat: implement search functionality with results display and state management
Tonhocf2864b906ef
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelteindex ac61ea79..79f1c5f5 100644--- a/src/routes/+page.svelte+++ b/src/routes/+page.svelte@@ -39,6 +39,16 @@format_note: string | null;};+ type SearchResult = {+ id: string;+ title: string;+ author: string;+ duration: number | null;+ thumbnail_url: string | null;+ url: string;+ platform: string;+ };+type OmniState =| { kind: "idle" }| { kind: "detecting" }@@ -46,6 +56,9 @@| { kind: "unsupported" }| { kind: "preparing"; platform: string }| { kind: "batch"; urls: string[] }+ | { kind: "searching" }+ | { kind: "search-results"; results: SearchResult[] }+ | { kind: "search-empty" }| { kind: "error"; message: string; originalUrl: string; platform: string };let url = $state("");@@ -112,6 +125,17 @@omniState.kind === "batch");+ let showOmnibox = $derived(+ omniState.kind === "idle" ||+ omniState.kind === "detecting" ||+ omniState.kind === "detected" ||+ omniState.kind === "unsupported" ||+ omniState.kind === "batch" ||+ omniState.kind === "searching" ||+ omniState.kind === "search-results" ||+ omniState.kind === "search-empty"+ );+function isUrl(value: string): boolean {return value.startsWith("http://") || value.startsWith("https://");}@@ -137,15 +161,22 @@return;}- if (!isUrl(trimmed)) {- omniState = { kind: "idle" };+ if (isUrl(trimmed)) {+ omniState = { kind: "detecting" };+ debounceTimer = setTimeout(() => {+ detectPlatform(trimmed);+ }, 500);return;}- omniState = { kind: "detecting" };- debounceTimer = setTimeout(() => {- detectPlatform(trimmed);- }, 500);+ if (trimmed.length >= 2) {+ omniState = { kind: "searching" };+ debounceTimer = setTimeout(() => {+ performSearch(trimmed);+ }, 600);+ } else {+ omniState = { kind: "idle" };+ }}async function detectPlatform(value: string) {@@ -161,6 +192,41 @@}}+ async function performSearch(query: string) {+ try {+ const results = await invoke<SearchResult[]>("search_videos", {+ query,+ platform: "youtube",+ maxResults: 6,+ });+ if (url.trim() !== query) return;+ if (results.length > 0) {+ omniState = { kind: "search-results", results };+ } else {+ omniState = { kind: "search-empty" };+ }+ } catch {+ if (url.trim() === query) {+ omniState = { kind: "search-empty" };+ }+ }+ }++ function selectSearchResult(result: SearchResult) {+ url = result.url;+ omniState = { kind: "detecting" };+ detectPlatform(result.url);+ }++ function formatDuration(seconds: number | null): string {+ if (seconds === null) return "";+ const h = Math.floor(seconds / 3600);+ const m = Math.floor((seconds % 3600) / 60);+ const s = Math.floor(seconds % 60);+ if (h > 0) return `${h}:${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`;+ return `${m}:${s.toString().padStart(2, "0")}`;+ }+function getContentTypeLabel(contentType: string | null): string {if (!contentType) return $t("omnibox.content_type.unknown");const key = `omnibox.content_type.${contentType}`;@@ -351,7 +417,7 @@/>{/if}- {#if omniState.kind === "idle" || omniState.kind === "detecting" || omniState.kind === "detected" || omniState.kind === "unsupported" || omniState.kind === "batch"}+ {#if showOmnibox}<div class="omnibox-wrapper"><inputclass="omnibox"@@ -547,6 +613,54 @@{$t('omnibox.batch_download_all')}</button>+ {:else if omniState.kind === "searching"}+ <div class="feedback feedback-enter">+ <span class="feedback-spinner"></span>+ <span class="feedback-text search-hint">{$t('omnibox.searching')}</span>+ </div>++ {:else if omniState.kind === "search-results"}+ <div class="search-results feedback-enter">+ {#each omniState.results as result (result.id)}+ <button class="search-result-row" onclick={() => selectSearchResult(result)}>+ {#if result.thumbnail_url}+ <img+ src={result.thumbnail_url}+ alt=""+ class="search-thumb"+ loading="lazy"+ />+ {:else}+ <div class="search-thumb search-thumb-placeholder">+ <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <path d="M23 7l-7 5 7 5V7z" />+ <rect x="1" y="5" width="15" height="14" rx="2" ry="2" />+ </svg>+ </div>+ {/if}+ <div class="search-result-info">+ <span class="search-result-title">{result.title}</span>+ <span class="search-result-meta">+ {#if result.author}{result.author}{/if}+ {#if result.author && result.duration !== null}+ <span class="feedback-sep">·</span>+ {/if}+ {#if result.duration !== null}{formatDuration(result.duration)}{/if}+ </span>+ </div>+ </button>+ {/each}+ </div>++ {:else if omniState.kind === "search-empty"}+ <div class="feedback feedback-enter" data-supported="false">+ <svg class="feedback-icon" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <circle cx="11" cy="11" r="8" />+ <path d="M21 21l-4.35-4.35" />+ </svg>+ <span class="feedback-text">{$t('omnibox.search_empty')}</span>+ </div>+{:else if omniState.kind === "unsupported"}<div class="feedback feedback-enter" data-supported="false"><svg class="feedback-icon" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">@@ -1086,6 +1200,99 @@}}+ .search-hint {+ color: var(--gray);+ }++ .search-results {+ width: 100%;+ display: flex;+ flex-direction: column;+ background: var(--button);+ border-radius: var(--border-radius);+ box-shadow: var(--button-box-shadow);+ overflow: hidden;+ }++ .search-result-row {+ display: flex;+ align-items: center;+ gap: var(--padding);+ padding: calc(var(--padding) * 0.75) var(--padding);+ background: transparent;+ border: none;+ border-bottom: 1px solid var(--button-stroke);+ cursor: pointer;+ text-align: left;+ color: var(--secondary);+ }++ .search-result-row:last-child {+ border-bottom: none;+ }++ @media (hover: hover) {+ .search-result-row:hover {+ background: var(--button-hover);+ }+ }++ .search-result-row:active {+ background: var(--button-press);+ }++ .search-result-row:focus-visible {+ outline: var(--focus-ring);+ outline-offset: var(--focus-ring-offset);+ }++ .search-thumb {+ width: 64px;+ height: 36px;+ border-radius: calc(var(--border-radius) / 2);+ object-fit: cover;+ flex-shrink: 0;+ pointer-events: none;+ background: var(--button-elevated);+ }++ .search-thumb-placeholder {+ display: flex;+ align-items: center;+ justify-content: center;+ color: var(--gray);+ }++ .search-thumb-placeholder svg {+ pointer-events: none;+ }++ .search-result-info {+ display: flex;+ flex-direction: column;+ gap: 2px;+ min-width: 0;+ flex: 1;+ }++ .search-result-title {+ font-size: 13px;+ font-weight: 500;+ color: var(--secondary);+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }++ .search-result-meta {+ font-size: 11.5px;+ font-weight: 500;+ color: var(--gray);+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+@media (prefers-reduced-motion: reduce) {.feedback-enter {animation: none;