Skip to content

Commit

Permalink
feat(cli): update telemetry consent copy
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 authored and bjoerge committed Dec 11, 2023
1 parent 18b2c26 commit b0ea049
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 40 deletions.
76 changes: 50 additions & 26 deletions packages/@sanity/cli/src/actions/telemetry/setTelemetryConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {ClientError, ServerError} from '@sanity/client'
import {type CliCommandAction} from '../../types'
import {debug} from '../../debug'
import {getUserConfig} from '../../util/getUserConfig'
import {TELEMETRY_CONSENT_CONFIG_KEY} from '../../util/createTelemetryStore'
import {TELEMETRY_CONSENT_CONFIG_KEY, resolveConsent} from '../../util/createTelemetryStore'
import {
telemetryLearnMoreMessage,
telemetryStatusMessage,
} from '../../commands/telemetry/telemetryStatusCommand'

type SettableConsentStatus = Extract<ConsentStatus, 'granted' | 'denied'>

Expand All @@ -21,11 +25,13 @@ type Mock = () => Promise<SetConsentResponse<'telemetry'>>
interface ResultMessage {
success: () => string
failure: (message?: string) => string
unchanged: () => string
}

const resultMessages: Record<SettableConsentStatus, ResultMessage> = {
granted: {
success: () => 'Telemetry enabled',
success: () => `You've now enabled telemetry data collection to help us improve Sanity.`,
unchanged: () => `You've already enabled telemetry data collection to help us improve Sanity.`,
failure: (message) => {
if (message) {
return `Failed to enable telemetry: ${message}`
Expand All @@ -34,7 +40,10 @@ const resultMessages: Record<SettableConsentStatus, ResultMessage> = {
},
},
denied: {
success: () => 'Telemetry disabled',
success: () =>
`You've opted out of telemetry data collection.\nNo data will be collected from your Sanity account.`,
unchanged: () =>
`You've already opted out of telemetry data collection.\nNo data is collected from your Sanity account.`,
failure: () => 'Failed to disable telemetry',
},
}
Expand Down Expand Up @@ -100,7 +109,8 @@ function getMock(): Mock | undefined {
}

export function createSetTelemetryConsentAction(status: SettableConsentStatus): CliCommandAction {
return async function setTelemetryConsentAction(_, {apiClient, output, chalk}) {
return async function setTelemetryConsentAction(_, context) {
const {apiClient, output} = context
const config = getUserConfig()

const client = apiClient({
Expand All @@ -113,30 +123,44 @@ export function createSetTelemetryConsentAction(status: SettableConsentStatus):

const mock = getMock()

debug('Setting telemetry consent to "%s"', status)

try {
if (mock) {
debug('Mocking telemetry consent request')
await mock()
} else {
// TODO: Finalise API request.
const uri = `/users/me/consents/telemetry/status/${status}`
debug('Sending telemetry consent status to %s', uri)

await client.request({
method: 'PUT',
uri,
})
}
// eslint-disable-next-line no-process-env
const {status: currentStatus} = await resolveConsent({env: process.env})
const isChanged = currentStatus !== status

// Clear cached telemetry consent
config.delete(TELEMETRY_CONSENT_CONFIG_KEY)
if (!isChanged) {
debug('Telemetry consent is already "%s"', status)
output.print(`${telemetryStatusMessage(status, context)}\n`)
output.print(resultMessages[status].unchanged())
}

output.print(chalk.green(resultMessages[status].success()))
} catch (err) {
err.message = resultMessages[status].failure(err?.responseBody?.message)
throw err
if (isChanged) {
debug('Setting telemetry consent to "%s"', status)
try {
if (mock) {
debug('Mocking telemetry consent request')
await mock()
} else {
// TODO: Finalise API request.
const uri = `/users/me/consents/telemetry/status/${status}`
debug('Sending telemetry consent status to %s', uri)

await client.request({
method: 'PUT',
uri,
})
}

// Clear cached telemetry consent
config.delete(TELEMETRY_CONSENT_CONFIG_KEY)

output.print(`${telemetryStatusMessage(status, context)}\n`)
output.print(resultMessages[status].success())
} catch (err) {
err.message = resultMessages[status].failure(err?.responseBody?.message)
throw err
}
}

output.print(`\n${telemetryLearnMoreMessage(status)}`)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {CliCommandDefinition} from '../../types'
import {ConsentStatus} from '@sanity/telemetry'
import type {CliCommandContext, CliCommandDefinition} from '../../types'
import {resolveConsent} from '../../util/createTelemetryStore'

const helpText = `
Expand All @@ -7,13 +8,38 @@ Examples
sanity telemetry status
`

export function telemetryStatusMessage(status: ConsentStatus, {chalk}: CliCommandContext): string {
switch (status) {
case 'granted':
return `Status: ${chalk.green('Enabled')}`
case 'denied':
return `Status: ${chalk.red('Disabled')}`
case 'unset':
return `Status: ${chalk.yellow('Not set')}`
default:
return ''
}
}

export function telemetryLearnMoreMessage(status: ConsentStatus): string {
const url = 'https://sanity.io/telemetry'

switch (status) {
case 'granted':
return `Learn more about the data being collected here:\n${url}`
default:
return `Learn more here:\n${url}`
}
}

const telemetryStatusCommand: CliCommandDefinition = {
name: 'status',
group: 'telemetry',
helpText,
signature: '',
description: 'Check telemetry consent status for your logged in user',
action: async (_, {chalk, output}) => {
action: async (_, context) => {
const {chalk, output} = context
// eslint-disable-next-line no-process-env
const {status, reason} = await resolveConsent({env: process.env})

Expand All @@ -25,31 +51,37 @@ const telemetryStatusCommand: CliCommandDefinition = {
output.print(chalk.yellow('Could not fetch telemetry consent status.'))
break
case status === 'denied' && reason === 'localOverride':
output.print(`Telemetry consent is ${chalk.red('denied')}.`)
output.print(`Using ${chalk.cyan('DO_NOT_TRACK')} environment variable.`)
output.print(`${telemetryStatusMessage(status, context)}\n`)
output.print(
`You've opted out of telemetry data collection.\nNo data will be collected from your machine.\n`,
)
output.print(`Using ${chalk.cyan('DO_NOT_TRACK')} environment variable.\n`)
break
case status === 'denied':
output.print(`Telemetry consent is ${chalk.red('denied')}.`)
output.print(`${telemetryStatusMessage(status, context)}\n`)
output.print(
`You've opted out of telemetry data collection.\nNo data will be collected from your Sanity account.`,
)
break
case status === 'granted':
output.print(`Telemetry consent is ${chalk.green('granted')}.`)
break
case status === 'unset':
output.print('You have not set telemetry consent.\n')
output.print(`${telemetryStatusMessage(status, context)}\n`)
output.print(
`Run ${chalk.cyan('sanity telemetry enable')} or ${chalk.cyan(
'sanity telemetry disable',
)} to control telemetry collection.`,
'Telemetry data on general usage and errors is collected to help us improve Sanity.',
)
break
case status === 'unset':
output.print(`${telemetryStatusMessage(status, context)}\n`)
output.print(`You've not set your preference for telemetry collection.\n`)
output.print(`Run ${chalk.cyan('npx sanity telemetry enable/disable')} to opt in or out.`)
output.print(
`You can alternatively use the ${chalk.cyan('DO_NOT_TRACK')} environment variable.`,
`You can also use the ${chalk.cyan('DO_NOT_TRACK')} environment variable to opt out.`,
)
break
default:
break
}

output.print('\nLearn more: https://sanity.io/telemetry')
output.print(`\n${telemetryLearnMoreMessage(status)}`)
},
}

Expand Down

0 comments on commit b0ea049

Please sign in to comment.