forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change embedded posts to use web UI (mastodon#31766)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
- Loading branch information
1 parent
f2a92c2
commit 3d46f47
Showing
115 changed files
with
710 additions
and
1,928 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import './public-path'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
import { afterInitialRender } from 'mastodon/../hooks/useRenderSignal'; | ||
|
||
import { start } from '../mastodon/common'; | ||
import { Status } from '../mastodon/features/standalone/status'; | ||
import { loadPolyfills } from '../mastodon/polyfills'; | ||
import ready from '../mastodon/ready'; | ||
|
||
start(); | ||
|
||
function loaded() { | ||
const mountNode = document.getElementById('mastodon-status'); | ||
|
||
if (mountNode) { | ||
const attr = mountNode.getAttribute('data-props'); | ||
|
||
if (!attr) return; | ||
|
||
const props = JSON.parse(attr) as { id: string; locale: string }; | ||
const root = createRoot(mountNode); | ||
|
||
root.render(<Status {...props} />); | ||
} | ||
} | ||
|
||
function main() { | ||
ready(loaded).catch((error: unknown) => { | ||
console.error(error); | ||
}); | ||
} | ||
|
||
loadPolyfills() | ||
.then(main) | ||
.catch((error: unknown) => { | ||
console.error(error); | ||
}); | ||
|
||
interface SetHeightMessage { | ||
type: 'setHeight'; | ||
id: string; | ||
height: number; | ||
} | ||
|
||
function isSetHeightMessage(data: unknown): data is SetHeightMessage { | ||
if ( | ||
data && | ||
typeof data === 'object' && | ||
'type' in data && | ||
data.type === 'setHeight' | ||
) | ||
return true; | ||
else return false; | ||
} | ||
|
||
window.addEventListener('message', (e) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- typings are not correct, it can be null in very rare cases | ||
if (!e.data || !isSetHeightMessage(e.data) || !window.parent) return; | ||
|
||
const data = e.data; | ||
|
||
// We use a timeout to allow for the React page to render before calculating the height | ||
afterInitialRender(() => { | ||
window.parent.postMessage( | ||
{ | ||
type: 'setHeight', | ||
id: data.id, | ||
height: document.getElementsByTagName('html')[0]?.scrollHeight, | ||
}, | ||
'*', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// This hook allows a component to signal that it's done rendering in a way that | ||
// can be used by e.g. our embed code to determine correct iframe height | ||
|
||
let renderSignalReceived = false; | ||
|
||
type Callback = () => void; | ||
|
||
let onInitialRender: Callback; | ||
|
||
export const afterInitialRender = (callback: Callback) => { | ||
if (renderSignalReceived) { | ||
callback(); | ||
} else { | ||
onInitialRender = callback; | ||
} | ||
}; | ||
|
||
export const useRenderSignal = () => { | ||
return () => { | ||
if (renderSignalReceived) { | ||
return; | ||
} | ||
|
||
renderSignalReceived = true; | ||
|
||
if (typeof onInitialRender !== 'undefined') { | ||
window.requestAnimationFrame(() => { | ||
onInitialRender(); | ||
}); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app/javascript/mastodon/features/standalone/status/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-return, | ||
@typescript-eslint/no-explicit-any, | ||
@typescript-eslint/no-unsafe-assignment */ | ||
|
||
import { useEffect, useCallback } from 'react'; | ||
|
||
import { Provider } from 'react-redux'; | ||
|
||
import { useRenderSignal } from 'mastodon/../hooks/useRenderSignal'; | ||
import { fetchStatus, toggleStatusSpoilers } from 'mastodon/actions/statuses'; | ||
import { hydrateStore } from 'mastodon/actions/store'; | ||
import { Router } from 'mastodon/components/router'; | ||
import { DetailedStatus } from 'mastodon/features/status/components/detailed_status'; | ||
import initialState from 'mastodon/initial_state'; | ||
import { IntlProvider } from 'mastodon/locales'; | ||
import { makeGetStatus, makeGetPictureInPicture } from 'mastodon/selectors'; | ||
import { store, useAppSelector, useAppDispatch } from 'mastodon/store'; | ||
|
||
const getStatus = makeGetStatus() as unknown as (arg0: any, arg1: any) => any; | ||
const getPictureInPicture = makeGetPictureInPicture() as unknown as ( | ||
arg0: any, | ||
arg1: any, | ||
) => any; | ||
|
||
const Embed: React.FC<{ id: string }> = ({ id }) => { | ||
const status = useAppSelector((state) => getStatus(state, { id })); | ||
const pictureInPicture = useAppSelector((state) => | ||
getPictureInPicture(state, { id }), | ||
); | ||
const domain = useAppSelector((state) => state.meta.get('domain')); | ||
const dispatch = useAppDispatch(); | ||
const dispatchRenderSignal = useRenderSignal(); | ||
|
||
useEffect(() => { | ||
dispatch(fetchStatus(id, false, false)); | ||
}, [dispatch, id]); | ||
|
||
const handleToggleHidden = useCallback(() => { | ||
dispatch(toggleStatusSpoilers(id)); | ||
}, [dispatch, id]); | ||
|
||
// This allows us to calculate the correct page height for embeds | ||
if (status) { | ||
dispatchRenderSignal(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
const permalink = status?.get('url') as string; | ||
|
||
return ( | ||
<div className='embed'> | ||
<DetailedStatus | ||
status={status} | ||
domain={domain} | ||
pictureInPicture={pictureInPicture} | ||
onToggleHidden={handleToggleHidden} | ||
withLogo | ||
/> | ||
|
||
<a | ||
className='embed__overlay' | ||
href={permalink} | ||
target='_blank' | ||
rel='noreferrer noopener' | ||
aria-label='' | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export const Status: React.FC<{ id: string }> = ({ id }) => { | ||
useEffect(() => { | ||
if (initialState) { | ||
store.dispatch(hydrateStore(initialState)); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<IntlProvider> | ||
<Provider store={store}> | ||
<Router> | ||
<Embed id={id} /> | ||
</Router> | ||
</Provider> | ||
</IntlProvider> | ||
); | ||
}; |
Oops, something went wrong.