Skip to content

Commit

Permalink
add basic support for 'none' telemetry option
Browse files Browse the repository at this point in the history
changing the option in the Select dropdown breaks things right now, gotta figure out why
  • Loading branch information
iLynxcat committed Sep 28, 2024
1 parent 3a7ee2b commit 4483496
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
30 changes: 21 additions & 9 deletions interface/app/$libraryId/settings/client/privacy.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
import { telemetryState, useTelemetryState } from '@sd/client';
import { Switch } from '@sd/ui';
import { TELEMETRY_LEVEL_PREFERENCES, telemetryState, useTelemetryState } from '@sd/client';
import { Select, SelectOption } from '@sd/ui';
import { useLocale } from '~/hooks';

import { Heading } from '../Layout';
import Setting from '../Setting';

export const Component = () => {
const fullTelemetry = useTelemetryState().shareFullTelemetry;

const { t } = useLocale();

const { telemetryLevelPreference } = useTelemetryState();

return (
<>
<Heading title={t('privacy')} description="" />

<Setting
mini
toolTipLabel={t('learn_more_about_telemetry')}
infoUrl="https://www.spacedrive.com/docs/product/resources/privacy"
title={t('telemetry_title')}
description={t('telemetry_description')}
>
<Switch
checked={fullTelemetry}
onClick={() => (telemetryState.shareFullTelemetry = !fullTelemetry)}
size="md"
/>
<Select
value={telemetryLevelPreference}
onChange={(newValue) => {
console.log('UPDATE UIPDATE update' + newValue);
// add "dateFormat" key to localStorage and set it as default date format
telemetryState.telemetryLevelPreference = newValue;
console.log('UPDATE UIPDATE update finalize ' + newValue);
}}
containerClassName="flex h-[30px] gap-2"
>
{TELEMETRY_LEVEL_PREFERENCES.map((format, index) => (
<SelectOption key={index} value={format}>
{format}
</SelectOption>
))}
</Select>
</Setting>
</>
);
Expand Down
26 changes: 17 additions & 9 deletions packages/client/src/hooks/usePlausible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import { PlausiblePlatformType, telemetryState, useTelemetryState } from '../sto
const DOMAIN = 'app.spacedrive.com';
const MOBILE_DOMAIN = 'mobile.spacedrive.com';

const PlausibleProvider = Plausible({
trackLocalhost: true,
domain: DOMAIN
});
let plausibleInstance: ReturnType<typeof Plausible>;

/**
* This defines all possible options that may be provided by events upon submission.
Expand Down Expand Up @@ -186,14 +183,22 @@ interface SubmitEventProps {
*/
const submitPlausibleEvent = async ({ event, debugState, ...props }: SubmitEventProps) => {
if (props.platformType === 'unknown') return;
// if (debugState.enabled && debugState.shareFullTelemetry !== true) return;
if (
'plausibleOptions' in event && 'telemetryOverride' in event.plausibleOptions
? event.plausibleOptions.telemetryOverride !== true
? // if telemetry override is on, always send. we never use this and probably never should.
// this should be discussed soon (if I don't forget™) and removed if we agree. ~ilynxcat
event.plausibleOptions.telemetryOverride !== true
: props.shareFullTelemetry !== true && event.type !== 'ping'
)
return;

// using a singleton this way instead of instantiating at file eval (first time it's imported)
// because a user having "none" teleemtry preference should mean plausible's code never ever runs
plausibleInstance ??= Plausible({
trackLocalhost: true,
domain: DOMAIN
});

const fullEvent: PlausibleTrackerEvent = {
eventName: event.type,
props: {
Expand All @@ -217,7 +222,7 @@ const submitPlausibleEvent = async ({ event, debugState, ...props }: SubmitEvent
: undefined
};

PlausibleProvider.trackEvent(
plausibleInstance.trackEvent(
fullEvent.eventName,
{
props: fullEvent.props,
Expand Down Expand Up @@ -271,9 +276,12 @@ interface EventSubmissionCallbackProps {
* });
* ```
*/
export const usePlausibleEvent = () => {
const debugState = useDebugState();
export const usePlausibleEvent = (): ((props: EventSubmissionCallbackProps) => Promise<void>) => {
const telemetryState = useTelemetryState();

if (telemetryState.telemetryLevelPreference === 'none') return async (...args: any[]) => {};

const debugState = useDebugState();
const previousEvent = useRef({} as BasePlausibleEvent<string>);

return useCallback(
Expand Down
11 changes: 9 additions & 2 deletions packages/client/src/stores/telemetryState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ import { createPersistedMutable, useSolidStore } from '../solid';
export type PlausiblePlatformType = 'web' | 'mobile' | 'desktop' | 'unknown';

type TelemetryState = {
shareFullTelemetry: boolean;
telemetryLevelPreference: TelemetryLevelPreference;
platform: PlausiblePlatformType;
buildInfo: BuildInfo | undefined;
};

export const TELEMETRY_LEVEL_PREFERENCES = [
'full',
'minimal',
'none'
] satisfies TelemetryLevelPreference[];
export type TelemetryLevelPreference = 'full' | 'minimal' | 'none';

export const telemetryState = createPersistedMutable(
'sd-explorer-layout',
createMutable<TelemetryState>({
shareFullTelemetry: false, // false by default
telemetryLevelPreference: 'none', // no telemetry by default
platform: 'unknown',
buildInfo: undefined
})
Expand Down

0 comments on commit 4483496

Please sign in to comment.