Skip to content

Commit 5a7d6bc

Browse files
committed
feat(react): add runtime server components
1 parent 08a1ebd commit 5a7d6bc

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use server';
2+
3+
import { ReplexicaChunkGetterArgs, ReplexicaChunkProps } from "../types";
4+
import { cookies } from 'next/headers';
5+
6+
export type ReplexicaServerChunkProps = ReplexicaChunkProps & {
7+
strategy?: 'cookie';
8+
importer: (locale: string) => ReplexicaChunkGetterArgs['data'];
9+
};
10+
11+
export async function ReplexicaServerChunk(props: ReplexicaServerChunkProps) {
12+
const locale = await getCurrentLocale();
13+
console.log(`[ReplexicaServerChunk] locale: ${locale}`);
14+
const data = await props.importer(locale);
15+
console.log(`[ReplexicaServerChunk] data: ${JSON.stringify(data)}`);
16+
const result = await getReplexicaChunkContent({
17+
data: data,
18+
selector: {
19+
fileId: props.fileId,
20+
scopeId: props.scopeId,
21+
chunkId: props.chunkId,
22+
},
23+
});
24+
25+
return result;
26+
}
27+
28+
export async function getReplexicaChunkContent(args: ReplexicaChunkGetterArgs) {
29+
const { fileId, scopeId, chunkId } = args.selector;
30+
const text = args.data?.[fileId]?.[scopeId]?.[chunkId];
31+
const fallback = `chunk1#${fileId}:${scopeId}.${chunkId}`;
32+
return text || fallback;
33+
}
34+
35+
export async function getCurrentLocale() {
36+
const cookieMgr = cookies();
37+
return cookieMgr.get('locale')?.value || 'en';
38+
}

packages/react/src/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './chunk';
2+
export * from './proxy';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use server';
2+
3+
import { createElement } from "react";
4+
import { ReplexicaProxyProps, ReplexicaChunkSelector } from "../types";
5+
import { getReplexicaChunkContent } from './chunk';
6+
7+
export async function ReplexicaServerProxy<P extends {}>(props: ReplexicaProxyProps<P>) {
8+
// const data = {};
9+
10+
// let propsPatch: Partial<P> = {};
11+
12+
// for (const [key, value] of Object.entries(props.attributes || {})) {
13+
// const result = await getReplexicaChunkContent({
14+
// data,
15+
// selector: value as ReplexicaChunkSelector,
16+
// });
17+
18+
// propsPatch = {
19+
// ...propsPatch,
20+
// [key]: result,
21+
// };
22+
// }
23+
24+
// const modifiedProps: P = {
25+
// ...props.targetProps,
26+
// ...propsPatch,
27+
// };
28+
29+
// return createElement(
30+
// props.target,
31+
// modifiedProps,
32+
// );
33+
34+
return null;
35+
}

0 commit comments

Comments
 (0)