|
| 1 | +import { Switch } from "@headlessui/react" |
| 2 | +import clsx from "clsx" |
| 3 | +import dayjs from "dayjs" |
| 4 | +import React, { useState } from "react" |
| 5 | +import { File } from "react-feather" |
| 6 | +import { |
| 7 | + ContentPlayerPlayingContent, |
| 8 | + InitPluginInRenderer, |
| 9 | + Program, |
| 10 | + Service, |
| 11 | +} from "../../@types/plugin" |
| 12 | + |
| 13 | +export const FileSelector: React.VFC<{ |
| 14 | + services: Service[] |
| 15 | + setPlayingContent: React.Dispatch< |
| 16 | + React.SetStateAction<ContentPlayerPlayingContent | null> |
| 17 | + > |
| 18 | + openContentPlayer: (_: ContentPlayerPlayingContent) => Promise<number> |
| 19 | + requestDialog: Parameters<InitPluginInRenderer>[0]["rpc"]["requestDialog"] |
| 20 | +}> = ({ services, setPlayingContent, openContentPlayer, requestDialog }) => { |
| 21 | + const [filePath, setFilePath] = useState("") |
| 22 | + const [startAt, setStartAt] = useState("") |
| 23 | + const [duration, setDuration] = useState(30) |
| 24 | + const [serviceId, setServiceId] = useState(-1) |
| 25 | + |
| 26 | + const [isOpenWithNewWindow, setIsOpenWithNewWindow] = useState(false) |
| 27 | + |
| 28 | + return ( |
| 29 | + <div className="w-full h-full flex flex-col"> |
| 30 | + <div className="w-full bg-gray-800 text-gray-200"> |
| 31 | + <div className="w-full py-2 pl-4 pr-2 flex items-center justify-between"> |
| 32 | + <h2 className="font-semibold text-lg">ローカルファイル再生</h2> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | + <div className="w-full flex overflow-auto p-4"> |
| 36 | + <form |
| 37 | + onSubmit={(e) => { |
| 38 | + e.preventDefault() |
| 39 | + if (!filePath) { |
| 40 | + return |
| 41 | + } |
| 42 | + const contentType = "Local" |
| 43 | + const url = "file://" + filePath |
| 44 | + const service = services.find( |
| 45 | + (service) => service.serviceId === serviceId |
| 46 | + ) |
| 47 | + const program: Program | undefined = startAt |
| 48 | + ? { |
| 49 | + startAt: dayjs(startAt).unix() * 1000, |
| 50 | + duration: duration * 1000 * 60, |
| 51 | + id: -1, |
| 52 | + networkId: -1, |
| 53 | + serviceId, |
| 54 | + eventId: -1, |
| 55 | + isFree: true, |
| 56 | + } |
| 57 | + : undefined |
| 58 | + const contentPlayload = { |
| 59 | + contentType, |
| 60 | + url, |
| 61 | + program, |
| 62 | + service, |
| 63 | + } |
| 64 | + console.info("再生します:", contentPlayload) |
| 65 | + if (isOpenWithNewWindow) { |
| 66 | + openContentPlayer(contentPlayload) |
| 67 | + } else { |
| 68 | + setPlayingContent(contentPlayload) |
| 69 | + } |
| 70 | + }} |
| 71 | + > |
| 72 | + <label className="block w-full"> |
| 73 | + <span>ファイルを選択</span> |
| 74 | + <div className="flex justify-center flex-grow"> |
| 75 | + <input |
| 76 | + type="text" |
| 77 | + className={clsx( |
| 78 | + "block mt-1 form-input rounded-l-md w-full text-gray-900 focus:outline-none cursor-pointer" |
| 79 | + )} |
| 80 | + value={filePath || ""} |
| 81 | + onChange={(e) => setFilePath(e.target.value)} |
| 82 | + spellCheck={false} |
| 83 | + /> |
| 84 | + <button |
| 85 | + className={clsx( |
| 86 | + `px-4 py-2 mt-1 rounded-r-md flex items-center justify-center bg-gray-200 text-gray-900 focus:outline-none cursor-pointer` |
| 87 | + )} |
| 88 | + onClick={async () => { |
| 89 | + const dialog = await requestDialog({ |
| 90 | + properties: ["openFile"], |
| 91 | + }) |
| 92 | + if (dialog.canceled) { |
| 93 | + return |
| 94 | + } |
| 95 | + const path = dialog.filePaths.slice(0).shift() |
| 96 | + if (!path) { |
| 97 | + return |
| 98 | + } |
| 99 | + setFilePath(path) |
| 100 | + }} |
| 101 | + > |
| 102 | + <File className="pointer-events-none" size="1.75rem" /> |
| 103 | + </button> |
| 104 | + </div> |
| 105 | + </label> |
| 106 | + <div className={clsx("flex", "space-x-2")}> |
| 107 | + <label className="block mt-2"> |
| 108 | + <span>開始時間</span> |
| 109 | + <input |
| 110 | + type="datetime-local" |
| 111 | + className="block mt-1 form-input rounded-md w-full text-gray-900" |
| 112 | + value={startAt || ""} |
| 113 | + onChange={(e) => setStartAt(e.target.value)} |
| 114 | + /> |
| 115 | + </label> |
| 116 | + <label className="block mt-2"> |
| 117 | + <span>長さ</span> |
| 118 | + <input |
| 119 | + type="number" |
| 120 | + className="block mt-1 form-input rounded-md w-full text-gray-900" |
| 121 | + value={duration} |
| 122 | + onChange={(e) => { |
| 123 | + const p = parseInt(e.target.value) |
| 124 | + if (Number.isNaN(p)) { |
| 125 | + return |
| 126 | + } |
| 127 | + setDuration(p) |
| 128 | + }} |
| 129 | + /> |
| 130 | + {startAt ? ( |
| 131 | + <span>{dayjs(startAt).add(duration, "minutes").format()}</span> |
| 132 | + ) : ( |
| 133 | + <span>未選択</span> |
| 134 | + )} |
| 135 | + </label> |
| 136 | + </div> |
| 137 | + <select |
| 138 | + className="appearance-none border rounded py-2 px-2 mt-2 leading-tight focus:outline-none" |
| 139 | + value={serviceId} |
| 140 | + onChange={(e) => { |
| 141 | + const selectedServiceId = parseInt(e.target.value) |
| 142 | + if (Number.isNaN(selectedServiceId)) { |
| 143 | + setServiceId(-1) |
| 144 | + return |
| 145 | + } |
| 146 | + setServiceId(selectedServiceId) |
| 147 | + }} |
| 148 | + > |
| 149 | + <option value="-1" defaultChecked> |
| 150 | + 選択解除 |
| 151 | + </option> |
| 152 | + {services.map((service) => { |
| 153 | + return ( |
| 154 | + <option key={service.serviceId} value={service.serviceId}> |
| 155 | + {service.name} |
| 156 | + </option> |
| 157 | + ) |
| 158 | + })} |
| 159 | + </select> |
| 160 | + <Switch.Group> |
| 161 | + <div className="flex items-center mt-4"> |
| 162 | + <Switch |
| 163 | + checked={isOpenWithNewWindow} |
| 164 | + onChange={(e) => setIsOpenWithNewWindow(e)} |
| 165 | + className={`${ |
| 166 | + isOpenWithNewWindow ? "bg-blue-600" : "bg-gray-300" |
| 167 | + } relative inline-flex items-center h-6 rounded-full w-11`} |
| 168 | + > |
| 169 | + <span |
| 170 | + className={`${ |
| 171 | + isOpenWithNewWindow ? "translate-x-6" : "translate-x-1" |
| 172 | + } inline-block w-4 h-4 transform bg-white rounded-full transition ease-in-out duration-200`} |
| 173 | + /> |
| 174 | + </Switch> |
| 175 | + <Switch.Label className="ml-2"> |
| 176 | + 新しいウィンドウで開く |
| 177 | + </Switch.Label> |
| 178 | + </div> |
| 179 | + </Switch.Group> |
| 180 | + <button |
| 181 | + type="submit" |
| 182 | + className="bg-blue-500 text-gray-100 p-2 px-3 my-4 rounded-md focus:outline-none cursor-pointer active:bg-gray-200" |
| 183 | + > |
| 184 | + 再生 |
| 185 | + </button> |
| 186 | + </form> |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + ) |
| 190 | +} |
0 commit comments