AlertSiteUrlUndefined.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import type { JSX } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { useSiteUrl } from '~/stores-universal/context';
  4. const isValidUrl = (str: string): boolean => {
  5. try {
  6. // eslint-disable-next-line no-new
  7. new URL(str);
  8. return true;
  9. }
  10. catch {
  11. return false;
  12. }
  13. };
  14. export const AlertSiteUrlUndefined = (): JSX.Element => {
  15. const { t } = useTranslation('commons');
  16. const { data: siteUrl, error: errorSiteUrl } = useSiteUrl();
  17. const isLoadingSiteUrl = siteUrl === undefined && errorSiteUrl === undefined;
  18. if (isLoadingSiteUrl) {
  19. return <></>;
  20. }
  21. if (typeof siteUrl === 'string' && isValidUrl(siteUrl)) {
  22. return <></>;
  23. }
  24. return (
  25. <div className="alert alert-danger rounded-0 d-edit-none mb-0 px-4 py-2">
  26. <span className="material-symbols-outlined">error</span>
  27. {
  28. t('alert.siteUrl_is_not_set', { link: t('headers.app_settings') })
  29. } &gt;&gt; <a href="/admin/app">{t('headers.app_settings')}<span className="material-symbols-outlined">login</span></a>
  30. </div>
  31. );
  32. };
  33. AlertSiteUrlUndefined.displayName = 'AlertSiteUrlUndefined';