Files
blitzkit/packages/preforged/tools/embed/[embed]/host/_index.tsx
T

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-10-27 22:38:14 -05:00
import { Box } from '@radix-ui/themes';
2024-10-27 22:29:28 -05:00
import { useMemo } from 'react';
import { parse } from 'urlon';
2024-12-27 12:38:38 -06:00
import { BlitzKitTheme } from '../../../../../../components/BlitzKitTheme';
2024-10-27 22:29:28 -05:00
import {
embedConfigurations,
embedRenderers,
extractEmbedConfigDefaults,
2024-12-27 12:38:38 -06:00
} from '../../../../../../constants/embeds';
2024-10-27 22:29:28 -05:00
import {
EmbedState,
type EmbedConfig,
type ExtractEmbedConfigTypes,
2024-12-27 12:38:38 -06:00
} from '../../../../../../stores/embedState';
import { useEmbedStateCurry } from '../../../../../../stores/embedState/utilities';
2024-10-27 22:29:28 -05:00
interface PageProps {
embed: keyof typeof embedConfigurations;
}
export function Page({ embed }: PageProps) {
const state = useMemo(() => {
const params = new URLSearchParams(window.location.search);
const stateRaw = params.get('state');
const partialState = stateRaw === null ? {} : parse(stateRaw);
const state = {
...extractEmbedConfigDefaults(embedConfigurations[embed]),
...partialState,
} as ExtractEmbedConfigTypes<EmbedConfig>;
return state;
}, [embed]);
return (
<EmbedState.Provider data={state}>
2024-10-27 22:38:14 -05:00
<Content embed={embed} />
2024-10-27 22:29:28 -05:00
</EmbedState.Provider>
);
}
2024-10-27 22:38:14 -05:00
function Content({ embed }: PageProps) {
const Renderer = embedRenderers[embed];
2024-10-28 10:30:53 -05:00
const { useEmbedState } = useEmbedStateCurry();
const width = useEmbedState('width');
const height = useEmbedState('height');
2024-10-27 22:38:14 -05:00
return (
<BlitzKitTheme style={{ background: 'transparent' }}>
<Box width={`${width}px`} height={`${height}px`} overflow="hidden">
<Renderer />
</Box>
</BlitzKitTheme>
);
}