import type { FC } from 'react'; import { useMemo } from 'react'; import type { SelectedServer } from '../servers/data'; import type { Settings, ShortUrlCreationSettings } from '../settings/reducers/settings'; import type { ShortUrlData } from './data'; import type { ShortUrlCreation } from './reducers/shortUrlCreation'; import type { CreateShortUrlResultProps } from './helpers/CreateShortUrlResult'; import type { ShortUrlFormProps } from './ShortUrlForm'; export interface CreateShortUrlProps { basicMode?: boolean; } interface CreateShortUrlConnectProps extends CreateShortUrlProps { settings: Settings; shortUrlCreation: ShortUrlCreation; selectedServer: SelectedServer; createShortUrl: (data: ShortUrlData) => Promise; resetCreateShortUrl: () => void; } const getInitialState = (settings?: ShortUrlCreationSettings): ShortUrlData => ({ longUrl: '', tags: [], customSlug: '', title: undefined, shortCodeLength: undefined, domain: '', validSince: undefined, validUntil: undefined, maxVisits: undefined, findIfExists: false, validateUrl: settings?.validateUrls ?? false, forwardQuery: settings?.forwardQuery ?? true, }); export const CreateShortUrl = ( ShortUrlForm: FC, CreateShortUrlResult: FC, ) => ({ createShortUrl, shortUrlCreation, resetCreateShortUrl, selectedServer, basicMode = false, settings: { shortUrlCreation: shortUrlCreationSettings }, }: CreateShortUrlConnectProps) => { const initialState = useMemo(() => getInitialState(shortUrlCreationSettings), [shortUrlCreationSettings]); return ( <> { resetCreateShortUrl(); return createShortUrl(data); }} /> ); };