import { h, render } from 'preact' import { useState } from 'preact/hooks' import { getExtensionSettingsAsync, targetPlatformSettings } from '../common/settings' import { getFileContent, getSubsFromCsv, getSubsFromJson, getSubsFromOpml } from '../common/yt' import { resolveById } from '../common/yt/urlResolve' import readme from './README.md' /** * Parses the subscription file and queries the API for lbry channels * * @param file to read * @returns a promise with the list of channels that were found on lbry */ async function lbryChannelsFromFile(file: File) { const ext = file.name.split('.').pop()?.toLowerCase() const ids = new Set(( ext === 'xml' || ext == 'opml' ? getSubsFromOpml : ext === 'csv' ? getSubsFromCsv : getSubsFromJson)(await getFileContent(file))) const items = await resolveById( Array.from(ids).map(id => ({ id, type: 'channel' } as const)), (progress) => render(, document.getElementById('root')!)) const { targetPlatform: platform } = await getExtensionSettingsAsync() const urlPrefix = targetPlatformSettings[platform].domainPrefix return Object.values(items).map((item) => urlPrefix + item.id) } function ConversionCard({ onSelect, progress }: { onSelect(file: File): Promise | void, progress: number }) { const [file, setFile] = useState(null as File | null) const [isLoading, setLoading] = useState(false) return Select YouTube Subscriptions setFile(e.currentTarget.files?.length ? e.currentTarget.files[0] : null)} /> { if (!file) return setLoading(true) await onSelect(file) setLoading(false) }} /> {progress > 0 ? `${(progress * 100).toFixed(1)}%` : ''} } function YTtoLBRY({ progress }: { progress: number }) { const [lbryChannels, setLbryChannels] = useState([] as string[]) return setLbryChannels(await lbryChannelsFromFile(file))} /> {lbryChannels.map((x, i) => } />)} } render(, document.getElementById('root')!)