From a1335dfc628b23c7d34b9d0bc67833f8f73a0b4a Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 14 Feb 2023 17:45:49 +0100 Subject: [PATCH 1/2] feat: system service --- src/lib/actions/analytics.ts | 18 ++++++++---------- src/lib/components/feedbackNPS.svelte | 1 - src/lib/constants.ts | 7 ------- src/lib/stores/app.ts | 6 +++--- src/lib/stores/sdk.ts | 4 ++-- src/lib/system.ts | 21 +++++++++++++++++++++ 6 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 src/lib/system.ts diff --git a/src/lib/actions/analytics.ts b/src/lib/actions/analytics.ts index cad336312..357b5ff55 100644 --- a/src/lib/actions/analytics.ts +++ b/src/lib/actions/analytics.ts @@ -3,16 +3,14 @@ import googleAnalytics from '@analytics/google-analytics'; import { get } from 'svelte/store'; import { page } from '$app/stores'; import { user } from '$lib/stores/user'; -import { growthEndpoint, Mode } from '$lib/constants'; import { AppwriteException } from '@aw-labs/appwrite-console'; +import { ENV, MODE, VARS } from '$lib/system'; -const isDevelopment = - import.meta.env.DEV || import.meta.env?.VITE_VERCEL_ENV?.toString() === 'preview'; const analytics = Analytics({ app: 'appwrite', plugins: [ googleAnalytics({ - measurementIds: [import.meta.env.VITE_GA_PROJECT?.toString() || 'G-R4YJ9JN8L4'] + measurementIds: [VARS.GOOGLE_ANALYTICS ?? 'G-R4YJ9JN8L4'] }) ] }); @@ -32,7 +30,7 @@ export function trackEvent(name: string, data: object = null): void { }; } - if (isDevelopment) { + if (ENV.DEV || ENV.PREVIEW) { console.debug(`[Analytics] Event ${name} ${path}`, data); } else { analytics.track(name, { ...data, path }); @@ -54,7 +52,7 @@ export function trackPageView(path: string) { return; } - if (isDevelopment) { + if (ENV.DEV || ENV.PREVIEW) { console.debug(`[Analytics] Pageview ${path}`); } else { analytics.page({ @@ -64,14 +62,14 @@ export function trackPageView(path: string) { } function sendEventToGrowth(event: string, path: string, data: object = null): void { - if (!growthEndpoint) return; + if (!VARS.GROWTH_ENDPOINT) return; const userStore = get(user); let email: string, name: string; if (userStore) { email = userStore.email; name = userStore.name; } - fetch(`${growthEndpoint}/analytics`, { + fetch(`${VARS.GROWTH_ENDPOINT}/analytics`, { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -80,7 +78,7 @@ function sendEventToGrowth(event: string, path: string, data: object = null): vo action: event, label: event, url: window.location.origin + path, - account: import.meta.env.VITE_CONSOLE_MODE?.toString() || Mode.SELF_HOSTED, + account: MODE, data: { email, name, @@ -91,7 +89,7 @@ function sendEventToGrowth(event: string, path: string, data: object = null): vo } function isTrackingAllowed() { - if (import.meta.env?.VITEST) { + if (ENV.TEST) { return; } if (window.navigator?.doNotTrack) { diff --git a/src/lib/components/feedbackNPS.svelte b/src/lib/components/feedbackNPS.svelte index b53bf8e27..19f42e254 100644 --- a/src/lib/components/feedbackNPS.svelte +++ b/src/lib/components/feedbackNPS.svelte @@ -20,7 +20,6 @@ async function handleSubmit() { try { await feedback.submitFeedback('feedback-nps', message, name, email, value); - console.log(value, message); feedback.switchType('general'); } catch (error) { feedback.switchType('general'); diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 871340d7a..0a19c9208 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -2,13 +2,6 @@ export const PAGE_LIMIT = 12; // default page limit export const CARD_LIMIT = 6; // default card limit export const INTERVAL = 5 * 60000; // default interval to check for feedback -export enum Mode { - CLOUD = 'cloud', - SELF_HOSTED = 'self-hosted' -} - -export const growthEndpoint = import.meta.env.VITE_APPWRITE_GROWTH_ENDPOINT; - export enum Dependencies { ORGANIZATION = 'dependency:organization', PROJECT = 'dependency:project', diff --git a/src/lib/stores/app.ts b/src/lib/stores/app.ts index 38290db24..4ddd2e08a 100644 --- a/src/lib/stores/app.ts +++ b/src/lib/stores/app.ts @@ -1,5 +1,5 @@ import { browser } from '$app/environment'; -import { growthEndpoint } from '$lib/constants'; +import { VARS } from '$lib/system'; import { writable } from 'svelte/store'; export type AppStore = { @@ -60,8 +60,8 @@ function createFeedbackStore() { email?: string, value?: number ) => { - if (!growthEndpoint) return; - const response = await fetch(`${growthEndpoint}/feedback`, { + if (!VARS.GROWTH_ENDPOINT) return; + const response = await fetch(`${VARS.GROWTH_ENDPOINT}/feedback`, { method: 'POST', headers: { 'Content-Type': 'application/json' diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index 7167b5319..ec05508fc 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -1,3 +1,4 @@ +import { VARS } from '$lib/system'; import { Account, Avatars, @@ -12,8 +13,7 @@ import { Users } from '@aw-labs/appwrite-console'; -const endpoint = - import.meta.env.VITE_APPWRITE_ENDPOINT?.toString() ?? `${globalThis?.location?.origin}/v1`; +const endpoint = VARS.APPWRITE_ENDPOINT ?? `${globalThis?.location?.origin}/v1`; const clientConsole = new Client(); clientConsole.setEndpoint(endpoint).setProject('console'); diff --git a/src/lib/system.ts b/src/lib/system.ts new file mode 100644 index 000000000..9360f8dea --- /dev/null +++ b/src/lib/system.ts @@ -0,0 +1,21 @@ +export enum Mode { + CLOUD = 'cloud', + SELF_HOSTED = 'self-hosted' +} + +export const VARS = { + APPWRITE_ENDPOINT: import.meta.env?.VITE_APPWRITE_ENDPOINT?.toString() as string | null, + GROWTH_ENDPOINT: import.meta.env?.VITE_APPWRITE_GROWTH_ENDPOINT?.toString() as string | null, + CONSOLE_MODE: import.meta.env?.VITE_CONSOLE_MODE?.toString() as string | null, + VERCEL_ENV: import.meta.env?.VITE_VERCEL_ENV?.toString() as string | null, + GOOGLE_ANALYTICS: import.meta.env?.VITE_GA_PROJECT?.toString() as string | null +}; + +export const ENV = { + DEV: import.meta.env.DEV, + PROD: import.meta.env.PROD, + PREVIEW: VARS.VERCEL_ENV === 'preview', + TEST: !!import.meta.env?.VITEST +}; + +export const MODE = VARS.CONSOLE_MODE === Mode.CLOUD ? Mode.CLOUD : Mode.SELF_HOSTED; From 0950c860365fc6daa2e2ac0860a07bf47d7644de Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 14 Feb 2023 17:55:30 +0100 Subject: [PATCH 2/2] fix: convert nullish to undefined --- src/lib/system.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/system.ts b/src/lib/system.ts index 9360f8dea..fc00cbd8e 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -4,11 +4,13 @@ export enum Mode { } export const VARS = { - APPWRITE_ENDPOINT: import.meta.env?.VITE_APPWRITE_ENDPOINT?.toString() as string | null, - GROWTH_ENDPOINT: import.meta.env?.VITE_APPWRITE_GROWTH_ENDPOINT?.toString() as string | null, - CONSOLE_MODE: import.meta.env?.VITE_CONSOLE_MODE?.toString() as string | null, - VERCEL_ENV: import.meta.env?.VITE_VERCEL_ENV?.toString() as string | null, - GOOGLE_ANALYTICS: import.meta.env?.VITE_GA_PROJECT?.toString() as string | null + APPWRITE_ENDPOINT: import.meta.env?.VITE_APPWRITE_ENDPOINT?.toString() as string | undefined, + GROWTH_ENDPOINT: import.meta.env?.VITE_APPWRITE_GROWTH_ENDPOINT?.toString() as + | string + | undefined, + CONSOLE_MODE: import.meta.env?.VITE_CONSOLE_MODE?.toString() as string | undefined, + VERCEL_ENV: import.meta.env?.VITE_VERCEL_ENV?.toString() as string | undefined, + GOOGLE_ANALYTICS: import.meta.env?.VITE_GA_PROJECT?.toString() as string | undefined }; export const ENV = {