From 491c0e9152bb1c87a67100ba1640d356633bd14a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 21 Aug 2022 12:37:14 -0400 Subject: [PATCH] EmbedModal: react-query, add useEmbed --- .../features/ui/components/embed_modal.tsx | 30 +++++-------------- app/soapbox/queries/embed.ts | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 app/soapbox/queries/embed.ts diff --git a/app/soapbox/features/ui/components/embed_modal.tsx b/app/soapbox/features/ui/components/embed_modal.tsx index 36f6e2270..e83e968b5 100644 --- a/app/soapbox/features/ui/components/embed_modal.tsx +++ b/app/soapbox/features/ui/components/embed_modal.tsx @@ -1,18 +1,9 @@ -import React, { useState, useEffect } from 'react'; +import React, { useEffect } from 'react'; import { FormattedMessage } from 'react-intl'; -import api from 'soapbox/api'; import SafeEmbed from 'soapbox/components/safe-embed'; import { Modal, Stack, Text, Input } from 'soapbox/components/ui'; -import { useAppDispatch } from 'soapbox/hooks'; - -import type { RootState } from 'soapbox/store'; - -const fetchEmbed = (url: string) => { - return (dispatch: any, getState: () => RootState) => { - return api(getState).get('/api/oembed', { params: { url } }); - }; -}; +import useEmbed from 'soapbox/queries/embed'; interface IEmbedModal { url: string, @@ -20,18 +11,13 @@ interface IEmbedModal { } const EmbedModal: React.FC = ({ url, onError }) => { - const dispatch = useAppDispatch(); - const [html, setHtml] = useState(''); + const { data: embed, error, isError } = useEmbed(url); useEffect(() => { - dispatch(fetchEmbed(url)).then(({ data }) => { - if (data?.html) { - setHtml(data.html); - } - }).catch(error => { + if (error && isError) { onError(error); - }); - }, []); + } + }, [isError]); const handleInputClick: React.MouseEventHandler = (e) => { e.currentTarget.select(); @@ -48,7 +34,7 @@ const EmbedModal: React.FC = ({ url, onError }) => { @@ -57,7 +43,7 @@ const EmbedModal: React.FC = ({ url, onError }) => { className='inline-flex rounded-xl overflow-hidden max-w-full' sandbox='allow-same-origin' title='embedded-status' - html={html} + html={embed?.html} /> diff --git a/app/soapbox/queries/embed.ts b/app/soapbox/queries/embed.ts new file mode 100644 index 000000000..e32403f88 --- /dev/null +++ b/app/soapbox/queries/embed.ts @@ -0,0 +1,30 @@ +import { useQuery } from '@tanstack/react-query'; + +import { useApi } from 'soapbox/hooks'; + +type Embed = { + type: string, + version: string, + author_name: string, + author_url: string, + provider_name: string, + provider_url: string, + cache_age: number, + html: string, + width: number, + height: number, +} + +/** Fetch OEmbed information for a status by its URL. */ +// https://github.com/mastodon/mastodon/blob/main/app/controllers/api/oembed_controller.rb +// https://github.com/mastodon/mastodon/blob/main/app/serializers/oembed_serializer.rb +export default function useEmbed(url: string) { + const api = useApi(); + + const getEmbed = async() => { + const { data } = await api.get('/api/oembed', { params: { url } }); + return data; + }; + + return useQuery(['embed', url], getEmbed); +}