From 9f9f6fd9714acc50fce737161d6fad546d00b5ec Mon Sep 17 00:00:00 2001 From: sopi <53455523+sopisoft@users.noreply.github.com> Date: Sat, 27 Apr 2024 13:34:30 +0900 Subject: [PATCH] fix #74 (#75) --- src/popup/components/search_input.tsx | 16 ++++++++++------ src/popup/components/search_result.tsx | 5 +++-- src/popup/utils.ts | 9 +++++++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/popup/components/search_input.tsx b/src/popup/components/search_input.tsx index 6b2bbcc..2c6181e 100644 --- a/src/popup/components/search_input.tsx +++ b/src/popup/components/search_input.tsx @@ -26,13 +26,14 @@ import { import { useToast } from "@/components/ui/use-toast"; import { getConfig } from "@/config"; import { SearchIcon } from "lucide-react"; -import { useEffect, useState } from "react"; +import { createRef, useEffect, useState } from "react"; import browser from "webextension-polyfill"; import search from "../api/search"; import { ErrorMessage } from "../utils"; import SearchResult from "./search_result"; function Search() { + const ref = createRef(); const [word, setWord] = useState(""); const [snapshot, setSnapshot] = useState(); const { toast } = useToast(); @@ -46,8 +47,9 @@ function Search() { return tabs_title; } - async function _search(word: string) { - const snapshot = await search(word); + async function _search() { + const _word = ref.current?.dataset.word ?? word; + const snapshot = await search(_word.replaceAll("-", " ")); if (snapshot instanceof Error) { ErrorMessage(toast, { error: snapshot }); setSnapshot(undefined); @@ -62,13 +64,13 @@ function Search() { const title = tabs_title.replaceAll("-", " "); setWord(title); getConfig("auto_search", async (enabled) => { - if (enabled) _search(title); + if (enabled) await _search(); }); })(); }, [get_tabs_title, _search]); async function on_search_button_click() { - _search(word); + await _search(); } return ( @@ -87,12 +89,14 @@ function Search() {
{ - setWord(e.target.value); + const value = e.target.value; + e.target.dataset.word = value; }} /> diff --git a/src/popup/components/search_result.tsx b/src/popup/components/search_result.tsx index 9732d10..2c427e0 100644 --- a/src/popup/components/search_result.tsx +++ b/src/popup/components/search_result.tsx @@ -92,12 +92,13 @@ function SearchResult(props: { snapshot: Snapshot }) { function handler(contentId: string) { if (contentId === videoId) { - ErrorMessage(toast, { + const t = ErrorMessage(toast, { message: { - title: "Error", + title: "エラー", description: "既に入力されている動画IDです。", }, }); + setTimeout(() => t.dismiss(), 1000); } setVideoId(contentId); } diff --git a/src/popup/utils.ts b/src/popup/utils.ts index 5c854d7..f803717 100644 --- a/src/popup/utils.ts +++ b/src/popup/utils.ts @@ -55,7 +55,10 @@ export const isVideoId = (id: string) => { }; export function ErrorMessage( - toast: (props: { title: string; description: string }) => void, + toast: (props: { + title: string; + description: string; + }) => ReturnType, props?: { error?: Error; message?: { title: string; description: string }; @@ -63,11 +66,13 @@ export function ErrorMessage( ) { const { error, message } = props ?? {}; - toast({ + const t = toast({ title: message?.title || error?.name || "エラー", description: message?.description || error?.message || "予期しないエラーが発生しました。", }); + + return t; }