Files
blitzkit/packages/website/src/stores/embedState/utilities.ts
T

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-08-05 13:13:48 -05:00
import type { TextProps } from "@radix-ui/themes";
import { EmbedState, type EmbedConfig, type ExtractEmbedConfigTypes } from ".";
import {
breakdownConfig,
extractEmbedConfigDefaults,
} from "../../constants/embeds";
import type { EmbedConfigItemType, EmbedItemType } from "./constants";
2024-08-08 14:18:22 -05:00
export function useEmbedStateCurry<Config extends EmbedConfig>() {
2026-08-05 13:13:48 -05:00
// TODO: find a way to initialize with the correct state preset
EmbedState.useInitialization(extractEmbedConfigDefaults(breakdownConfig));
2024-10-28 10:30:53 -05:00
function useEmbedState<Key extends keyof Config>(key: Key) {
2024-08-08 14:18:22 -05:00
return EmbedState.use((state) => {
2024-10-05 16:44:07 -05:00
return (state as ExtractEmbedConfigTypes<Config>)[key];
2024-08-08 14:18:22 -05:00
});
}
function useRichText<
Key extends {
[K in keyof Config]: Config[K] extends { type: EmbedItemType.RichText }
? K
: never;
}[keyof Config],
>(key: Key) {
2024-10-28 10:30:53 -05:00
const { color, ...state } = useEmbedState(
2024-08-08 14:18:22 -05:00
key,
2026-08-05 13:13:48 -05:00
) as EmbedConfigItemType<EmbedItemType.RichText>["default"];
2024-08-08 14:18:22 -05:00
return {
style: { color },
...state,
} satisfies TextProps;
}
2024-10-28 10:30:53 -05:00
return { useEmbedState, useRichText };
2024-08-08 14:18:22 -05:00
}