-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a965afc
commit 2e70f8e
Showing
6 changed files
with
97 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { useIsAdmin } from 'src/rtk/features/moderation/hooks' | ||
import { useModerationContext } from './ModerationProvider' | ||
|
||
export default function ModerateButton() { | ||
export default function ModerateButton({ postId }: { postId: string }) { | ||
const isAdmin = useIsAdmin() | ||
const { openModal } = useModerationContext() | ||
if (!isAdmin) return null | ||
|
||
return <div>Moderate</div> | ||
return <div onClick={() => openModal(postId)}>Moderate</div> | ||
} |
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,71 @@ | ||
import React, { ReactNode, useEffect, useMemo, useRef, useState } from 'react' | ||
import { isDevMode } from 'src/config/env' | ||
import { parseGrillMessage } from 'src/utils/iframe' | ||
import { getCurrentUrlOrigin } from 'src/utils/url' | ||
|
||
type State = { | ||
openModal: (postId: string) => void | ||
} | ||
|
||
const Context = React.createContext<State>({ openModal: () => undefined }) | ||
|
||
export default function ModerationProvider({ children }: { children: ReactNode }) { | ||
const [isOpenModal, setIsOpenModal] = useState(false) | ||
const iframeRef = useRef<HTMLIFrameElement | null>(null) | ||
|
||
useEffect(() => { | ||
window.onmessage = event => { | ||
const message = parseGrillMessage(event.data + '') | ||
if (!message) return | ||
|
||
const { name, value } = message | ||
if (name === 'moderation' && value === 'close') { | ||
setIsOpenModal(false) | ||
} | ||
} | ||
}, []) | ||
|
||
const value = useMemo(() => { | ||
return { | ||
openModal: (postId: string) => { | ||
if (isDevMode) return | ||
iframeRef.current?.contentWindow?.postMessage( | ||
{ | ||
type: 'grill:moderate', | ||
payload: postId, | ||
}, | ||
'*', | ||
) | ||
setIsOpenModal(true) | ||
}, | ||
} | ||
}, []) | ||
|
||
return ( | ||
<Context.Provider value={value}> | ||
{!isDevMode && ( | ||
<iframe | ||
ref={iframeRef} | ||
src={`${getCurrentUrlOrigin()}/c/widget/moderation`} | ||
style={{ | ||
opacity: isOpenModal ? 1 : 0, | ||
pointerEvents: isOpenModal ? 'auto' : 'none', | ||
border: 'none', | ||
transition: 'opacity 0.3s ease-in-out', | ||
colorScheme: 'none', | ||
background: 'transparent', | ||
position: 'fixed', | ||
inset: 0, | ||
width: '100%', | ||
height: '100%', | ||
}} | ||
/> | ||
)} | ||
{children} | ||
</Context.Provider> | ||
) | ||
} | ||
|
||
export function useModerationContext() { | ||
return React.useContext(Context) | ||
} |
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
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,10 @@ | ||
export function parseGrillMessage(data: string) { | ||
const match = data.match(/^([^:]+):([^:]+):(.+)$/) | ||
if (!match) return null | ||
|
||
const origin = match[1] | ||
const name = match[2] | ||
const value = match[3] | ||
if (origin !== 'grill') return null | ||
return { name: name ?? '', value: value ?? '' } | ||
} |