From e4c9a257ad1436c7084b584d9b4c67320e98b3bf Mon Sep 17 00:00:00 2001 From: Jaanus Sellin Date: Fri, 29 Dec 2023 13:19:08 +0200 Subject: [PATCH] feat: make local storage work and make feedback url configurable (#5738) Make storage work react way. Make feedback url configurable by env variable. --- .../useUserFeedbackApi/useUserFeedbackApi.ts | 39 ++++++++++--------- frontend/src/hooks/useSubmittedFeedback.ts | 11 ++---- frontend/src/interfaces/uiConfig.ts | 1 + .../__snapshots__/create-config.test.ts.snap | 1 + src/lib/create-config.ts | 3 ++ src/lib/openapi/spec/ui-config-schema.ts | 6 +++ src/lib/routes/admin-api/config.ts | 1 + src/lib/types/option.ts | 1 + 8 files changed, 36 insertions(+), 27 deletions(-) diff --git a/frontend/src/hooks/api/actions/useUserFeedbackApi/useUserFeedbackApi.ts b/frontend/src/hooks/api/actions/useUserFeedbackApi/useUserFeedbackApi.ts index a47d67d8da..af50e7f8bf 100644 --- a/frontend/src/hooks/api/actions/useUserFeedbackApi/useUserFeedbackApi.ts +++ b/frontend/src/hooks/api/actions/useUserFeedbackApi/useUserFeedbackApi.ts @@ -1,40 +1,41 @@ -import { IInternalBanner } from 'interfaces/banner'; import useAPI from '../useApi/useApi'; import { ProvideFeedbackSchema } from '../../../../openapi'; +import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; -const DIRECT_ENDPOINT = 'https://sandbox.getunleash.io/enterprise/feedback'; const ENDPOINT = 'feedback'; export const useUserFeedbackApi = () => { - const addDirectFeedback = async (feedbackSchema: ProvideFeedbackSchema) => { - await fetch(DIRECT_ENDPOINT, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(feedbackSchema), - }); - }; + const { uiConfig } = useUiConfig(); + const { loading, makeRequest, createRequest, errors } = useAPI({ propagateErrors: true, }); const addFeedback = async (feedbackSchema: ProvideFeedbackSchema) => { - const requestId = 'addFeedback'; - const req = createRequest( - ENDPOINT, - { + if (uiConfig.feedbackUriPath !== undefined) { + await fetch(uiConfig.feedbackUriPath, { method: 'POST', + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(feedbackSchema), - }, - requestId, - ); + }); + } else { + const requestId = 'addFeedback'; + const req = createRequest( + ENDPOINT, + { + method: 'POST', + body: JSON.stringify(feedbackSchema), + }, + requestId, + ); - const response = await makeRequest(req.caller, req.id); - return response.json(); + const response = await makeRequest(req.caller, req.id); + return response.json(); + } }; return { addFeedback, - addDirectFeedback, errors, loading, }; diff --git a/frontend/src/hooks/useSubmittedFeedback.ts b/frontend/src/hooks/useSubmittedFeedback.ts index c1a26642bf..cbf48cbe3f 100644 --- a/frontend/src/hooks/useSubmittedFeedback.ts +++ b/frontend/src/hooks/useSubmittedFeedback.ts @@ -1,20 +1,15 @@ import { useEffect, useState } from 'react'; import { getLocalStorageItem, setLocalStorageItem } from '../utils/storage'; import { basePath } from 'utils/formatPath'; +import { createLocalStorage } from '../utils/createLocalStorage'; export type IFeedbackCategory = 'search'; export const useUserSubmittedFeedback = (category: IFeedbackCategory) => { const key = `${basePath}:unleash-userSubmittedFeedback:${category}`; - const [hasSubmittedFeedback, setHasSubmittedFeedback] = useState(() => { - return getLocalStorageItem(key) || false; - }); - - useEffect(() => { - setLocalStorageItem(key, hasSubmittedFeedback); - }, [hasSubmittedFeedback, key]); - + const { value: hasSubmittedFeedback, setValue: setHasSubmittedFeedback } = + createLocalStorage(key, false); return { hasSubmittedFeedback, setHasSubmittedFeedback, diff --git a/frontend/src/interfaces/uiConfig.ts b/frontend/src/interfaces/uiConfig.ts index 2f6d04dad8..3aea2ba5c0 100644 --- a/frontend/src/interfaces/uiConfig.ts +++ b/frontend/src/interfaces/uiConfig.ts @@ -4,6 +4,7 @@ import { Variant } from 'utils/variants'; export interface IUiConfig { authenticationType?: string; baseUriPath?: string; + feedbackUriPath?: string; /** * @deprecated `useUiFlags` can be used instead * @example diff --git a/src/lib/__snapshots__/create-config.test.ts.snap b/src/lib/__snapshots__/create-config.test.ts.snap index f1d80a111e..bc8b09248f 100644 --- a/src/lib/__snapshots__/create-config.test.ts.snap +++ b/src/lib/__snapshots__/create-config.test.ts.snap @@ -69,6 +69,7 @@ exports[`should create default config 1`] = ` "_maxListeners": undefined, Symbol(kCapture): false, }, + "feedbackUriPath": undefined, "flagResolver": FlagResolver { "experiments": { "anonymiseEventLog": false, diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index 4509bf99a9..1c3fd35e0a 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -549,6 +549,8 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { const rateLimiting = loadRateLimitingConfig(options); + const feedbackUriPath = process.env.FEEDBACK_URI_PATH; + return { db, session, @@ -584,6 +586,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { isEnterprise: isEnterprise, metricsRateLimiting, rateLimiting, + feedbackUriPath, }; } diff --git a/src/lib/openapi/spec/ui-config-schema.ts b/src/lib/openapi/spec/ui-config-schema.ts index bb715fefb4..371c8694de 100644 --- a/src/lib/openapi/spec/ui-config-schema.ts +++ b/src/lib/openapi/spec/ui-config-schema.ts @@ -43,6 +43,12 @@ export const uiConfigSchema = { 'The base URI path at which this Unleash instance is listening.', example: '/enterprise', }, + feedbackUriPath: { + type: 'string', + description: + 'The URI path at which the feedback endpoint is listening.', + example: '/feedback', + }, disablePasswordAuth: { type: 'boolean', description: diff --git a/src/lib/routes/admin-api/config.ts b/src/lib/routes/admin-api/config.ts index 9c4aca8c8d..5270da77b6 100644 --- a/src/lib/routes/admin-api/config.ts +++ b/src/lib/routes/admin-api/config.ts @@ -142,6 +142,7 @@ class ConfigController extends Controller { networkViewEnabled: this.config.prometheusApi !== undefined, disablePasswordAuth, maintenanceMode, + feedbackUriPath: this.config.feedbackUriPath, }; this.openApiService.respondWithValidation( diff --git a/src/lib/types/option.ts b/src/lib/types/option.ts index 9d353b2f74..f43dbe4e5c 100644 --- a/src/lib/types/option.ts +++ b/src/lib/types/option.ts @@ -243,4 +243,5 @@ export interface IUnleashConfig { disableScheduler?: boolean; isEnterprise: boolean; rateLimiting: IRateLimiting; + feedbackUriPath?: string; }