2024-05-13 13:41:41 +00:00
|
|
|
|
import { ComponentPropsWithoutRef } from "react";
|
2024-04-24 15:56:11 +00:00
|
|
|
|
import { Root, createRoot } from "react-dom/client";
|
2024-05-04 16:18:56 +00:00
|
|
|
|
import { pollForSelector } from "../shared/pollForSelector";
|
2024-05-13 13:41:41 +00:00
|
|
|
|
import QuizContainer from "./QuizContainer";
|
2024-04-24 15:56:11 +00:00
|
|
|
|
|
|
|
|
|
|
2024-05-13 13:41:41 +00:00
|
|
|
|
type Props = ComponentPropsWithoutRef<typeof QuizContainer>;
|
|
|
|
|
|
2024-04-24 15:56:11 +00:00
|
|
|
|
export class ContainerWidget {
|
|
|
|
|
root: Root | undefined;
|
|
|
|
|
|
2024-05-13 13:41:41 +00:00
|
|
|
|
constructor(props: Props & {
|
2024-04-24 15:56:11 +00:00
|
|
|
|
selector: string;
|
2024-04-28 13:25:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* In seconds, null - polling disabled
|
|
|
|
|
*/
|
|
|
|
|
selectorPollingTimeLimit?: number | null;
|
2024-04-24 15:56:11 +00:00
|
|
|
|
}) {
|
2024-05-13 13:41:41 +00:00
|
|
|
|
const { selector, selectorPollingTimeLimit = 60 } = props;
|
|
|
|
|
|
2024-04-25 14:06:42 +00:00
|
|
|
|
const element = document.querySelector(selector);
|
2024-04-28 13:25:59 +00:00
|
|
|
|
if (element) {
|
|
|
|
|
this.root = createRoot(element);
|
2024-05-13 13:41:41 +00:00
|
|
|
|
this.render(props);
|
2024-04-24 15:56:11 +00:00
|
|
|
|
|
2024-04-28 13:25:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-24 15:56:11 +00:00
|
|
|
|
|
2024-04-28 13:25:59 +00:00
|
|
|
|
if (!selectorPollingTimeLimit) {
|
|
|
|
|
console.error(`Не удалось найти элемент ${selector} для вставки виджета`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pollForSelector(selector, selectorPollingTimeLimit, (element) => {
|
|
|
|
|
this.root = createRoot(element);
|
2024-05-13 13:41:41 +00:00
|
|
|
|
this.render(props);
|
2024-04-28 13:25:59 +00:00
|
|
|
|
});
|
2024-04-24 15:56:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 13:41:41 +00:00
|
|
|
|
render(props: Props) {
|
|
|
|
|
this.root?.render(<QuizContainer {...props} />);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 15:56:11 +00:00
|
|
|
|
destroy() {
|
|
|
|
|
if (this.root) this.root.unmount();
|
|
|
|
|
}
|
|
|
|
|
}
|