1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-22 01:16:07 +02:00

docs: integrate unleash to show feedback

This commit is contained in:
Thomas Heartman 2022-03-01 17:21:14 +01:00
parent 8f1750a538
commit d0132ef61d
3 changed files with 71 additions and 18 deletions

View File

@ -10,6 +10,13 @@ module.exports = {
organizationName: 'Unleash', // Usually your GitHub org/user name. organizationName: 'Unleash', // Usually your GitHub org/user name.
projectName: 'unleash.github.io', // Usually your repo name. projectName: 'unleash.github.io', // Usually your repo name.
trailingSlash: false, trailingSlash: false,
customFields: {
// expose env vars etc here
unleashProxyUrl: process.env.UNLEASH_PROXY_URL,
unleashProxyClientKey: process.env.UNLEASH_PROXY_CLIENT_KEY,
unleashFeedbackTargetUrl: process.env.UNLEASH_FEEDBACK_TARGET_URL,
environment: process.env.NODE_ENV,
},
themeConfig: { themeConfig: {
defaultMode: 'light', defaultMode: 'light',
disableSwitch: true, disableSwitch: true,
@ -159,7 +166,7 @@ module.exports = {
{ {
from: '/advanced/impression_data', from: '/advanced/impression_data',
to: '/advanced/impression-data', to: '/advanced/impression-data',
} },
], ],
createRedirects: function (toPath) { createRedirects: function (toPath) {
if ( if (

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import styles from './styles.module.css'; import styles from './styles.module.css';
import CloseIcon from '@site/src/icons/close'; import CloseIcon from '@site/src/icons/close';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
const join = (...cs) => cs.join(' '); const join = (...cs) => cs.join(' ');
@ -107,6 +108,14 @@ const stateReducer = (state, message) => {
}; };
export const FeedbackWrapper = ({ seedData, open }) => { export const FeedbackWrapper = ({ seedData, open }) => {
const {
siteConfig: { customFields },
} = useDocusaurusContext();
const feedbackTargetUrl =
customFields?.unleashFeedbackTargetUrl ??
(typeof process !== 'undefined' &&
process?.env?.UNLEASH_FEEDBACK_TARGET_URL);
const [feedbackIsOpen, setFeedbackIsOpen] = React.useState(open); const [feedbackIsOpen, setFeedbackIsOpen] = React.useState(open);
const [manuallyOpened, setManuallyOpened] = React.useState(open); const [manuallyOpened, setManuallyOpened] = React.useState(open);
@ -134,11 +143,15 @@ export const FeedbackWrapper = ({ seedData, open }) => {
dispatch({ kind: 'set customer type', data: customerType }); dispatch({ kind: 'set customer type', data: customerType });
const submitFeedback = () => { const submitFeedback = () => {
fetch(process.env.UNLEASH_FEEDBACK_TARGET_URL, { if (feedbackTargetUrl) {
fetch(feedbackTargetUrl, {
method: 'post', method: 'post',
body: JSON.stringify({ data: state.data }), body: JSON.stringify({ data: state.data }),
headers: {
'content-type': 'application/then',
},
}) })
.then(async (res) => .json(async (res) =>
res.ok res.ok
? console.log('Success! Feedback was registered.') ? console.log('Success! Feedback was registered.')
: console.warn( : console.warn(
@ -146,8 +159,16 @@ export const FeedbackWrapper = ({ seedData, open }) => {
), ),
) )
.catch((e) => .catch((e) =>
console.error('Oh, no! The feedback registration failed:', e), console.error(
'Oh, no! The feedback registration failed:',
e,
),
); );
} else {
console.warn(
'No target url specified for feedback. Not doing anything.',
);
}
dispatch({ kind: 'completed' }); dispatch({ kind: 'completed' });
stepForward(); stepForward();
}; };
@ -399,8 +420,6 @@ export const FeedbackWrapper = ({ seedData, open }) => {
return ( return (
<div className={styles['user-feedback-container']}> <div className={styles['user-feedback-container']}>
<p>State.data is {JSON.stringify(state.data)}</p>
<button <button
aria-hidden={feedbackIsOpen} aria-hidden={feedbackIsOpen}
className={join( className={join(
@ -451,3 +470,5 @@ export const FeedbackWrapper = ({ seedData, open }) => {
</div> </div>
); );
}; };
export default FeedbackWrapper;

View File

@ -1,12 +1,37 @@
import React from 'react'; import React from 'react';
import UF from '@site/src/components/UserFeedback'; import UserFeedback from '@site/src/components/UserFeedback';
import { UnleashClient } from 'unleash-proxy-client';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
// Default implementation, that you can customize // Default implementation, that you can customize
function Root({ children }) { function Root({ children }) {
const {
siteConfig: { customFields },
} = useDocusaurusContext();
const unleashConfig = {
clientKey: customFields.unleashProxyClientKey,
url: customFields.unleashProxyUrl,
refreshInterval: 1,
appName: `docs.getunleash.io-${customFields.environment}`,
};
const [showFeedback, setShowFeedback] = React.useState(false);
try {
const unleash = new UnleashClient(unleashConfig);
unleash.on('ready', () => {
setShowFeedback(unleash.isEnabled('docs-feedback-survey-v1'));
});
unleash.start();
} catch (e) {
console.warn('Unable to initialize the Unleash client:', e.message);
}
return ( return (
<> <>
<UF />
{children} {children}
{showFeedback && <UserFeedback />}
</> </>
); );
} }