embed.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Field, { getValidationRules } from './_field';
  2. import { useFormContext } from 'react-hook-form';
  3. import { Input } from 'app/design/controls'
  4. import { useFetch } from 'app/lib/hooks/use-fetch';
  5. import { appSetting } from 'app/lib/util';
  6. import { View } from 'app/design/view'
  7. import Embed from 'app/ui/molecules/content/embed'
  8. import { useFormField } from 'app/lib/form/use-form-field';
  9. function InnerEmbed({ url }) {
  10. // keepPreviousData: the old preview stays while the next URL resolves.
  11. const { data: response } = useFetch(
  12. // Encoded: the URL is a query value (`params[]=`), its own `&` would split it.
  13. url ? `/api.php?r=${appSetting("urls", "embeds_new")}${encodeURIComponent(url)}` : null,
  14. { keepPreviousData: true }
  15. );
  16. if (url && response?.data) {
  17. return (
  18. <View className="w-full mt-2 max-w-md">
  19. <Embed data={response.data} />
  20. </View>
  21. );
  22. }
  23. return null;
  24. }
  25. export default function FormFieldEmbed(props) {
  26. const {
  27. name,
  28. field,
  29. placeholder,
  30. placeholderTextColor,
  31. focused,
  32. onFocus,
  33. onBlur,
  34. isAdaptiveLabel,
  35. } = useFormField(props, {
  36. rules: getValidationRules(props),
  37. });
  38. const video_source = useFormContext().watch('video_source');
  39. if (video_source == 'upload') return null;
  40. return (
  41. <Field {...props} value={field.value} focused={focused} isAdaptiveLabel={isAdaptiveLabel}>
  42. <Input
  43. autoFocus={true}
  44. name={name}
  45. placeholder={placeholder}
  46. placeholderTextColor={placeholderTextColor}
  47. onChangeText={field.onChange}
  48. onFocus={onFocus}
  49. onBlur={onBlur}
  50. value={String(field.value)}
  51. aria-label={props.caption}
  52. />
  53. <InnerEmbed url={field.value} />
  54. </Field>
  55. );
  56. }