|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useRef, useState } from 'react'; |
| 4 | + |
| 5 | +import { useSearchParams } from 'next/navigation'; |
| 6 | + |
| 7 | +import { BlockViewer } from '@/components/block-viewer'; |
| 8 | +import { siteConfig } from '@/config/site'; |
| 9 | + |
| 10 | +const i18n = { |
| 11 | + cn: { |
| 12 | + buildYourRichTextEditor: '构建你的富文本编辑器', |
| 13 | + description: '框架 · 插件 · 组件 · 主题', |
| 14 | + getStarted: '开始使用', |
| 15 | + github: 'GitHub', |
| 16 | + potionDescription: '一个类似 Notion 的 AI 模板。', |
| 17 | + }, |
| 18 | + en: { |
| 19 | + buildYourRichTextEditor: 'Build your rich-text editor', |
| 20 | + description: 'Framework · Plugins · Components · Themes', |
| 21 | + getStarted: 'Get Started', |
| 22 | + github: 'GitHub', |
| 23 | + potionDescription: 'A Notion-like AI template.', |
| 24 | + }, |
| 25 | +}; |
| 26 | + |
| 27 | +export function PotionLazyBlock() { |
| 28 | + const searchParams = useSearchParams(); |
| 29 | + |
| 30 | + const [locale, setLocale] = useState<keyof typeof i18n>('en'); |
| 31 | + const ref = useRef<HTMLDivElement>(null); |
| 32 | + const [shouldRender, setShouldRender] = useState(false); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const getLocale = async () => { |
| 36 | + const resolvedLocale = (searchParams?.get('locale') || |
| 37 | + 'en') as keyof typeof i18n; |
| 38 | + setLocale(resolvedLocale); |
| 39 | + }; |
| 40 | + |
| 41 | + void getLocale(); |
| 42 | + }, [searchParams]); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + const handleScroll = () => { |
| 46 | + // scroll home page down over 100px and render the block |
| 47 | + if (window.scrollY > 100 && !shouldRender) { |
| 48 | + setShouldRender(true); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + window.addEventListener('scroll', handleScroll); |
| 53 | + |
| 54 | + // Check initial scroll position |
| 55 | + handleScroll(); |
| 56 | + |
| 57 | + return () => { |
| 58 | + window.removeEventListener('scroll', handleScroll); |
| 59 | + }; |
| 60 | + }, [shouldRender]); |
| 61 | + |
| 62 | + const content = i18n[locale]; |
| 63 | + |
| 64 | + const block = { |
| 65 | + description: content.potionDescription, |
| 66 | + descriptionSrc: siteConfig.links.potionTemplate, |
| 67 | + isPro: true, |
| 68 | + meta: { |
| 69 | + iframeHeight: 800, |
| 70 | + }, |
| 71 | + name: 'potion', |
| 72 | + src: siteConfig.links.potionIframe, |
| 73 | + }; |
| 74 | + |
| 75 | + return ( |
| 76 | + <div ref={ref}> |
| 77 | + {shouldRender && ( |
| 78 | + <BlockViewer |
| 79 | + dependencies={[]} |
| 80 | + highlightedFiles={[]} |
| 81 | + isPro={block.isPro} |
| 82 | + item={{ |
| 83 | + description: block.description, |
| 84 | + meta: block.meta, |
| 85 | + name: block.name, |
| 86 | + src: block.src, |
| 87 | + type: 'registry:block', |
| 88 | + }} |
| 89 | + tree={[]} |
| 90 | + /> |
| 91 | + )} |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
0 commit comments