Skip to content

Commit

Permalink
removed derived atom from async cookies atom (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopiovanello authored Feb 4, 2025
1 parent 1141903 commit 65960fb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dev": "vite --host 0.0.0.0",
"build": "vite build"
},
"type": "module",
"author": "marcopiovanello",
"license": "GPL-3.0-only",
"private": true,
Expand Down
10 changes: 2 additions & 8 deletions frontend/src/atoms/downloadTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getOrElse } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/function'
import { atom } from 'jotai'
import { atomWithStorage } from 'jotai/utils'
import { ffetch } from '../lib/httpClient'
import { CustomTemplate } from '../types'
import { serverSideCookiesState, serverURL } from './settings'
import { atom } from 'jotai'
import { atomWithStorage } from 'jotai/utils'

export const cookiesTemplateState = atom<Promise<string>>(async (get) =>
await get(serverSideCookiesState)
Expand All @@ -22,12 +22,6 @@ export const filenameTemplateState = atomWithStorage(
localStorage.getItem('lastFilenameTemplate') ?? ''
)

export const downloadTemplateState = atom<Promise<string>>(async (get) =>
`${get(customArgsState)} ${await get(cookiesTemplateState)}`
.replace(/ +/g, ' ')
.trim()
)

export const savedTemplatesState = atom<Promise<CustomTemplate[]>>(async (get) => {
const task = ffetch<CustomTemplate[]>(`${get(serverURL)}/api/v1/template/all`)
const either = await task()
Expand Down
35 changes: 35 additions & 0 deletions frontend/src/components/CustomArgsTextField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TextField } from '@mui/material'
import { useAtom, useAtomValue } from 'jotai'
import { customArgsState } from '../atoms/downloadTemplate'
import { settingsState } from '../atoms/settings'
import { useI18n } from '../hooks/useI18n'
import { useEffect } from 'react'

const CustomArgsTextField: React.FC = () => {
const { i18n } = useI18n()

const settings = useAtomValue(settingsState)

const [customArgs, setCustomArgs] = useAtom(customArgsState)

useEffect(() => {
setCustomArgs('')
}, [])

const handleCustomArgsChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setCustomArgs(e.target.value)
}

return (
<TextField
fullWidth
label={i18n.t('customArgsInput')}
variant="outlined"
onChange={handleCustomArgsChange}
value={customArgs}
disabled={settings.formatSelection}
/>
)
}

export default CustomArgsTextField
60 changes: 22 additions & 38 deletions frontend/src/components/DownloadDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ import {
FC,
Suspense,
forwardRef,
useEffect,
useRef,
useState,
useTransition
} from 'react'
import {
cookiesTemplateState,
customArgsState,
downloadTemplateState,
filenameTemplateState,
savedTemplatesState
} from '../atoms/downloadTemplate'
Expand All @@ -47,6 +46,7 @@ import { useI18n } from '../hooks/useI18n'
import { useRPC } from '../hooks/useRPC'
import type { DLMetadata } from '../types'
import { toFormatArgs } from '../utils'
import CustomArgsTextField from './CustomArgsTextField'
import ExtraDownloadOptions from './ExtraDownloadOptions'
import LoadingBackdrop from './LoadingBackdrop'

Expand All @@ -69,17 +69,16 @@ const DownloadDialog: FC<Props> = ({ open, onClose, onDownloadStart }) => {
const settings = useAtomValue(settingsState)
const isConnected = useAtomValue(connectedState)
const availableDownloadPaths = useAtomValue(availableDownloadPathsState)
const downloadTemplate = useAtomValue(downloadTemplateState)
const savedTemplates = useAtomValue(savedTemplatesState)
const customArgs = useAtomValue(customArgsState)
const cookies = useAtomValue(cookiesTemplateState)

const [downloadFormats, setDownloadFormats] = useState<DLMetadata>()
const [pickedVideoFormat, setPickedVideoFormat] = useState('')
const [pickedAudioFormat, setPickedAudioFormat] = useState('')
const [pickedBestFormat, setPickedBestFormat] = useState('')
const [isFormatsLoading, setIsFormatsLoading] = useState(false)

const [customArgs, setCustomArgs] = useAtom(customArgsState)

const [downloadPath, setDownloadPath] = useState('')

const [filenameTemplate, setFilenameTemplate] = useAtom(
Expand All @@ -101,10 +100,6 @@ const DownloadDialog: FC<Props> = ({ open, onClose, onDownloadStart }) => {

const [isPending, startTransition] = useTransition()

useEffect(() => {
setCustomArgs('')
}, [open])

/**
* Retrive url from input, cli-arguments from checkboxes and emits via WebSocket
*/
Expand All @@ -115,6 +110,10 @@ const DownloadDialog: FC<Props> = ({ open, onClose, onDownloadStart }) => {
if (pickedAudioFormat !== '') codes.push(pickedAudioFormat)
if (pickedBestFormat !== '') codes.push(pickedBestFormat)

const downloadTemplate = `${customArgsState} ${cookies}`
.replace(/ +/g, ' ')
.trim()

await new Promise(r => setTimeout(r, 10))
client.download({
url: immediate || line,
Expand Down Expand Up @@ -178,10 +177,6 @@ const DownloadDialog: FC<Props> = ({ open, onClose, onDownloadStart }) => {
setFileExtension(e.target.value)
}

const handleCustomArgsChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setCustomArgs(e.target.value)
}

const parseUrlListFile = async (e: React.ChangeEvent<HTMLInputElement>) => {
const files = e.currentTarget.files
if (!files || files.length < 1) {
Expand Down Expand Up @@ -277,33 +272,22 @@ const DownloadDialog: FC<Props> = ({ open, onClose, onDownloadStart }) => {
/>
</Grid>
<Grid container spacing={1} sx={{ mt: 1 }}>
{
settings.enableCustomArgs &&
{settings.enableCustomArgs &&
<Grid item xs={12}>
<TextField
fullWidth
label={i18n.t('customArgsInput')}
variant="outlined"
onChange={handleCustomArgsChange}
value={customArgs}
disabled={
!isConnected ||
(settings.formatSelection && downloadFormats != null)
}
/>
<CustomArgsTextField />
</Grid>
}
{
settings.fileRenaming &&
<Grid item xs={
!settings.autoFileExtension && !settings.pathOverriding
? 12
: !settings.autoFileExtension && settings.pathOverriding
? 8
: settings.autoFileExtension && !settings.pathOverriding
? 10
: 6
}>
!settings.autoFileExtension && !settings.pathOverriding
? 12
: !settings.autoFileExtension && settings.pathOverriding
? 8
: settings.autoFileExtension && !settings.pathOverriding
? 10
: 6
}>
<TextField
sx={{ mt: 1 }}
ref={customFilenameInputRef}
Expand All @@ -329,10 +313,10 @@ const DownloadDialog: FC<Props> = ({ open, onClose, onDownloadStart }) => {
value={fileExtension}
onChange={handleFileExtensionChange}
variant="outlined">
<MenuItem value=".%(ext)s">Auto</MenuItem>
<MenuItem value=".mp4">mp4</MenuItem>
<MenuItem value=".mkv">mkv</MenuItem>
</Select>
<MenuItem value=".%(ext)s">Auto</MenuItem>
<MenuItem value=".mp4">mp4</MenuItem>
<MenuItem value=".mkv">mkv</MenuItem>
</Select>
</Grid>
}
{
Expand Down
File renamed without changes.

0 comments on commit 65960fb

Please sign in to comment.