Skip to content

Commit

Permalink
sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
the-kwisatz-haderach committed Jan 15, 2024
1 parent 26b4dbc commit 3fa774f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 25 deletions.
13 changes: 11 additions & 2 deletions components/Obituary/components/ObituaryLarge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const ObituaryLarge: ObituaryRenderer = ({
initialData: props,
})
const {
_id,
firstname,
name_misc,
surname,
Expand All @@ -51,12 +52,20 @@ export const ObituaryLarge: ObituaryRenderer = ({
} = data
const { t } = useTranslation()

const shareToFacebook = useCallback(() => {
const shareToFacebook = useCallback(async () => {
const shareUrl = `${window.location.origin}/${type}/${_id}`
window?.FB?.ui({
display: 'popup',
method: 'share',
href: window.location.href,
href: shareUrl,
})
// if (navigator?.canShare?.()) {
// await navigator.share({
// title: 'Herro',
// text: 'Learn web development on MDN!',
// url: 'https://developer.mozilla.org',
// })
// }
}, [])

return (
Expand Down
11 changes: 11 additions & 0 deletions lib/domain/formatLongText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IObituary } from './types'
import ReactDOMServer from 'react-dom/server'

import { render } from 'storyblok-rich-text-react-renderer'

const htmlTagsRegexp = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g

export const stringifyLongText = (text: IObituary['long_text']): string =>
typeof text === 'string'
? text.replace(htmlTagsRegexp, '')
: ReactDOMServer.renderToString(render(text)).replace(htmlTagsRegexp, '')
9 changes: 9 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ module.exports = (phase, { defaultConfig }) => {
},
],
},
// {
// source: '/',
// headers: [
// {
// key: 'Permissions-Policy',
// value: 'web-share=*',
// },
// ],
// },
]
},
async rewrites() {
Expand Down
8 changes: 6 additions & 2 deletions pages/[category]/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ObituaryLarge } from '../../components/Obituary/components/ObituaryLarg
import { connectToDb } from '../../db'
import { createMetaTitle } from '../../lib/domain'
import { IObituary } from '../../lib/domain/types'
import { stringifyLongText } from 'lib/domain/formatLongText'

const capitalize = (str = '') => str?.[0]?.toLocaleUpperCase() + str.slice(1)

Expand Down Expand Up @@ -49,8 +50,11 @@ export default function Obituary({
content={typeof image === 'string' ? image : image.filename}
/>
)}
{typeof long_text === 'string' && (
<meta property="og:description" content={long_text} />
{long_text && (
<meta
property="og:description"
content={stringifyLongText(long_text)}
/>
)}
<meta
property="og:title"
Expand Down
48 changes: 27 additions & 21 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ class MyDocument extends Document {
return (
<Html>
<Head>
<script
id="facebook"
async
dangerouslySetInnerHTML={{
__html: `window.fbAsyncInit = function() {
FB.init({
appId : '${process.env.NEXT_PUBLIC_FACEBOOK_APP_ID}',
xfbml : true,
version : 'v18.0'
});
FB.AppEvents.logPageView();
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));`,
}}
/>
<script
async
defer
crossOrigin="anonymous"
src="https://connect.facebook.net/en_US/sdk.js"
/>
{process.env.NODE_ENV === 'production' && (
<>
{/* <!-- Google Tag Manager --> */}
Expand Down Expand Up @@ -38,27 +65,6 @@ class MyDocument extends Document {
)}
<Main />
<NextScript />
<Script
id="facebook"
async
dangerouslySetInnerHTML={{
__html: `window.fbAsyncInit = function() {
FB.init({
appId : '${process.env.NEXT_PUBLIC_FACEBOOK_APP_ID}',
xfbml : true,
version : 'v12.0'
});
FB.AppEvents.logPageView();
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));`,
}}
/>
</body>
</Html>
)
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ declare global {
interface Window {
storyblok
IntersectionObserver: typeof IntersectionObserver
fbAsyncInit: () => void
FB: {
init: (options: any) => void
ui: (options: { display: string; method: string; href: string }) => void
}
}
Expand Down

1 comment on commit 3fa774f

@vercel
Copy link

@vercel vercel bot commented on 3fa774f Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.