Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add preferences for analytics (#843)
Dhruv Bhanushali47343a89c386
diff --git a/.gitignore b/.gitignoreindex 6b7ecaff1..4d7fc01a6 100644--- a/.gitignore+++ b/.gitignore@@ -25,6 +25,9 @@ node_modules/npm-debug.log*.pnpm-debug.log+# Nuxt+.nuxt+# Local certificates*.pem*.crtdiff --git a/documentation/reference/frontend/feature_flags.md b/documentation/reference/frontend/feature_flags.mdnew file mode 100644index 000000000..9da58d876--- /dev/null+++ b/documentation/reference/frontend/feature_flags.md@@ -0,0 +1,119 @@+# Feature flags++Feature flags control how the app works in different environments and for+different users.++## Concepts++### Status++The status of a feature can be one of three values:++- `enabled`+- `disabled`+- `switchable`++This is configurable and must be set in the feature flags configuration file,+`~/feat/feature-flags.json`.++A `switchable` flag in a public environment like 'staging' or 'production'+effectively becomes a user-level preferences toggle.++### State++The state of a feature can be one of two values:++- `on`+- `off`++This is determined by the [feature status](#status) and user-preferences.++- An `enabled` feature is always `on`.+- A `disabled` feature is always `off`.+- A switchable feature can be `on` or `off` based on the user's preference. This+ `preferredState` is recorded in a cookie. If no preference has been set, the+ [`defaultState`](#defaultstate) will be used.++### Cascade++The current environment can be 'local', 'development', 'staging' or 'production'+based on the `DEPLOYMENT_ENV` environment variable (assumed 'local' by default).++If a feature status is set for an environment, it is assumed to be same value+for all environments before it and `disabled` for all environments after it+(unless specified otherwise). For example, consider the following configuration:++```json+{+ "local": "enabled",+ "staging": "switchable"+}+```++Here the feature will be++- `enabled` for 'local' environment (configured)+- `switchable` for 'development' environment (cascaded from staging)+- `switchable` for 'staging' environment (configured)+- `disabled` for 'production' environment (default)++## File structure++The feature flags configuration file, `~/feat/feature-flags.json` contains two+top-level keys, `features` and `groups`.++### `features`++This is a mapping of feature names to feature configurations.++#### Key: title++This is an identifier for the feature. This identifier is looked up in the i18n+strings to generate a human-readable purpose (`pref-page.features.${title}`) for+the feature.++Conventionally, `_` is used as the separator for the name so that the name is a+valid JS identifier.++#### Value: configuration++This controls the behavior of the feature flag. It is an object containing three+fields, `status`, `description` and `defaultState`.++##### `status`++It is a mapping of different environments with the status of the flag in that+environment. For example, a feature that's in development may be 'switchable' in+'staging' but 'disabled' in 'production'.++##### `defaultState`++This is the state of a switchable feature when the user has not switched it. For+example, a feature that's in development may start as 'opt-in', being 'off' by+default and may gradually change to opt-out, becoming 'on' by default.++##### `description`++This is a description of the feature that appears in the internal `/preferences`+page.++⚠️ This field is not internationalised because it's for internal use only. You+should [populate `en.json5`](#key-title) if the feature is switchable and+visible to the user.++### `groups`++This setting pertains to how feature flags can serve as user-level preferences.+It is an array of objects, each containing two fields, `title` and `features`.++#### `title`++This is an identifier for the group. This identifier is looked up in the i18n+strings to generate a human-readable name (`pref-page.groups.${title}.title`)+and description (`pref-page.groups.${title}.desc`) for the group.++#### `features`++This is a list of strings where each string must be a+[key from the upper `features` section](#key-title). Each switchable feature+from this list will show up in the preferences modal.diff --git a/documentation/reference/frontend/index.md b/documentation/reference/frontend/index.mdnew file mode 100644index 000000000..498a27ee8--- /dev/null+++ b/documentation/reference/frontend/index.md@@ -0,0 +1,7 @@+# Frontend++```{toctree}+:maxdepth: 1++feature_flags+```diff --git a/documentation/reference/index.md b/documentation/reference/index.mdindex 888abea08..d480d8714 100644--- a/documentation/reference/index.md+++ b/documentation/reference/index.md@@ -6,5 +6,6 @@github_contribution_practicesdev_flowapi/index+frontend/indexsearch_algorithm```diff --git a/frontend/feat/feature-flags.json b/frontend/feat/feature-flags.jsonindex 6b0ef7b74..015dcd6b0 100644--- a/frontend/feat/feature-flags.json+++ b/frontend/feat/feature-flags.json@@ -8,25 +8,18 @@"description": "Toggle the external sources in the header's search type switcher.","defaultState": "off"},- "new_header": {+ "analytics": {"status": {- "staging": "enabled",- "production": "enabled"+ "staging": "switchable",+ "production": "disabled"},- "description": "Toggle the new header, including recent searches."- },- "feat_disabled": {- "status": "disabled",- "description": "Will always be disabled"- },- "feat_enabled": {- "status": "enabled",- "description": "Will always be enabled"- },- "feat_switchable": {- "status": "switchable",- "description": "Can be switched between on and off",- "defaultState": "on"+ "description": "Record custom events and page views."+ }+ },+ "groups": [+ {+ "title": "analytics",+ "features": ["analytics"]}- }+ ]}diff --git a/frontend/src/components/VCheckbox/VCheckbox.vue b/frontend/src/components/VCheckbox/VCheckbox.vueindex 11fe5cdb1..2a472e728 100644--- a/frontend/src/components/VCheckbox/VCheckbox.vue+++ b/frontend/src/components/VCheckbox/VCheckbox.vue@@ -4,24 +4,55 @@class="relative flex text-sm leading-5":class="labelClasses">- <!--- The checkbox focus style is a slight variation on the `focus-slim-tx` style.- Because it becomes filled when checked, it also needs the- `checked:focus-visible:border-white` class.- -->- <input- :id="id"- type="checkbox"- class="checkbox h-5 w-5 flex-shrink-0 appearance-none rounded-sm border border-dark-charcoal bg-white me-3 focus-slim-tx checked:bg-dark-charcoal checked:focus-visible:border-white disabled:border-dark-charcoal-40 disabled:bg-dark-charcoal-10 checked:disabled:border-dark-charcoal-40 checked:disabled:bg-dark-charcoal-40"- v-bind="inputAttrs"- @change="onChange"- />- <VIcon- v-show="localCheckedState"- class="absolute transform text-white"- :icon-path="checkIcon"- :size="5"- />+ <div class="relative">+ <!--+ The checkbox focus style is a slight variation on the `focus-slim-tx` style.+ Because it becomes filled when checked, it also needs the+ `checked:focus-visible:border-white` class.+ -->+ <input+ :id="id"+ type="checkbox"+ class="block appearance-none border border-dark-charcoal bg-white transition-colors duration-100 me-3 checked:bg-dark-charcoal disabled:border-dark-charcoal-40 disabled:bg-dark-charcoal-10 checked:disabled:border-dark-charcoal-40 checked:disabled:bg-dark-charcoal-40"+ :class="+ isSwitch+ ? ['h-4.5', 'w-9', 'rounded-full', 'focus-slim-offset']+ : [+ 'h-5',+ 'w-5',+ 'rounded-sm',+ 'focus-slim-tx',+ 'checked:focus-visible:border-white',+ ]+ "+ v-bind="inputAttrs"+ @click="onChange"+ />++ <!-- Knob, for when `ifSwitch` is `true` -->+ <div+ v-if="isSwitch"+ class="absolute top-0.75 left-0.75 h-3 w-3 rounded-full transition-transform duration-100"+ :class="+ localCheckedState+ ? ['bg-white', 'translate-x-[1.125rem]']+ : disabled+ ? ['bg-dark-charcoal-40']+ : ['bg-dark-charcoal']+ "+ aria-hidden="true"+ />++ <!-- Checkmark, for when `ifSwitch` is `false` -->+ <VIcon+ v-else+ v-show="localCheckedState"+ class="absolute inset-0 transform text-white"+ :icon-path="checkIcon"+ :size="5"+ />+ </div>+<!-- @slot The checkbox label --><slot /></label></template>@@ -103,6 +134,13 @@ export default defineComponent({type: Boolean,default: false,},+ /**+ * whether to make the checkbox appear like a switch+ */+ isSwitch: {+ type: Boolean,+ default: false,+ },},emits: {change: defineEvent<[Omit<CheckboxAttrs, "disabled">]>(),diff --git a/frontend/src/components/VCheckbox/meta/VCheckbox.stories.mdx b/frontend/src/components/VCheckbox/meta/VCheckbox.stories.mdxindex d9b7216c6..dadb37c35 100644--- a/frontend/src/components/VCheckbox/meta/VCheckbox.stories.mdx+++ b/frontend/src/components/VCheckbox/meta/VCheckbox.stories.mdx@@ -42,6 +42,8 @@ export const Template = (args) => ({<Description of={VCheckbox} />+<ArgsTable of={VCheckbox} />+## Default checkbox<Canvas>@@ -58,7 +60,25 @@ export const Template = (args) => ({</Story></Canvas>-<ArgsTable of={VCheckbox} />+## Switch++The checkbox can alternatively be rendered as a switch, when the choice is+between an on/off decision.++<Canvas>+ <Story+ name="Switch"+ args={{+ id: "default",+ name: "storybook",+ value: "codeIsPoetry",+ isSwitch: true,+ }}+ argTypes={checkboxArgTypes}+ >+ {Template.bind({})}+ </Story>+</Canvas>export const LicenseCheckboxTemplate = (args) => ({template: `<fieldset>diff --git a/frontend/src/locales/scripts/en.json5 b/frontend/src/locales/scripts/en.json5index b258d2782..108975ae9 100644--- a/frontend/src/locales/scripts/en.json5+++ b/frontend/src/locales/scripts/en.json5@@ -680,6 +680,15 @@"skip-to-content": "Skip to content","pref-page": {title: "Preferences",+ groups: {+ analytics: {+ title: "Analytics",+ desc: "Participate in anonymous analytics to improve user experience.",+ },+ },+ features: {+ analytics: "Record custom events and page views.",+ },"non-switchable": {title: "Non-switchable features",desc: "You cannot modify the status of these features.",diff --git a/frontend/src/pages/preferences.vue b/frontend/src/pages/preferences.vueindex 2b2ee2305..4ee79d7fc 100644--- a/frontend/src/pages/preferences.vue+++ b/frontend/src/pages/preferences.vue@@ -2,25 +2,65 @@<VContentPage><h1>{{ $t("pref-page.title") }}</h1>- <div v-for="isSwitchable in [false, true]" :key="isSwitchable">- <h2>+ <!-- TODO: Extract this to preferences modal. -->+ <!--+ This area only lists switchable flags because if the flag is not switchable,+ it doesn't count as a user preference.+ -->+ <div+ v-for="(group, groupIndex) in featureData.groups"+ :key="groupIndex"+ class="not-prose border-b border-dark-charcoal-20 py-6 last-of-type:border-b-0"+ >+ <h2 class="label-bold mb-2">+ {{ $t(`pref-page.groups.${group.title}.title`) }}+ </h2>+ <p class="label-regular mb-4">+ {{ $t(`pref-page.groups.${group.title}.desc`) }}+ </p>+ <ul>+ <li+ v-for="(name, featureIndex) in group.features"+ :key="featureIndex"+ class="mb-4 last:mb-0"+ >+ <VCheckbox+ v-if="getFlagStatus(featureData.features[name]) === SWITCHABLE"+ :id="name"+ class="flex-row items-center"+ :checked="featureState(name) === ON"+ is-switch+ @change="handleChange"+ >{{ $t(`pref-page.features.${name}`) }}</VCheckbox+ >+ </li>+ </ul>+ </div>++ <div+ v-for="isSwitchable in [false, true]"+ :key="isSwitchable"+ class="not-prose border-b border-dark-charcoal-20 py-6 last-of-type:border-b-0"+ >+ <h2 class="label-bold mb-2">{{ $t(`pref-page.${isSwitchable ? "" : "non-"}switchable.title`) }}</h2>- <p>+ <p class="label-regular mb-4">{{ $t(`pref-page.${isSwitchable ? "" : "non-"}switchable.desc`) }}</p>- <ul class="!ps-0">+ <ul><template v-for="(feature, name) in flags"><liv-if="(getFlagStatus(feature) === SWITCHABLE) === isSwitchable":key="name"- class="flex flex-row items-center"+ class="mb-4 flex flex-row items-center last:mb-0"><VCheckbox:id="name"class="flex-row items-center":checked="featureState(name) === ON":disabled="!isSwitchable"+ is-switch@change="handleChange"><div>@@ -36,39 +76,6 @@<h2>{{ $t("pref-page.store-state") }}</h2><pre><code>{{ flags }}</code></pre>-- <h2>{{ $t("pref-page.content-filtering") }}</h2>- <ul>- <template v-for="(_, featName) in flags">- <template v-for="featState in FEATURE_STATES">- <i18n- v-if="featureState(featName) === featState"- :key="`${featName}-${featState}`"- path="pref-page.explanation"- tag="li"- >- <template #feat-name- ><code>{{ featName }}</code></template- >- <template #feat-state- ><code>{{ featState }}</code></template- >- </i18n>- </template>- </template>- <!--<i18n- v-if="featureState('feat_nonexistent') === ON"- path="pref-page.explanation"- tag="li"- >- <template #feat-name- ><code>{{ $t('flag-status.nonexistent') }}</code></template- >- <template #feat-state- ><code>{{ $t(`flag-status.${ON}`) }}</code></template- >- </i18n>-->- </ul></VContentPage></template>@@ -76,6 +83,8 @@import { computed, defineComponent } from "vue"import { useContext } from "@nuxtjs/composition-api"+import featureData from "~~/feat/feature-flags.json"+import { useFeatureFlagStore, getFlagStatus } from "~/stores/feature-flag"import { SWITCHABLE, ON, OFF, FEATURE_STATES } from "~/constants/feature-flag"@@ -122,6 +131,8 @@ export default defineComponent({handleChange,getFlagStatus,++ featureData,}},})diff --git a/frontend/src/styles/tailwind.css b/frontend/src/styles/tailwind.cssindex 63ca458d5..06b6915e9 100644--- a/frontend/src/styles/tailwind.css+++ b/frontend/src/styles/tailwind.css@@ -144,6 +144,12 @@ Time - 11px - xs semibold (default leading-[120%])* The color of the ring is set by a plugin defined in `tailwind.config.js`.*/+ [class*="focus-slim-offset"] {+ @apply focus-visible:outline-none; /* UA styles: none */+ @apply focus-visible:ring; /* outer-stroke: box-shadow */+ @apply ring-offset-1;+ }+[class*="focus-slim-tx"] {@apply focus-visible:outline-none; /* UA styles: none */@apply focus-visible:ring; /* outer-stroke: box-shadow */diff --git a/frontend/tailwind.config.js b/frontend/tailwind.config.jsindex a93020537..1289918e7 100644--- a/frontend/tailwind.config.js+++ b/frontend/tailwind.config.js@@ -6,7 +6,7 @@ const { Z_INDICES } = require("./src/constants/z-indices")module.exports = {content: [- "src/**/*.{vue,js,jsx,ts,tsx,mdx}",+ "./src/**/*.{vue,js,jsx,ts,tsx,mdx}","./nuxt.config.js","./tailwind.safelist.txt",],@@ -73,14 +73,17 @@ module.exports = {// Indexed by multiples of baseline (~ `0.25rem`)0: "0",0.5: "0.125rem",+ 0.75: "0.1875rem",1: "0.25rem",2: "0.50rem",3: "0.75rem",4: "1.00rem",+ 4.5: "1.125rem",5: "1.25rem",6: "1.50rem",7: "1.75rem",8: "2.00rem",+ 9: "2.25rem",10: "2.50rem",12: "3.00rem",14: "3.50rem",@@ -234,9 +237,12 @@ module.exports = {plugin(({ matchUtilities, theme }) => {matchUtilities(Object.fromEntries(- ["focus-slim-tx", "focus-slim-filled", "focus-bold-filled"].map(- (item) => [item, (value) => ({ "--tw-ring-color": value })]- )+ [+ "focus-slim-offset",+ "focus-slim-tx",+ "focus-slim-filled",+ "focus-bold-filled",+ ].map((item) => [item, (value) => ({ "--tw-ring-color": value })])),{ values: { ...theme("colors"), DEFAULT: theme("colors.pink") } })