Ghost/apps/comments-ui/src/index.js
Fabien "egg" O'Carroll ea531e8c19 Added initial support for the Admin API
refs https://github.com/TryGhost/Team/issues/1664

This allows us to have access to the currently logged in admin user as well as
future access to show and hide comments via the Admin API.
2022-07-05 15:33:55 +02:00

69 lines
2.1 KiB
JavaScript

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
const ROOT_DIV_ID = 'ghost-comments-root';
function addRootDiv() {
const scriptTag = document.querySelector('script[data-ghost-comments]');
// We need to inject the comment box at the same place as the script tag
if (scriptTag) {
const elem = document.createElement('div');
elem.id = ROOT_DIV_ID;
scriptTag.parentElement.insertBefore(elem, scriptTag);
} else if (process.env.NODE_ENV === 'development') {
const elem = document.createElement('div');
elem.id = ROOT_DIV_ID;
document.body.appendChild(elem);
} else {
// eslint-disable-next-line no-console
console.warn('Comment box location was not found: could not load comments box.');
}
}
function getSiteData() {
/**
* @type {HTMLElement}
*/
const scriptTag = document.querySelector('script[data-ghost-comments]');
if (scriptTag) {
const siteUrl = scriptTag.dataset.ghostComments;
const apiKey = scriptTag.dataset.key;
const apiUrl = scriptTag.dataset.api;
const adminUrl = scriptTag.dataset.admin;
const sentryDsn = scriptTag.dataset.sentryDsn;
const postId = scriptTag.dataset.postId;
return {siteUrl, apiKey, apiUrl, sentryDsn, postId, adminUrl};
}
return {};
}
function handleTokenUrl() {
const url = new URL(window.location.href);
if (url.searchParams.get('token')) {
url.searchParams.delete('token');
window.history.replaceState({}, document.title, url.href);
}
}
function setup({siteUrl}) {
addRootDiv();
handleTokenUrl();
}
function init() {
// const customSiteUrl = getSiteUrl();
const {siteUrl: customSiteUrl, sentryDsn, postId, adminUrl} = getSiteData();
const siteUrl = customSiteUrl || window.location.origin;
setup({siteUrl});
ReactDOM.render(
<React.StrictMode>
{<App adminUrl={adminUrl} siteUrl={siteUrl} customSiteUrl={customSiteUrl} sentryDsn={sentryDsn} postId={postId} />}
</React.StrictMode>,
document.getElementById(ROOT_DIV_ID)
);
}
init();