@@ -7,6 +7,14 @@ import { FormattedMessage } from "react-intl";
77import { useTextParam } from "./utils" ;
88import { clsx } from "clsx" ;
99
10+ interface WebfingerResponseJSON {
11+ links : {
12+ rel : string ;
13+ type : string ;
14+ href : string ;
15+ } [ ] ;
16+ }
17+
1018interface ServerResponseJSON {
1119 domain : string ;
1220}
@@ -36,6 +44,40 @@ const extractDomain = (str: string): string => {
3644 return value ;
3745} ;
3846
47+ const fetchTemplate = (
48+ domain : string ,
49+ text : string ,
50+ ) : Promise < [ string , string ] > => {
51+ let fallbackDomain ;
52+
53+ return fetch (
54+ `https://${ domain } /.well-known/webfinger?resource=${ encodeURIComponent ( `https://${ domain } ` ) } ` ,
55+ )
56+ . then ( ( response ) => {
57+ if ( ! response . ok ) {
58+ throw new Error ( "Unexpected response" ) ;
59+ }
60+
61+ // If the server is using a different domain/subdomain to serve the UI
62+ fallbackDomain = new URL ( response . url ) . host ;
63+
64+ return response . json ( ) ;
65+ } )
66+ . then ( ( json : WebfingerResponseJSON ) => {
67+ const link = json . links . find (
68+ ( { rel } ) => rel === "https://w3id.org/fep/3b86/Create" ,
69+ ) ;
70+ const template =
71+ link ?. template ?? `https://${ fallbackDomain } /share?text={content}` ;
72+ const redirectUrl = template . replace (
73+ "{content}" ,
74+ encodeURIComponent ( text ) ,
75+ ) ;
76+
77+ return [ template , redirectUrl ] ;
78+ } ) ;
79+ } ;
80+
3981export const NewDomain : React . FC < { onDismiss : ( ) => void } > = ( ) => {
4082 const dispatch = useAppDispatch ( ) ;
4183 const text = useTextParam ( ) ;
@@ -118,15 +160,15 @@ export const NewDomain: React.FC<{ onDismiss: () => void }> = () => {
118160
119161 setValue ( domain ) ;
120162 setShowResults ( false ) ;
121-
122- const redirectUrl = `https://${ domain } /share?text=${ encodeURIComponent ( text ) } ` ;
123-
124163 setSubmitting ( true ) ;
125- dispatch ( addDomain ( domain ) ) ;
126164
127- setTimeout ( ( ) => {
128- window . location . href = redirectUrl ;
129- } , 100 ) ;
165+ fetchTemplate ( domain , text ) . then ( ( [ template , redirectUrl ] ) => {
166+ dispatch ( addDomain ( { domain, template } ) ) ;
167+
168+ setTimeout ( ( ) => {
169+ window . location . href = redirectUrl ;
170+ } , 100 ) ;
171+ } ) ;
130172 } ,
131173 [ dispatch , text ] ,
132174 ) ;
@@ -141,14 +183,15 @@ export const NewDomain: React.FC<{ onDismiss: () => void }> = () => {
141183 return ;
142184 }
143185
144- const redirectUrl = `https://${ domain } /share?text=${ encodeURIComponent ( text ) } ` ;
145-
146186 setSubmitting ( true ) ;
147- dispatch ( addDomain ( domain ) ) ;
148187
149- setTimeout ( ( ) => {
150- window . location . href = redirectUrl ;
151- } , 100 ) ;
188+ fetchTemplate ( domain , text ) . then ( ( [ template , redirectUrl ] ) => {
189+ dispatch ( addDomain ( { domain, template } ) ) ;
190+
191+ setTimeout ( ( ) => {
192+ window . location . href = redirectUrl ;
193+ } , 100 ) ;
194+ } ) ;
152195 } ,
153196 [ dispatch , text , domain ] ,
154197 ) ;
0 commit comments