Skip to content

Commit

Permalink
refactor: frontend logic
Browse files Browse the repository at this point in the history
  • Loading branch information
xpadev-net committed Nov 22, 2023
1 parent 6c866f2 commit 9601aab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/controller/comment-picker/remote/remote-comment-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TextField } from "@mui/material";
import Button from "@mui/material/Button";
import { useSetAtom } from "jotai";
import type { ChangeEvent, FC } from "react";
import { useState } from "react";
import { useRef, useState } from "react";

import type { TCommentOption, TWatchV3Metadata } from "@/@types/niconico";
import type { TCommentItemRemote } from "@/@types/queue";
Expand All @@ -26,14 +26,24 @@ const RemoteCommentPicker: FC<Props> = ({ onChange }) => {
>(undefined);
const setMessage = useSetAtom(messageAtom);
const setIsLoading = useSetAtom(isLoadingAtom);
const lastUrl = useRef<string>("");

const onUrlChange = (e: ChangeEvent<HTMLInputElement>): void => {
setMetadata(undefined);
setUrl(e.target.value);
};
const updateMetadata = (): void => {
void (async () => {
const nicoId = getNicoId(url);
if (!isNicovideoUrl(url) || metadata || !nicoId) return;
if (!isNicovideoUrl(url) || metadata || !nicoId) {
if (!url || lastUrl.current === nicoId) return;
setMessage({
title: "URLが正しくありません",
content:
"以下のような形式のURLを入力してください\nhttps://www.nicovideo.jp/watch/sm9\nhttps://nico.ms/sm9\ncontroller/movie-picker/remote/remote-movie-picker.tsx / getFormats",
});
return;
}
setIsLoading(true);
const targetMetadata = (await window.api.request({
type: "getNiconicoMovieMetadata",
Expand All @@ -56,6 +66,7 @@ const RemoteCommentPicker: FC<Props> = ({ onChange }) => {
return;
}
setMetadata(targetMetadata);
lastUrl.current = nicoId;
})();
};
const onClick = (): void => {
Expand Down
17 changes: 12 additions & 5 deletions src/controller/movie-picker/remote/remote-movie-picker.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FormControlLabel, Radio, RadioGroup, TextField } from "@mui/material";
import Button from "@mui/material/Button";
import { useSetAtom } from "jotai";
import type { ChangeEvent, FC } from "react";
import { useState } from "react";
import type { ChangeEvent, FC, KeyboardEvent } from "react";
import { useRef, useState } from "react";

import type { TWatchV3Metadata } from "@/@types/niconico";
import type { TMovieItemRemote, TRemoteMovieItemFormat } from "@/@types/queue";
Expand All @@ -26,6 +26,7 @@ const RemoteMoviePicker: FC<Props> = ({ onChange }) => {
const [format, setFormat] = useState<TRemoteMovieItemFormat | undefined>();
const setMessage = useSetAtom(messageAtom);
const setIsLoading = useSetAtom(isLoadingAtom);
const lastUrl = useRef<string>("");
const onUrlChange = (e: ChangeEvent<HTMLInputElement>): void => {
setMetadata(undefined);
setUrl(e.target.value);
Expand All @@ -34,11 +35,11 @@ const RemoteMoviePicker: FC<Props> = ({ onChange }) => {
void (async () => {
const nicoId = getNicoId(url);
if (!isNicovideoUrl(url) || metadata || !nicoId) {
if (!url) return;
if (!url || lastUrl.current === nicoId) return;
setMessage({
title: "URLが正しくありません",
content:
"以下のような形式のURLを入力してください\nhttps://www.nicovideo.jp/watch/sm9\nhttps://nico.ms/sm9",
"以下のような形式のURLを入力してください\nhttps://www.nicovideo.jp/watch/sm9\nhttps://nico.ms/sm9\ncontroller/movie-picker/remote/remote-movie-picker.tsx / getFormats",
});
return;
}
Expand All @@ -65,16 +66,21 @@ const RemoteMoviePicker: FC<Props> = ({ onChange }) => {
}
setMediaServer(targetMetadata.data.media.domand ? "dms" : "dmc");
setMetadata(targetMetadata);
lastUrl.current = nicoId;
})();
};
const onKeyDown = (e: KeyboardEvent<HTMLInputElement>): void => {
if (e.key !== "Enter") return;
getFormats();
};
const onClick = (): void => {
void (async () => {
const nicoId = getNicoId(url);
if (!nicoId) {
setMessage({
title: "URLが正しくありません",
content:
"以下のような形式のURLを入力してください\nhttps://www.nicovideo.jp/watch/sm9\nhttps://nico.ms/sm9",
"以下のような形式のURLを入力してください\nhttps://www.nicovideo.jp/watch/sm9\nhttps://nico.ms/sm9\ncontroller/movie-picker/remote/remote-movie-picker.tsx / onClick",
});
return;
}
Expand Down Expand Up @@ -122,6 +128,7 @@ const RemoteMoviePicker: FC<Props> = ({ onChange }) => {
value={url}
onChange={onUrlChange}
onBlur={getFormats}
onKeyDown={onKeyDown}
fullWidth={true}
/>
{metadata && (
Expand Down

0 comments on commit 9601aab

Please sign in to comment.