Files
blitzkit/packages/website/src/components/Embeds/Import.tsx
T

88 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-10-26 14:33:04 -05:00
import {
AlertDialog,
Button,
Flex,
Heading,
Text,
TextField,
Tooltip,
} from '@radix-ui/themes';
import { useRef, useState } from 'react';
2024-11-08 10:38:18 -06:00
import { parse } from 'urlon';
2025-01-15 13:55:24 -06:00
import { useLocale } from '../../hooks/useLocale';
2024-10-26 14:33:04 -05:00
import { EmbedState } from '../../stores/embedState';
export function Import() {
const importInput = useRef<HTMLInputElement>(null);
const [showInvalidURLPrompt, setShowInvalidURLPrompt] = useState(false);
const [showConfirmation, setConfirmation] = useState(false);
const mutateEmbedState = EmbedState.useMutation();
2025-01-15 13:55:24 -06:00
const { strings } = useLocale();
2024-10-26 14:33:04 -05:00
return (
<>
2025-01-15 13:55:24 -06:00
<Heading>
{strings.website.tools.embed.configuration.import.title}
</Heading>
2024-10-26 14:33:04 -05:00
<Text size="2" color="gray" mb="2">
2025-01-15 13:55:24 -06:00
{strings.website.tools.embed.configuration.import.description}
2024-10-26 14:33:04 -05:00
</Text>
<AlertDialog.Root
open={showInvalidURLPrompt}
onOpenChange={setShowInvalidURLPrompt}
>
<AlertDialog.Content>
2025-01-15 13:55:24 -06:00
<AlertDialog.Title>
{strings.website.tools.embed.configuration.import.idiot_title}
</AlertDialog.Title>
2024-10-26 14:33:04 -05:00
<AlertDialog.Description>
2025-01-15 13:55:24 -06:00
{strings.website.tools.embed.configuration.import.idiot_description}
2024-10-26 14:33:04 -05:00
</AlertDialog.Description>
<Flex gap="3" mt="4" justify="end">
<AlertDialog.Cancel>
2025-01-15 13:55:24 -06:00
<Button>
{strings.website.tools.embed.configuration.import.idiot_button}
</Button>
2024-10-26 14:33:04 -05:00
</AlertDialog.Cancel>
</Flex>
</AlertDialog.Content>
</AlertDialog.Root>
<Flex gap="2" mb="6">
<TextField.Root
ref={importInput}
2025-01-15 13:55:24 -06:00
placeholder={strings.website.tools.embed.configuration.import.hint}
2024-10-26 14:33:04 -05:00
style={{ flex: 1 }}
/>
<Tooltip content="Applied!" open={showConfirmation}>
<Button
onClick={() => {
if (!importInput.current) return;
try {
const url = new URL(importInput.current.value);
const state = url.searchParams.get('state');
if (state === null) throw undefined;
2024-11-08 10:38:18 -06:00
const json = parse(state);
2024-10-26 14:33:04 -05:00
mutateEmbedState((draft) => {
Object.assign(draft, json);
});
importInput.current.value = '';
setConfirmation(true);
setTimeout(() => setConfirmation(false), 2000);
} catch {
setShowInvalidURLPrompt(true);
}
}}
>
2025-01-15 13:55:24 -06:00
{strings.website.tools.embed.configuration.import.apply}
2024-10-26 14:33:04 -05:00
</Button>
</Tooltip>
</Flex>
</>
);
}