From d5b37b6b8b644da56dd7ada9e545192cf3a52886 Mon Sep 17 00:00:00 2001 From: pajowu Date: Sun, 10 Dec 2023 01:45:10 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20podlove=20expor?= =?UTF-8?q?t=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/editor/export/webvtt.tsx | 111 ++++++++++-------------- frontend/src/utils/export_to_podlove.ts | 68 +++++---------- 2 files changed, 65 insertions(+), 114 deletions(-) diff --git a/frontend/src/editor/export/webvtt.tsx b/frontend/src/editor/export/webvtt.tsx index b9b47278..fe45992d 100644 --- a/frontend/src/editor/export/webvtt.tsx +++ b/frontend/src/editor/export/webvtt.tsx @@ -1,4 +1,4 @@ -import { useMemo, useState } from 'react'; +import { useEffect, useMemo, useState } from 'react'; import * as Automerge from '@automerge/automerge'; import { Checkbox, FormControl, Input, Select } from '../../components/form'; @@ -10,18 +10,30 @@ import { ExportProps } from '.'; import { PrimaryButton, SecondaryButton, IconButton } from '../../components/button'; import { BsEye, BsEyeSlash } from 'react-icons/bs'; +type ExportFormat = SubtitleFormat | 'podlove'; + export function WebVttExportBody({ onClose, outputNameBase, editor }: ExportProps) { const [includeSpeakerNames, setIncludeSpeakerNames] = useState(true); const [includeWordTimings, setIncludeWordTimings] = useState(false); const [limitLineLength, setLimitLineLength] = useState(false); const [maxLineLength, setMaxLineLength] = useState(60); + const [podloveEpisodeId, setPodloveEpisodeId] = useState(1); const [podloveUser, setPodloveUser] = useState(''); const [podloveShowApplicationId, setPodloveShowApplicationId] = useState(false); const [podloveApplicationId, setPodloveId] = useState(''); const [podloveUrl, setPodloveUrl] = useState(''); - const [podloveExportPossible, setPodloveExportPossible] = useState(true); - const [format, setFormat] = useState('vtt' as SubtitleFormat); + const [podloveExportPossible, setPodloveExportPossible] = useState(false); + useEffect(() => { + checkIsPodloveExportPossible( + podloveEpisodeId, + podloveUser, + podloveApplicationId, + podloveUrl, + ).then(setPodloveExportPossible); + }, [podloveEpisodeId, podloveUser, podloveApplicationId, podloveApplicationId, podloveUrl]); + + const [format, setFormat] = useState('vtt' as ExportFormat); const canExport = useMemo(() => canGenerateVtt(editor.doc.children), [editor.v]); return ( @@ -36,11 +48,6 @@ export function WebVttExportBody({ onClose, outputNameBase, editor }: ExportProp e.target.value === 'podlove' ) { setFormat(e.target.value); - if (e.target.value == 'srt' || e.target.value == 'vtt') { - setPodloveExportPossible(true); - } else { - setPodloveExportPossible(false); - } } }} > @@ -49,42 +56,15 @@ export function WebVttExportBody({ onClose, outputNameBase, editor }: ExportProp - {format == 'vtt' || format == 'podlove' ? ( - setIncludeSpeakerNames(x)} - /> - ) : ( - <> - )} - {format == 'vtt' ? ( - { - setIncludeWordTimings(x); - }} - /> - ) : ( - <> - )} {format == 'podlove' ? ( <> - + { - setPodloveEpisodeId(parseInt(e.target.value)); - checkIsPodloveExportPossible( - parseInt(e.target.value), - podloveUser, - podloveApplicationId, - podloveUrl, - setPodloveExportPossible, - ); + setPodloveUrl(e.target.value); }} /> @@ -95,31 +75,17 @@ export function WebVttExportBody({ onClose, outputNameBase, editor }: ExportProp type="string" onChange={(e) => { setPodloveUser(e.target.value); - checkIsPodloveExportPossible( - podloveEpisodeId, - e.target.value, - podloveApplicationId, - podloveUrl, - setPodloveExportPossible, - ); }} /> -
+
{ setPodloveId(e.target.value); - checkIsPodloveExportPossible( - podloveEpisodeId, - podloveUser, - e.target.value, - podloveUrl, - setPodloveExportPossible, - ); }} />
- + { - setPodloveUrl(e.target.value); - checkIsPodloveExportPossible( - podloveEpisodeId, - podloveUser, - podloveApplicationId, - e.target.value, - setPodloveExportPossible, - ); + setPodloveEpisodeId(parseInt(e.target.value)); }} /> @@ -157,6 +116,26 @@ export function WebVttExportBody({ onClose, outputNameBase, editor }: ExportProp ) : ( <> )} + {format == 'vtt' || format == 'podlove' ? ( + setIncludeSpeakerNames(x)} + /> + ) : ( + <> + )} + {format == 'vtt' ? ( + { + setIncludeWordTimings(x); + }} + /> + ) : ( + <> + )} Export diff --git a/frontend/src/utils/export_to_podlove.ts b/frontend/src/utils/export_to_podlove.ts index 1e8c4600..ce990a3f 100644 --- a/frontend/src/utils/export_to_podlove.ts +++ b/frontend/src/utils/export_to_podlove.ts @@ -38,60 +38,32 @@ export function pushToPodlove( .catch((err) => console.error(err)); } -export function checkIsPodloveExportPossible( +export async function checkIsPodloveExportPossible( episodeId: number, user: string, appId: string, url: string, - setIsPodloveUploadPossible: (v: boolean) => void, -) { +): Promise { if (url.length < 1 || appId.length < 1 || user.length < 1 || episodeId < 1) { - setIsPodloveUploadPossible(false); + return false; } - const podloveUrlEpispde = url + '/wp-json/podlove/v2/episodes/' + episodeId.toString(); - fetch(podloveUrlEpispde, { - method: 'GET', - }) - .then((response) => { - // export the vtt to the podlove publisher - if (response.status === 200) { - setIsPodloveUploadPossible(true); - } else { - if (response.status === 401) { - fetch(podloveUrlEpispde, { - method: 'GET', - headers: { - 'Content-type': 'application/json;charset=UTF-8', - Authorization: `Basic ${btoa(`${user}:${appId}`)}`, - }, - }) - .then((response) => { - if (response.status === 200) { - setIsPodloveUploadPossible(true); - } else { - if (response.status === 401) { - setIsPodloveUploadPossible(false); - } - if (response.status === 404) { - setIsPodloveUploadPossible(false); - } - setIsPodloveUploadPossible(false); - } - }) - .catch((err) => { - console.error(err); - setIsPodloveUploadPossible(false); - }); - } - if (response.status === 404) { - setIsPodloveUploadPossible(false); - } - setIsPodloveUploadPossible(false); - } - }) - .catch((err) => { - console.error(err); - setIsPodloveUploadPossible(false); + const podloveUrlEpisode = url + '/wp-json/podlove/v2/episodes/' + episodeId.toString(); + try { + const response = await fetch(podloveUrlEpisode, { + method: 'GET', + headers: { + 'Content-type': 'application/json;charset=UTF-8', + Authorization: `Basic ${btoa(`${user}:${appId}`)}`, + }, }); + if (response.status === 200) { + return true; + } else { + return false; + } + } catch (err) { + console.log(err); + return false; + } }