diff --git a/packages/nc-gui-v2/composables/useGlobalState.ts b/packages/nc-gui-v2/composables/useGlobalState/index.ts similarity index 95% rename from packages/nc-gui-v2/composables/useGlobalState.ts rename to packages/nc-gui-v2/composables/useGlobalState/index.ts index f3392b4236..268299eb99 100644 --- a/packages/nc-gui-v2/composables/useGlobalState.ts +++ b/packages/nc-gui-v2/composables/useGlobalState/index.ts @@ -1,6 +1,7 @@ import { breakpointsTailwind, usePreferredLanguages, useStorage } from '@vueuse/core' import { useJwt } from '@vueuse/integrations/useJwt' import type { JwtPayload } from 'jwt-decode' +import initialFeedBackForm from './initialFeedBackForm' import { computed, ref, toRefs, useBreakpoints, useNuxtApp, useTimestamp, watch } from '#imports' import type { Actions, Getters, GlobalState, StoredState, User } from '~/lib/types' @@ -67,7 +68,13 @@ export const useGlobalState = (): GlobalState => { }, 'en' /** fallback locale */) /** State */ - const initialState: StoredState = { token: null, user: null, lang: preferredLanguage, darkMode: prefersDarkMode } + const initialState: StoredState = { + token: null, + user: null, + lang: preferredLanguage, + darkMode: prefersDarkMode, + feedbackForm: initialFeedBackForm, + } /** saves a reactive state, any change to these values will write/delete to localStorage */ const storage = $(useStorage(storageKey, initialState)) diff --git a/packages/nc-gui-v2/composables/useGlobalState/initialFeedBackForm.ts b/packages/nc-gui-v2/composables/useGlobalState/initialFeedBackForm.ts new file mode 100644 index 0000000000..e351e88a9c --- /dev/null +++ b/packages/nc-gui-v2/composables/useGlobalState/initialFeedBackForm.ts @@ -0,0 +1,5 @@ +export default { + url: 'https://docs.google.com/forms/d/e/1FAIpQLSeTlAfZjszgr53lArz3NvUEnJGOT9JtG9NAU5d0oQwunDS2Pw/viewform?embedded=true', + createdAt: new Date('2020-01-01T00:00:00.000Z').toISOString(), + isHidden: false, +} diff --git a/packages/nc-gui-v2/lib/types.ts b/packages/nc-gui-v2/lib/types.ts index a702fbd9f7..0691490864 100644 --- a/packages/nc-gui-v2/lib/types.ts +++ b/packages/nc-gui-v2/lib/types.ts @@ -9,11 +9,19 @@ export interface User { roles: Roles } +export interface FeedbackForm { + url: string + createdAt: string + isHidden: boolean + lastFormPollDate?: string +} + export interface StoredState { token: string | null user: User | null lang: string darkMode: boolean + feedbackForm: FeedbackForm } export interface State extends StoredState { diff --git a/packages/nc-gui-v2/plugins/api.ts b/packages/nc-gui-v2/plugins/api.ts index be280baadd..57ef2e368f 100644 --- a/packages/nc-gui-v2/plugins/api.ts +++ b/packages/nc-gui-v2/plugins/api.ts @@ -21,7 +21,7 @@ function addAxiosInterceptors(api: Api, app: { $state: GlobalState }) { api.instance.interceptors.request.use((config) => { config.headers['xc-gui'] = 'true' - if (app.$state.token.value) config.headers['xc-auth'] = app.$state.token.value + if (app.$state?.token.value) config.headers['xc-auth'] = app.$state.token.value if (!config.url?.endsWith('/user/me') && !config.url?.endsWith('/admin/roles')) { // config.headers['xc-preview'] = store.state.users.previewAs diff --git a/packages/nc-gui-v2/plugins/init.ts b/packages/nc-gui-v2/plugins/init.ts new file mode 100644 index 0000000000..c1d90253c6 --- /dev/null +++ b/packages/nc-gui-v2/plugins/init.ts @@ -0,0 +1,46 @@ +import type { Dayjs } from 'dayjs' +import dayjs from 'dayjs' +import duration from 'dayjs/plugin/duration' +import { defineNuxtPlugin } from '#app' + +dayjs.extend(duration) + +const handleFeedbackForm = async () => { + let { feedbackForm: currentFeedbackForm } = $(useGlobalState()) + if (!currentFeedbackForm) return + + const { $api } = useNuxtApp() + + const fetchFeedbackForm = async (now: Dayjs) => { + try { + const { data: feedbackForm } = await $api.instance.get('/api/v1/feedback_form') + const isFetchedFormDuplicate = currentFeedbackForm.url === feedbackForm.url + + currentFeedbackForm = { + url: feedbackForm.url, + lastFormPollDate: now.toISOString(), + createdAt: feedbackForm.created_at, + isHidden: isFetchedFormDuplicate ? currentFeedbackForm.isHidden : false, + } + } catch (e) { + console.error(e) + } + } + + const isFirstTimePolling = !currentFeedbackForm.lastFormPollDate + + const now = dayjs() + const lastFormPolledDate = dayjs(currentFeedbackForm.lastFormPollDate) + + if (isFirstTimePolling || dayjs.duration(now.diff(lastFormPolledDate)).days() > 0) { + await fetchFeedbackForm(now) + } +} + +const handleInitFunctions = async () => { + await handleFeedbackForm() +} + +export default defineNuxtPlugin((nuxtApp) => { + nuxtApp.vueApp.use(handleInitFunctions) +})