Skip to content

Commit

Permalink
Removed SettingsContext. Added hostUrl prop.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Jan 29, 2024
1 parent fec8169 commit 1a70350
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 48 deletions.
6 changes: 0 additions & 6 deletions src/app/(main)/settings/SettingsContext.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions src/app/(main)/settings/users/UserAddButton.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Button, Icon, Text, Modal, Icons, ModalTrigger, useToasts } from 'react-basics';
import UserAddForm from './UserAddForm';
import { useMessages } from 'components/hooks';
import { setValue } from 'store/cache';
import { touch } from 'store/cache';

export function UserAddButton({ onSave }: { onSave?: () => void }) {
const { formatMessage, labels, messages } = useMessages();
const { showToast } = useToasts();

const handleSave = () => {
showToast({ message: formatMessage(messages.saved), variant: 'success' });
setValue('users', Date.now());
touch('users');
onSave?.();
};

Expand Down
16 changes: 11 additions & 5 deletions src/app/(main)/settings/websites/[id]/ShareUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import {
LoadingButton,
useToasts,
} from 'react-basics';
import { useContext, useState } from 'react';
import { useState } from 'react';
import { getRandomChars } from 'next-basics';
import { useApi, useMessages } from 'components/hooks';
import SettingsContext from 'app/(main)/settings/SettingsContext';

const generateId = () => getRandomChars(16);

export function ShareUrl({ website, onSave }: { website: Website; onSave?: () => void }) {
export function ShareUrl({
website,
hostUrl,
onSave,
}: {
website: Website;
hostUrl?: string;
onSave?: () => void;
}) {
const { domain, shareId } = website;
const { hostUrl } = useContext(SettingsContext);
const { formatMessage, labels, messages } = useMessages();
const [id, setId] = useState(shareId);
const { showToast } = useToasts();
Expand All @@ -28,7 +34,7 @@ export function ShareUrl({ website, onSave }: { website: Website; onSave?: () =>
mutationFn: (data: any) => post(`/websites/${website.id}`, data),
});

const url = `${hostUrl || location.origin}${
const url = `${hostUrl || process.env.hostUrl || window?.location.origin}${
process.env.basePath
}/share/${id}/${encodeURIComponent(domain)}`;

Expand Down
11 changes: 5 additions & 6 deletions src/app/(main)/settings/websites/[id]/TrackingCode.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { TextArea } from 'react-basics';
import { useMessages, useConfig } from 'components/hooks';
import { useContext } from 'react';
import SettingsContext from '../../SettingsContext';

export function TrackingCode({ websiteId }: { websiteId: string }) {
export function TrackingCode({ websiteId, hostUrl }: { websiteId: string; hostUrl?: string }) {
const { formatMessage, messages } = useMessages();
const config = useConfig();
const { trackingCodeUrl } = useContext(SettingsContext);

const trackerScriptName =
config?.trackerScriptName?.split(',')?.map(n => n.trim())?.[0] || 'script.js';
config?.trackerScriptName?.split(',')?.map((n: string) => n.trim())?.[0] || 'script.js';

const url = trackerScriptName?.startsWith('http')
? trackerScriptName
: `${trackingCodeUrl}${process.env.basePath}/${trackerScriptName}`;
: `${hostUrl || process.env.hostUrl || window?.location.origin}${
process.env.basePath
}/${trackerScriptName}`;

const code = `<script defer src="${url}" data-website-id="${websiteId}"></script>`;

Expand Down
32 changes: 5 additions & 27 deletions src/app/Providers.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use client';
import { useEffect, useState } from 'react';
import { IntlProvider } from 'react-intl';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactBasicsProvider } from 'react-basics';
import ErrorBoundary from 'components/common/ErrorBoundary';
import SettingsContext from 'app/(main)/settings/SettingsContext';
import { useLocale } from 'components/hooks';
import 'chartjs-adapter-date-fns';

Expand All @@ -26,34 +24,14 @@ function MessagesProvider({ children }) {
);
}

function SettingsProvider({ children }) {
const [config, setConfig] = useState({});

useEffect(() => {
const hostUrl = process.env.hostUrl || window?.location.origin;

setConfig({
shareUrl: hostUrl,
trackingCodeUrl: hostUrl,
websitesUrl: '/websites',
settingsPath: '/settings/websites',
websitesPath: `/websites`,
});
}, []);

return <SettingsContext.Provider value={config}>{children}</SettingsContext.Provider>;
}

export function Providers({ children }) {
return (
<MessagesProvider>
<SettingsProvider>
<QueryClientProvider client={client}>
<ReactBasicsProvider>
<ErrorBoundary>{children}</ErrorBoundary>
</ReactBasicsProvider>
</QueryClientProvider>
</SettingsProvider>
<QueryClientProvider client={client}>
<ReactBasicsProvider>
<ErrorBoundary>{children}</ErrorBoundary>
</ReactBasicsProvider>
</QueryClientProvider>
</MessagesProvider>
);
}
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export * from 'app/(main)/settings/websites/WebsiteSettings';
export * from 'app/(main)/settings/websites/WebsitesDataTable';
export * from 'app/(main)/settings/websites/WebsitesTable';

export * from 'app/(main)/settings/SettingsContext';

export * from 'components/common/TypeConfirmationForm';
export * from 'components/common/DataTable';
export * from 'components/common/Empty';
Expand Down

0 comments on commit 1a70350

Please sign in to comment.