Skip to content

Commit e5a259a

Browse files
committed
feat(react): add runtime client components
1 parent 2f8fee3 commit e5a259a

8 files changed

Lines changed: 118 additions & 7 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use client';
2+
3+
import { useContext } from "react";
4+
import { replexicaContext } from "./context";
5+
import { ReplexicaChunkGetterArgs, ReplexicaChunkProps } from "../types";
6+
7+
export function ReplexicaClientChunk(props: ReplexicaChunkProps) {
8+
const r = useContext(replexicaContext)!;
9+
10+
const result = getReplexicaChunkContent({
11+
data: r.data,
12+
selector: props,
13+
});
14+
15+
return result;
16+
}
17+
18+
export function getReplexicaChunkContent(args: ReplexicaChunkGetterArgs) {
19+
const fileData = args.data?.[args.selector.fileId];
20+
const scopeData = fileData?.[args.selector.scopeId];
21+
const chunkData = scopeData?.[args.selector.chunkId];
22+
23+
const fallback = `chunk#${args.selector.fileId}:${args.selector.scopeId}.${args.selector.chunkId}`;
24+
25+
const result = chunkData || fallback;
26+
27+
return result;
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use client';
2+
3+
import { createContext } from "react";
4+
5+
export type ReplexicaIntlContext = {
6+
data: any;
7+
};
8+
9+
export const replexicaContext = createContext<ReplexicaIntlContext | null>(null);

packages/react/src/client/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './provider';
2+
export * from './chunk';
3+
export * from './proxy';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use client';
2+
3+
import { useMemo } from "react";
4+
import { replexicaContext } from './context';
5+
6+
export type ReplexicaIntlProviderProps = {
7+
data: any;
8+
children?: React.ReactNode;
9+
};
10+
11+
export function ReplexicaIntlProvider(props: ReplexicaIntlProviderProps) {
12+
const value = useMemo(getReplexicaValue, [props.data]);
13+
return (
14+
<replexicaContext.Provider value={value}>
15+
{props.children}
16+
</replexicaContext.Provider>
17+
);
18+
19+
function getReplexicaValue() {
20+
return {
21+
data: props.data,
22+
};
23+
}
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use client';
2+
3+
import { createElement, useContext } from "react";
4+
import { ReplexicaChunkSelector, ReplexicaProxyProps } from "../types";
5+
import { replexicaContext } from "./context";
6+
import { getReplexicaChunkContent } from "./chunk";
7+
8+
export function ReplexicaClientProxy<P extends {}>(props: ReplexicaProxyProps<P>) {
9+
// const r = useContext(replexicaContext)!;
10+
// const data = r.data;
11+
12+
// const propsPatch: Partial<P> = Object
13+
// .entries(props.attributes || {})
14+
// .reduce((acc, [key, value]) => {
15+
// const result = getReplexicaChunkContent({
16+
// data: data,
17+
// selector: value as ReplexicaChunkSelector,
18+
// });
19+
20+
// return {
21+
// ...acc,
22+
// [key]: result,
23+
// };
24+
// }, {});
25+
26+
// const modifiedProps: P = {
27+
// ...props.targetProps,
28+
// ...propsPatch,
29+
// };
30+
31+
// return createElement(
32+
// props.target,
33+
// modifiedProps,
34+
// );
35+
return null;
36+
}

packages/react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 42;

packages/react/src/index.tsx

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/react/src/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export type ReplexicaChunkSelector = {
2+
fileId: string;
3+
scopeId: string;
4+
chunkId: string;
5+
};
6+
7+
export type ReplexicaChunkProps = ReplexicaChunkSelector;
8+
9+
export type ReplexicaChunkGetterArgs = {
10+
data: any;
11+
selector: ReplexicaChunkSelector;
12+
};
13+
14+
export type ReplexicaProxyProps<P extends {}> = P & {
15+
__ReplexixcaComponent: string | React.ComponentType<P>;
16+
__ReplexicaAttributes: Record<keyof P, ReplexicaChunkSelector>;
17+
};

0 commit comments

Comments
 (0)