Skip to content

Commit

Permalink
Merge pull request #348 from Weaverse/paul
Browse files Browse the repository at this point in the history
feat: add weaverse pixel
  • Loading branch information
paul-phan authored Nov 27, 2023
2 parents 17ce72d + 19a413d commit cf7e071
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@weaverse:registry=https://registry.npmjs.com
progress=false
node-linker=hoisted
enable-pre-post-scripts=true
enable-pre-post-scripts=true
39 changes: 19 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@
"preinstall": "npx only-allow pnpm"
},
"devDependencies": {
"@types/node": "^20.9.4",
"@types/react": "^18.2.38",
"@types/node": "20.9.4",
"@types/react": "18.2.38",
"@types/react-dom": "18.2.17",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"cross-env": "^7.0.3",
"@typescript-eslint/eslint-plugin": "6.12.0",
"@typescript-eslint/parser": "6.12.0",
"cross-env": "7.0.3",
"eslint": "8.54.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-react": "^7.33.2",
"husky": "^8.0.3",
"lint-staged": "^15.1.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-prettier": "5.0.1",
"eslint-plugin-react": "7.33.2",
"husky": "8.0.3",
"lint-staged": "15.1.0",
"prettier": "3.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^5.0.5",
"tsup": "^8.0.1",
"turbo": "^1.10.16",
"typescript": "^5.3.2"
"react": "18.2.0",
"react-dom": "18.2.0",
"rimraf": "5.0.5",
"tsup": "8.0.1",
"turbo": "1.10.16",
"typescript": "5.3.2"
},
"optionalDependencies": {
"@esbuild/darwin-x64": "^0.19.7",
"turbo-darwin-arm64": "^1.10.16"
"@esbuild/darwin-x64": "*",
"turbo-darwin-arm64": "*"
},
"engines": {
"node": ">=18"
Expand All @@ -57,8 +57,7 @@
]
},
"lint-staged": {
"./**/*.{js,jsx,ts,tsx,yml,json}": [
"prettier --write --ignore-unknown",
"./**/*.{js,jsx,ts,tsx}": [
"nr lint:fix"
]
}
Expand Down
7 changes: 6 additions & 1 deletion packages/hydrogen/src/WeaverseHydrogenRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {
WeaverseLoaderData,
} from './types'

import { WeaversePixel } from '~/pixel'

type WeaverseData = WeaverseLoaderData | Promise<WeaverseLoaderData>

export let WeaverseHydrogenRoot = memo(

Check warning on line 20 in packages/hydrogen/src/WeaverseHydrogenRoot.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Component definition is missing display name
Expand Down Expand Up @@ -57,7 +59,10 @@ function RenderRoot(props: {
let { data, components } = props
let weaverse = createWeaverseInstance(data, components)
useStudio(weaverse)
return <WeaverseRoot context={weaverse} />
return <>
<WeaverseRoot context={weaverse} />
<WeaversePixel context={weaverse} />
</>
}

export function withWeaverse(Component: React.ComponentType<any>) {
Expand Down
25 changes: 25 additions & 0 deletions packages/hydrogen/src/pixel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// create a pixel Component that will be used to track page views
// /api/public/weaverse-pixel?projectId=xxx&pageId=xxx
// The pixel will be removed from the DOM after it is loaded

import type { FC } from 'react'
import React from 'react'

import type { WeaverseHydrogen } from './index'

export interface PixelProps {
baseUrl: string
projectId: string
pageId: string
}

export const Pixel: FC<PixelProps> = ({ baseUrl, projectId, pageId }) => {
const url = `${baseUrl}/api/public/px?projectId=${projectId}&pageId=${pageId}`
return <img src={url} alt="weaverse pixel" style={{ display: 'none' }} />
}

export const WeaversePixel = ({ context }: { context: WeaverseHydrogen }) => {
let { projectId, pageId, weaverseHost, isDesignMode } = context
if (isDesignMode || !projectId || !pageId || !weaverseHost) return null
return <Pixel baseUrl={weaverseHost} projectId={projectId} pageId={pageId} />
}
4 changes: 2 additions & 2 deletions packages/react/src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ const ItemComponent = memo(({ instance }: ItemComponentProps) => {
rest.ref = instance.ref
}
let renderChildren = (
children.length
children?.length
? children
: childIds.length
: childIds?.length
? childIds.map((cid: string) => ({ id: cid }))
: []
).map((item: { id: string }) => (
Expand Down
40 changes: 20 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf7e071

Please sign in to comment.