Skip to content

Commit ca9c3a9

Browse files
committed
refactor(react): add server runtime components
1 parent 5629dcc commit ca9c3a9

3 files changed

Lines changed: 39 additions & 64 deletions

File tree

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
11
'use server';
22

3-
import { ReplexicaChunkGetterArgs, ReplexicaChunkProps } from "../types";
4-
import { cookies } from 'next/headers';
3+
import { ReplexicaBaseChunk, ReplexicaBaseChunkProps } from "../shared";
4+
import { ReplexicaServerProps } from "./types";
55

6-
export type ReplexicaServerChunkProps = ReplexicaChunkProps & {
7-
strategy?: 'cookie';
8-
importer: (locale: string) => ReplexicaChunkGetterArgs['data'];
9-
};
6+
export type ReplexicaServerChunkProps =
7+
& Omit<ReplexicaBaseChunkProps, 'data'>
8+
& ReplexicaServerProps;
109

1110
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-
});
11+
const locale = await props.loadLocale();
12+
const data = await props.loadLocaleData(locale);
2413

25-
return result;
14+
return (
15+
<ReplexicaBaseChunk
16+
data={data}
17+
fileId={props.fileId}
18+
scopeId={props.scopeId}
19+
chunkId={props.chunkId}
20+
/>
21+
);
2622
}
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-
}
Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
11
'use server';
22

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-
}
3+
import { ReplexicaBaseProxy, ReplexicaBaseProxyProps } from "../shared";
4+
import { ReplexicaServerProps } from "./types";
5+
6+
export type ReplexicaServerProxyProps<P extends {}> =
7+
& Omit<ReplexicaBaseProxyProps<P>, 'data'>
8+
& ReplexicaServerProps;
9+
10+
export async function ReplexicaServerProxy<P extends {}>(props: ReplexicaServerProxyProps<P>) {
11+
const { loadLocale, loadLocaleData, ...baseProps } = props;
12+
13+
const locale = await loadLocale();
14+
const localeData = await loadLocaleData(locale);
15+
16+
return (
17+
<ReplexicaBaseProxy<P>
18+
data={localeData as any}
19+
{...baseProps as any}
20+
/>
21+
);
22+
}

packages/react/src/server/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type ReplexicaServerProps = {
2+
loadLocale: () => Promise<string>;
3+
loadLocaleData: (locale: string) => Promise<any>;
4+
};

0 commit comments

Comments
 (0)