diff --git a/internal/anime/handler.go b/internal/anime/handler.go index c7164e6..a4643e4 100644 --- a/internal/anime/handler.go +++ b/internal/anime/handler.go @@ -125,6 +125,7 @@ func (h *AnimeHandler) Register(r *gin.Engine) { r.GET("/api/catalog/popular", h.HandleCatalogPopular) r.GET("/api/catalog/continue", h.HandleCatalogContinue) r.GET("/api/catalog/top-pick", h.HandleCatalogTopPickForYou) + r.GET("/search", h.HandleSearch) r.GET("/discover", h.HandleDiscover) r.GET("/discover/top-picks", h.HandleDiscoverTopPicksForYou) r.GET("/api/discover/trending", h.HandleDiscoverTrending) @@ -142,6 +143,13 @@ func (h *AnimeHandler) Register(r *gin.Engine) { r.GET("/api/jikan/producers", h.HandleProducers) } +func (h *AnimeHandler) HandleSearch(c *gin.Context) { + c.HTML(http.StatusOK, "search.gohtml", gin.H{ + "User": server.CurrentUser(c), + "CurrentPath": "/search", + }) +} + func producerQueryParams(c *gin.Context) (string, int, int, error) { q := strings.TrimSpace(c.Query("q")) diff --git a/static/search.ts b/static/search.ts index bc5c804..d74ceba 100644 --- a/static/search.ts +++ b/static/search.ts @@ -25,6 +25,7 @@ const searchResults = document.querySelector( ) as HTMLElement | null; const searchDialog = document.querySelector("[data-command-palette-dialog]") as HTMLElement | null; const searchRoot = document.querySelector("[data-command-palette-root]") as HTMLElement | null; +const searchPage = document.querySelector("[data-command-palette-page]") as HTMLElement | null; const searchOpenButtons = document.querySelectorAll("[data-command-palette-open]"); const searchCloseButtons = document.querySelectorAll("[data-command-palette-close]"); const searchClearButtons = document.querySelectorAll("[data-command-palette-clear]"); @@ -446,7 +447,9 @@ const appendItems = (items: CommandPaletteItem[]): void => { } const existingIDs = new Set(resultItems.map((item) => item.id)); - const nextItems = dedupeByID(items, (item) => item.id).filter((item) => !existingIDs.has(item.id)); + const nextItems = dedupeByID(items, (item) => item.id).filter( + (item) => !existingIDs.has(item.id), + ); if (nextItems.length === 0) { return; } @@ -502,6 +505,7 @@ const fetchSearchItems = (query: string): void => { if (query === "") { clearResults(); + renderEmptyState(""); return; } @@ -620,18 +624,21 @@ const scheduleFetch = (): void => { }; const openSearch = (): void => { - if (!searchDialog || !searchInput) { + if (!searchInput) { + window.location.href = "/search"; return; } lastFocusedSearchOpener = document.activeElement instanceof HTMLElement ? document.activeElement : null; - setSearchState(true); - searchInput.value = ""; - lastQuery = ""; - cancelScheduledFetch(); - setClearButtonState(false); - clearResults(); + if (searchDialog) { + setSearchState(true); + searchInput.value = ""; + lastQuery = ""; + cancelScheduledFetch(); + setClearButtonState(false); + clearResults(); + } searchInput.focus(); }; @@ -695,7 +702,9 @@ const onDocumentKeydown = (event: KeyboardEvent): void => { if (commandShortcut && !isTypingTarget(event.target)) { event.preventDefault(); - if (isSearchOpen()) { + if (searchPage) { + searchInput?.focus(); + } else if (isSearchOpen()) { closeSearch(); } else { openSearch(); @@ -705,7 +714,11 @@ const onDocumentKeydown = (event: KeyboardEvent): void => { if (event.key === "/" && !isTypingTarget(event.target)) { event.preventDefault(); - openSearch(); + if (searchPage) { + searchInput?.focus(); + } else { + openSearch(); + } return; } @@ -741,6 +754,17 @@ const initSearchOverlay = (): void => { document.addEventListener("click", onDocumentClick); document.addEventListener("keydown", onDocumentKeydown); searchDialog?.setAttribute("aria-hidden", "true"); + + const initialQuery = new URLSearchParams(window.location.search).get("q")?.trim() || ""; + if (initialQuery) { + searchInput.value = initialQuery; + fetchSearchItems(initialQuery); + } else { + renderEmptyState(""); + } + if (searchPage) { + searchInput.focus(); + } }; initSearchOverlay(); diff --git a/templates/base.gohtml b/templates/base.gohtml index f6f46d7..130fe1d 100644 --- a/templates/base.gohtml +++ b/templates/base.gohtml @@ -99,37 +99,6 @@ {{end}} -
-