2024-09-22 22:04:03 -05:00
|
|
|
import type { TextProps } from '@radix-ui/themes';
|
2024-10-05 16:44:07 -05:00
|
|
|
import { EmbedState, type EmbedConfig, type ExtractEmbedConfigTypes } from '.';
|
2024-09-22 22:04:03 -05:00
|
|
|
import type { EmbedConfigItemType, EmbedItemType } from './constants';
|
2024-08-08 14:18:22 -05:00
|
|
|
|
|
|
|
|
export function useEmbedStateCurry<Config extends EmbedConfig>() {
|
|
|
|
|
function useState<Key extends keyof Config>(key: Key) {
|
|
|
|
|
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) {
|
|
|
|
|
const { color, ...state } = useState(
|
|
|
|
|
key,
|
|
|
|
|
) as EmbedConfigItemType<EmbedItemType.RichText>['default'];
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
style: { color },
|
|
|
|
|
...state,
|
|
|
|
|
} satisfies TextProps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { useState, useRichText };
|
|
|
|
|
}
|