Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add recent searches to the search store (#1865)

Zack Krida6de7e20174ea
diff --git a/feat/feature-flags.json b/feat/feature-flags.json
index c000f0229..df4a6767c 100644
--- a/feat/feature-flags.json
+++ b/feat/feature-flags.json
@@ -16,6 +16,14 @@
"description": "Toggle the new header.",
"defaultState": "off"
},
+ "recent_searches": {
+ "status": {
+ "staging": "switchable",
+ "production": "disabled"
+ },
+ "description": "Save and display the user's recent searches.",
+ "defaultState": "on"
+ },
"feat_disabled": {
"status": "disabled",
"description": "Will always be disabled"
diff --git a/package.json b/package.json
index ba6dea59e..b921a311c 100644
--- a/package.json
+++ b/package.json
@@ -72,6 +72,7 @@
"@popperjs/core": "^2.11.2",
"@tailwindcss/line-clamp": "^0.3.1",
"@tailwindcss/typography": "^0.5.2",
+ "@vueuse/core": "^9.3.0",
"@wordpress/is-shallow-equal": "^4.6.0",
"async-mutex": "^0.3.2",
"axios": "^0.27.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 328797057..ab81c1309 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -42,6 +42,7 @@ specifiers:
'@typescript-eslint/parser': ^5.20.0
'@vue/runtime-dom': ^3.2.37
'@vue/test-utils': ^1.1.3
+ '@vueuse/core': ^9.3.0
'@wordpress/is-shallow-equal': ^4.6.0
async-mutex: ^0.3.2
autoprefixer: ^10.4.0
@@ -124,6 +125,7 @@ dependencies:
'@popperjs/core': 2.11.2
'@tailwindcss/line-clamp': [email protected]
'@tailwindcss/typography': [email protected]
+ '@vueuse/core': [email protected]
'@wordpress/is-shallow-equal': 4.8.0
async-mutex: 0.3.2
axios: 0.27.2
@@ -1719,14 +1721,12 @@ packages:
/@cspotcode/source-map-consumer/0.8.0:
resolution: {integrity: sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==}
engines: {node: '>= 12'}
- dev: true
/@cspotcode/source-map-support/0.7.0:
resolution: {integrity: sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA==}
engines: {node: '>=12'}
dependencies:
'@cspotcode/source-map-consumer': 0.8.0
- dev: true
/@csstools/convert-colors/1.4.0:
resolution: {integrity: sha512-5a6wqoJV/xEdbRNKVo6I4hO3VjyDq//8q2f9I6PBAvMesJHFauXDorcNCsr9RzvsZnaWi5NYCcfyqP1QeFHFbw==}
@@ -4688,19 +4688,15 @@ packages:
/@tsconfig/node10/1.0.8:
resolution: {integrity: sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==}
- dev: true
/@tsconfig/node12/1.0.9:
resolution: {integrity: sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==}
- dev: true
/@tsconfig/node14/1.0.1:
resolution: {integrity: sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==}
- dev: true
/@tsconfig/node16/1.0.2:
resolution: {integrity: sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==}
- dev: true
/@types/anymatch/3.0.0:
resolution: {integrity: sha512-qLChUo6yhpQ9k905NwL74GU7TxH+9UODwwQ6ICNI+O6EDMExqH/Cv9NsbmcZ7yC/rRXJ/AHCzfgjsFRY5fKjYw==}
@@ -5090,6 +5086,10 @@ packages:
resolution: {integrity: sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==}
dev: true
+ /@types/web-bluetooth/0.0.15:
+ resolution: {integrity: sha512-w7hEHXnPMEZ+4nGKl/KDRVpxkwYxYExuHOYXyzIzCDzEZ9ZCGMAewulr9IqJu2LR4N37fcnb1XVeuZ09qgOxhA==}
+ dev: false
+
/@types/webpack-bundle-analyzer/3.9.3:
resolution: {integrity: sha512-l/vaDMWGcXiMB3CbczpyICivLTB07/JNtn1xebsRXE9tPaUDEHgX3x7YP6jfznG5TOu7I4w0Qx1tZz61znmPmg==}
dependencies:
@@ -5439,7 +5439,6 @@ packages:
'@babel/parser': 7.18.13
postcss: 8.4.16
source-map: 0.6.1
- dev: false
/@vue/compiler-sfc/3.2.37:
resolution: {integrity: sha512-+7i/2+9LYlpqDv+KTtWhOZH+pa8/HnX/905MdVmAcI/mPQOBwkHHIzrsEsucyOIZQYMkXUiTkmZq5am/NyXKkg==}
@@ -5603,7 +5602,6 @@ packages:
/@vue/devtools-api/6.2.1:
resolution: {integrity: sha512-OEgAMeQXvCoJ+1x8WyQuVZzFo0wcyCmUR3baRVLmKBo1LmYZWMlRiXlux5jd0fqVJu6PfDbOrZItVqUEzLobeQ==}
- dev: false
/@vue/reactivity-transform/3.2.37:
resolution: {integrity: sha512-IWopkKEb+8qpu/1eMKVeXrK0NLw9HicGviJzhJDEyfxTR9e1WtpnnbYkJWurX6WwoFP0sz10xQg8yL8lgskAZg==}
@@ -5663,6 +5661,31 @@ packages:
vue-template-compiler: 2.7.10
dev: true
+ /@vueuse/core/[email protected]:
+ resolution: {integrity: sha512-64Rna8IQDWpdrJxgitDg7yv1yTp41ZmvV8zlLEylK4QQLWAhz1OFGZDPZ8bU4lwcGgbEJ2sGi2jrdNh4LttUSQ==}
+ dependencies:
+ '@types/web-bluetooth': 0.0.15
+ '@vueuse/metadata': 9.3.0
+ '@vueuse/shared': [email protected]
+ vue-demi: [email protected]
+ transitivePeerDependencies:
+ - '@vue/composition-api'
+ - vue
+ dev: false
+
+ /@vueuse/metadata/9.3.0:
+ resolution: {integrity: sha512-GnnfjbzIPJIh9ngL9s9oGU1+Hx/h5/KFqTfJykzh/1xjaHkedV9g0MASpdmPZIP+ynNhKAcEfA6g5i8KXwtoMA==}
+ dev: false
+
+ /@vueuse/shared/[email protected]:
+ resolution: {integrity: sha512-caGUWLY0DpPC6l31KxeUy6vPVNA0yKxx81jFYLoMpyP6cF84FG5Dkf69DfSUqL57wX8JcUkJDMnQaQIZPWFEQQ==}
+ dependencies:
+ vue-demi: [email protected]
+ transitivePeerDependencies:
+ - '@vue/composition-api'
+ - vue
+ dev: false
+
/@webassemblyjs/ast/1.9.0:
resolution: {integrity: sha512-C6wW5L+b7ogSDVqymbkkvuW9kruN//YisMED04xzeBBqjHa2FYnmvOlS6Xj68xWQRgWvI9cIglsjFowH/RJyEA==}
dependencies:
@@ -5829,12 +5852,10 @@ packages:
acorn: 7.4.1
acorn-walk: 7.2.0
xtend: 4.0.2
- dev: true
/acorn-walk/7.2.0:
resolution: {integrity: sha512-OPdCF6GsMIP+Az+aWfAAOEt2/+iVDKE7oy6lJ098aoe59oAmK76qV6Gw60SbZ8jHuG2wH058GF4pLFbYamYrVA==}
engines: {node: '>=0.4.0'}
- dev: true
/acorn-walk/8.2.0:
resolution: {integrity: sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==}
@@ -5849,13 +5870,11 @@ packages:
resolution: {integrity: sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==}
engines: {node: '>=0.4.0'}
hasBin: true
- dev: true
/acorn/8.7.0:
resolution: {integrity: sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==}
engines: {node: '>=0.4.0'}
hasBin: true
- dev: true
/acorn/8.7.1:
resolution: {integrity: sha512-Xx54uLJQZ19lKygFXOWsscKUbsBZW0CPykPhVQdhIeIwrbPmJzqeASDInc8nKBnp/JT6igTs82qPXz069H8I/A==}
@@ -6960,7 +6979,6 @@ packages:
/camelcase-css/2.0.1:
resolution: {integrity: sha512-QOSvevhslijgYwRx6Rv7zKdMF8lbRmx+uQGx2+vDc+KI/eBnsy9kit5aj23AgGu3pa4t9AgwbnXWqS+iOY+2aA==}
engines: {node: '>= 6'}
- dev: true
/camelcase-keys/2.1.0:
resolution: {integrity: sha512-bA/Z/DERHKqoEOrp+qeGKw1QlvEQkGZSc0XaY6VnTxZr+Kv1G5zFwttpjv8qxZ/sBPT4nthwZaAcsAZTJlSKXQ==}
@@ -7485,7 +7503,7 @@ packages:
lodash: ^4.17.20
marko: ^3.14.4
mote: ^0.2.0
- mustache: ^4.0.1
+ mustache: ^3.0.0
nunjucks: ^3.2.2
plates: ~0.4.11
pug: ^3.0.0
@@ -7654,7 +7672,7 @@ packages:
lodash: ^4.17.20
marko: ^3.14.4
mote: ^0.2.0
- mustache: ^4.0.1
+ mustache: ^3.0.0
nunjucks: ^3.2.2
plates: ~0.4.11
pug: ^3.0.0
@@ -8295,7 +8313,6 @@ packages:
/csstype/3.1.0:
resolution: {integrity: sha512-uX1KG+x9h5hIJsaKR9xHUeUraxf8IODOwq9JLNPq6BwB04a/xgpq3rcx47l5BZu5zBPlgD342tdke3Hom/nJRA==}
- dev: false
/cuint/0.2.2:
resolution: {integrity: sha1-QICG1AlVDCYxFVYZ6fp7ytw7mRs=}
@@ -8466,7 +8483,6 @@ packages:
/defined/1.0.0:
resolution: {integrity: sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=}
- dev: true
/defu/2.0.4:
resolution: {integrity: sha512-G9pEH1UUMxShy6syWk01VQSRVs3CDWtlxtZu7A+NyqjxaCA4gSlWAKDBx6QiUEKezqS8+DUlXLI14Fp05Hmpwg==}
@@ -8565,7 +8581,6 @@ packages:
acorn-node: 1.8.2
defined: 1.0.0
minimist: 1.2.5
- dev: true
/devalue/2.0.1:
resolution: {integrity: sha512-I2TiqT5iWBEyB8GRfTDP0hiLZ0YeDJZ+upDxjBfOC2lebO5LezQMv7QvIUTzdb64jQyAKLf1AHADtGN+jw6v8Q==}
@@ -8573,7 +8588,6 @@ packages:
/didyoumean/1.2.2:
resolution: {integrity: sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==}
- dev: true
/diff-sequences/26.6.2:
resolution: {integrity: sha512-Mv/TDa3nZ9sbc5soK+OoA74BsS3mL37yixCvUAQkiuA4Wz6YtwP/K47n2rv2ovzHZvoiQeA5FTQOschKkEwB0Q==}
@@ -8583,7 +8597,6 @@ packages:
/diff/4.0.2:
resolution: {integrity: sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==}
engines: {node: '>=0.3.1'}
- dev: true
/diffie-hellman/5.0.3:
resolution: {integrity: sha512-kqag/Nl+f3GwyK25fhUMYj81BUOrZ9IuJsjIcDE5icNM9FJHAVm3VcUDxdLPoQtTuUylWm6ZIknYJwwaPxsUzg==}
@@ -8607,7 +8620,6 @@ packages:
/dlv/1.1.3:
resolution: {integrity: sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==}
- dev: true
/doctrine/2.1.0:
resolution: {integrity: sha512-35mSku4ZXK0vfCuHEDAwt55dg2jNajHZ1odvF+8SSr82EsZY4QmXfuWso8oEd8zRhVObSN18aM0CjSdoBX7zIw==}
@@ -10068,7 +10080,6 @@ packages:
engines: {node: '>=10.13.0'}
dependencies:
is-glob: 4.0.3
- dev: true
/glob-promise/[email protected]:
resolution: {integrity: sha512-q08RJ6O+eJn+dVanerAndJwIcumgbDdYiUT7zFQl3Wm1xD6fBKtah7H8ZJChj4wP+8C+QfeVy8xautR7rdmKEw==}
@@ -10628,7 +10639,6 @@ packages:
engines: {node: '>=8'}
dependencies:
import-from: 3.0.0
- dev: true
/import-fresh/2.0.0:
resolution: {integrity: sha1-2BNVwVYS04bGH53dOSLUMEgipUY=}
@@ -10657,7 +10667,6 @@ packages:
engines: {node: '>=8'}
dependencies:
resolve-from: 5.0.0
- dev: true
/import-local/3.0.3:
resolution: {integrity: sha512-bE9iaUY3CXH8Cwfan/abDKAxe1KGT9kyGsBPqf6DMK/z0a2OzAsrukeYNgIH6cH5Xr452jb1TUL8rSfCLjZ9uA==}
@@ -12116,7 +12125,6 @@ packages:
/lilconfig/2.0.4:
resolution: {integrity: sha512-bfTIN7lEsiooCocSISTWXkiWJkRqtL9wYtYy+8EK3Y41qh3mpwPU0ycTOgjdY9ErwXCc8QyrQp82bdL0Xkm9yA==}
engines: {node: '>=10'}
- dev: true
/lines-and-columns/1.2.4:
resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==}
@@ -12390,7 +12398,6 @@ packages:
/make-error/1.3.6:
resolution: {integrity: sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==}
- dev: true
/makeerror/1.0.12:
resolution: {integrity: sha512-JmqCvUhmt43madlpFzG4BQzG2Z3m6tvQDNKdClZnO3VbIudJYmxsT0FNJMeiB2+JTSlTQTSbU8QdesVmwJcmLg==}
@@ -13206,7 +13213,6 @@ packages:
/object-hash/2.2.0:
resolution: {integrity: sha512-gScRMn0bS5fH+IuwyIFgnh9zBdo4DV+6GhygmWM9HyNJSgS0hScp1f5vjtm7oIIOiT9trXrShAkLFSc2IqKNgw==}
engines: {node: '>= 6'}
- dev: true
/object-inspect/1.12.0:
resolution: {integrity: sha512-Ho2z80bVIvJloH+YzRmpZVQe87+qASmBUKZDWgx9cu+KDrX2ZDH/3tMy+gXbZETVGs2M8YdxObOh7XAtim9Y0g==}
@@ -13735,7 +13741,6 @@ packages:
typescript: 4.6.2
vue: 2.7.10
- dev: false
/pinkie-promise/2.0.1:
resolution: {integrity: sha512-0Gni6D4UcLTbv9c57DfxDGdr41XfgUjqWZu492f0cIGr16zDU06BWP/RAEvOuo7CQ0CNjHaLlM59YJJFm3NWlw==}
@@ -14077,7 +14082,6 @@ packages:
dependencies:
camelcase-css: 2.0.1
postcss: 8.4.16
- dev: true
/postcss-lab-function/2.0.1:
resolution: {integrity: sha512-whLy1IeZKY+3fYdqQFuDBf8Auw+qFuVnChWjmxm/UhHWqNHZx+B99EwxTvGYmUBqe3Fjxs4L1BoZTJmPu6usVg==}
@@ -14109,7 +14113,6 @@ packages:
lilconfig: 2.0.4
ts-node: 10.7.0_cbygeln5s5sjlseyk3kbidkfny
yaml: 1.10.2
- dev: true
/postcss-loader/3.0.0:
resolution: {integrity: sha512-cLWoDEY5OwHcAjDnkyRQzAXfs2jrKjXpO/HQFcc5b5u/r7aa471wdmChmwfnv7x2u840iat/wi0lQ5nbRgSkUA==}
@@ -14301,7 +14304,6 @@ packages:
dependencies:
postcss: 8.4.16
postcss-selector-parser: 6.0.9
- dev: true
/postcss-nesting/7.0.1:
resolution: {integrity: sha512-FrorPb0H3nuVq0Sff7W2rnc3SmIcruVC6YwpcS+k687VxyxO33iE1amna7wHuRVzM8vfiYofXSBHNAZ3QhLvYg==}
@@ -15076,7 +15078,6 @@ packages:
/quick-lru/5.1.1:
resolution: {integrity: sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==}
engines: {node: '>=10'}
- dev: true
/ramda/0.21.0:
resolution: {integrity: sha512-HGd5aczYKQXGILB+abY290V7Xz62eFajpa6AtMdwEmQSakJmgSO7ks4eI3HdR34j+X2Vz4Thp9VAJbrCAMbO2w==}
@@ -15503,7 +15504,6 @@ packages:
/resolve-from/5.0.0:
resolution: {integrity: sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==}
engines: {node: '>=8'}
- dev: true
/resolve-path/1.4.0:
resolution: {integrity: sha1-xL2p9e+y/OZSR4c6s2u02DT+Fvc=}
@@ -16643,7 +16643,6 @@ packages:
resolve: 1.22.0
transitivePeerDependencies:
- ts-node
- dev: true
/talkback/3.0.1:
resolution: {integrity: sha512-2/mBW8vhcNw3JN5q8Pvq9QLdd3seks859EVabmRpSev2cyCPX4A7eU87jUnlBDOyXYdXPAflJ7cRkyTHmSKNGQ==}
@@ -16994,7 +16993,6 @@ packages:
typescript: 4.6.2
v8-compile-cache-lib: 3.0.0
yn: 3.1.1
- dev: true
resolution: {integrity: sha512-csd+vJOb/gkzvcCHgTGSChYpy5f1/XKNsmvBGO4JXS+z1v2HobugDz4s1IeFXM3wZB44uczs+eazB5Q/ccdhQw==}
@@ -17107,7 +17105,6 @@ packages:
resolution: {integrity: sha512-HM/hFigTBHZhLXshn9sN37H085+hQGeJHJ/X7LpBWLID/fbc2acUMfU+lGD98X81sKP+pFa9f0DZmCwB9GnbAg==}
engines: {node: '>=4.2.0'}
hasBin: true
- dev: true
/ua-parser-js/0.7.31:
resolution: {integrity: sha512-qLK/Xe9E2uzmYI3qLeOmI0tEOt+TBBQyUIAh4aAgU05FVYzeZrKUdkAZfBNVGRaHVgV0TDkdEngJSw/SyQchkQ==}
@@ -17399,7 +17396,6 @@ packages:
/v8-compile-cache-lib/3.0.0:
resolution: {integrity: sha512-mpSYqfsFvASnSn5qMiwrr4VKfumbPyONLCOPmsR3A6pTY/r0+tSaVbgPWSAIuzbk3lCTa+FForeTiO+wBQGkjA==}
- dev: true
/v8-compile-cache/2.3.0:
resolution: {integrity: sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==}
@@ -17826,7 +17822,6 @@ packages:
dependencies:
'@vue/compiler-sfc': 2.7.10
csstype: 3.1.0
- dev: false
resolution: {integrity: sha512-ETW44IqCgBpVomy520DT5jf8n0zoCac+sxWnn+hMe/CzaSejb/eVw2YToiXYX+Ex/AuHHia28vWTq4goAexFbw==}
@@ -18328,7 +18323,6 @@ packages:
/yn/3.1.1:
resolution: {integrity: sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==}
engines: {node: '>=6'}
- dev: true
/yocto-queue/0.1.0:
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
diff --git a/src/components/VNotificationBanner.vue b/src/components/VNotificationBanner.vue
index 373ca9595..6cd2e9faa 100644
--- a/src/components/VNotificationBanner.vue
+++ b/src/components/VNotificationBanner.vue
@@ -29,9 +29,10 @@
<script lang="ts">
import { defineComponent, PropType } from '@nuxtjs/composition-api'
+import { useStorage } from '@vueuse/core'
+
import { defineEvent } from '~/types/emits'
import { useI18n } from '~/composables/use-i18n'
-import { useStorage } from '~/composables/use-storage'
import VIconButton from '~/components/VIconButton/VIconButton.vue'
diff --git a/src/composables/use-storage.ts b/src/composables/use-storage.ts
deleted file mode 100644
index e08522215..000000000
--- a/src/composables/use-storage.ts
+++ /dev/null
@@ -1,125 +0,0 @@
-import { ref, watch, Ref } from '@nuxtjs/composition-api'
-
-import local from '~/utils/local'
-
-import { useEventListener } from './use-event-listener'
-
-const defaultWindow =
- typeof window === 'undefined' || !('localStorage' in window)
- ? undefined
- : window
-
-export type Serializer<T> = {
- read(v: string): T
- write(v: T): string
-}
-
-export const StorageSerializers = Object.freeze({
- boolean: {
- read: (v) => v === 'true',
- write: (v) => String(v),
- } as Serializer<boolean>,
- number: {
- read: (v) => parseFloat(v),
- write: (v) => String(v),
- } as Serializer<number>,
- string: {
- read: (v) => v,
- write: (v) => String(v),
- } as Serializer<string>,
- object: {
- read: (v) => JSON.parse(v),
- write: (v) => JSON.stringify(v),
- // Disable reason: We do actually mean `any` here, not unknown
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- } as Serializer<any>,
-} as const)
-
-export type UseStorageOptions<T> = {
- listenToStorageChanges: boolean
- writeDefaults: boolean
- window: Window
- storage: Storage
- serializer: Serializer<T>
- onError: (e: Error) => void
-}
-
-const defaultStorageOptions = Object.freeze({
- listenToStorageChanges: true,
- writeDefaults: true,
- window: defaultWindow,
- storage: local,
- serializer: StorageSerializers.object,
- onError: (e) => {
- console.error(e)
- },
-} as UseStorageOptions<unknown>)
-
-/**
- * Reactive LocalStorage.
- *
- * @see https://vueuse.org/useStorage
- */
-export function useStorage<T>(
- key: string,
- initialValue: T,
- options: Partial<UseStorageOptions<T>> = {}
-): Ref<T> {
- const optionsWithDefaults = {
- ...defaultStorageOptions,
- ...options,
- }
- const { listenToStorageChanges, writeDefaults, window, storage, onError } =
- optionsWithDefaults
-
- const type =
- initialValue == null
- ? 'object'
- : typeof initialValue === 'boolean'
- ? 'boolean'
- : typeof initialValue === 'string'
- ? 'string'
- : typeof initialValue === 'number'
- ? 'number'
- : 'object'
-
- const data = ref(initialValue) as Ref<T>
- const serializer = options.serializer ?? StorageSerializers[type]
-
- function read(event?: StorageEvent) {
- if (!storage || (event && event.key !== key)) return
-
- try {
- const rawValue = event ? event.newValue : storage.getItem(key)
- if (rawValue == null) {
- data.value = initialValue as T
- if (writeDefaults && initialValue !== null)
- storage.setItem(key, serializer.write(initialValue))
- } else {
- data.value = serializer.read(rawValue) as T
- }
- } catch (e) {
- onError(e as Error)
- }
- }
-
- read()
-
- if (window && listenToStorageChanges)
- useEventListener(window, 'storage', (e: Event) =>
- setTimeout(() => read(e as StorageEvent), 0)
- )
-
- if (storage) {
- watch(data, () => {
- try {
- if (data.value == null) storage.removeItem(key)
- else storage.setItem(key, serializer.write(data.value))
- } catch (e) {
- onError(e as Error)
- }
- })
- }
-
- return data
-}
diff --git a/src/pages/search.vue b/src/pages/search.vue
index 4a94732a5..cf8aa28ac 100644
--- a/src/pages/search.vue
+++ b/src/pages/search.vue
@@ -74,6 +74,13 @@ export default defineComponent({
const mediaStore = useMediaStore()
const searchStore = useSearchStore()
+ // I don't know *exactly* why this is necessary, but without it
+ // transitioning from the homepage to this page breaks the
+ // watcher in useStorage and recent searches won't be saved
+ // properly. It is something related to Pinia, Nuxt SSR,
+ // hydration and Vue reactives. Hopefully fixed in Nuxt 3.
+ searchStore.refreshRecentSearches()
+
const searchTerm = computed(() => searchStore.searchTerm)
const searchType = computed(() => searchStore.searchType)
const query = computed(() => searchStore.searchQueryParams)
diff --git a/src/stores/search.ts b/src/stores/search.ts
index 50a27ce0c..0f09b74e4 100644
--- a/src/stores/search.ts
+++ b/src/stores/search.ts
@@ -1,5 +1,8 @@
import { defineStore } from 'pinia'
+import { useStorage } from '@vueuse/core'
+
+import { env } from '~/utils/env'
import { deepClone } from '~/utils/clone'
import type { DeepWriteable } from '~/types/utils'
@@ -32,6 +35,8 @@ import { useProviderStore } from '~/stores/provider'
import { useFeatureFlagStore } from '~/stores/feature-flag'
+import type { Ref } from '@nuxtjs/composition-api'
+
import type { Dictionary } from 'vue-router/types/router'
export const isSearchTypeSupported = (
@@ -42,6 +47,7 @@ export const isSearchTypeSupported = (
export interface SearchState {
searchType: SearchType
+ recentSearches: Ref<string[]>
searchTerm: string
filters: Filters
}
@@ -71,8 +77,13 @@ export const useSearchStore = defineStore('search', {
state: (): SearchState => ({
searchType: ALL_MEDIA,
searchTerm: '',
+ recentSearches: useStorage<string[]>('recent-searches', []),
filters: deepClone(filterData as DeepWriteable<typeof filterData>),
}),
+ hydrate(state) {
+ // @ts-expect-error https://github.com/microsoft/TypeScript/issues/43826
+ state.recentSearches = useStorage<string[]>('recent-searches', [])
+ },
getters: {
filterCategories(state) {
return Object.keys(state.filters) as FilterCategory[]
@@ -156,11 +167,47 @@ export const useSearchStore = defineStore('search', {
this.clearOtherMediaTypeFilters(type)
},
setSearchTerm(term: string) {
- this.searchTerm = term.trim()
+ const formattedTerm = term.trim()
+ this.searchTerm = formattedTerm
+ this.addRecentSearch(formattedTerm)
+ },
+ /**
+ * This method need not exist and is only used to fix an odd
+ * hydration bug in the search route. After navigating from
+ * the homepage, the watcher in useStorage doesn't work.
+ */
+ refreshRecentSearches() {
+ // @ts-expect-error https://github.com/microsoft/TypeScript/issues/43826
+ this.recentSearches = useStorage<string[]>('recent-searches', [])
+ },
+ /** Add a new term to the list of recent search terms */
+ addRecentSearch(
+ search: string /** A search term to add to the saved list.*/
+ ) {
+ // No-op if feature isn't enabled
+ const featureFlagStore = useFeatureFlagStore()
+ const saveRecentSearches = featureFlagStore.isOn('recent_searches')
+ if (!saveRecentSearches) {
+ return
+ }
+
+ /**
+ * Add the latest search to the top of the stack,
+ * then add the existing items, making sure not to exceed
+ * the max count, and removing existing occurrences of the
+ * most latest search term, if there are any.
+ */
+ this.recentSearches = [
+ search,
+ ...this.recentSearches.filter((i) => i !== search),
+ ].slice(0, parseInt(env.savedSearchCount))
},
computeQueryParams(type: SupportedSearchType) {
return computeQueryParams(type, this.filters, this.searchTerm)
},
+ clearRecentSearches() {
+ this.recentSearches = []
+ },
/**
* Initial filters do not include the provider filters. We create the provider filters object
* when we fetch the provider data on the Nuxt server initialization.
diff --git a/src/utils/env.ts b/src/utils/env.ts
index 3198c4cf0..7f3e995f6 100644
--- a/src/utils/env.ts
+++ b/src/utils/env.ts
@@ -8,5 +8,6 @@ const apiUrl = process.env.API_URL ?? 'https://api.openverse.engineering/'
export const env = {
apiUrl: apiUrl.endsWith('/') ? apiUrl : `${apiUrl}/`,
filterStorageKey: 'openverse-filter-visibility',
+ savedSearchCount: '4',
providerUpdateFrequency: `${60 * 60 * 1000}`, // 1 hour
} as const
diff --git a/test/unit/specs/stores/search-store.spec.js b/test/unit/specs/stores/search-store.spec.js
index 1c22f1ef5..7568847a1 100644
--- a/test/unit/specs/stores/search-store.spec.js
+++ b/test/unit/specs/stores/search-store.spec.js
@@ -2,6 +2,8 @@ import { nextTick } from '@nuxtjs/composition-api'
import { setActivePinia, createPinia } from '~~/test/unit/test-utils/pinia'
+import { env } from '~/utils/env'
+
import { filterData, mediaFilterKeys } from '~/constants/filters'
import {
ALL_MEDIA,
@@ -435,5 +437,47 @@ describe('Search Store', () => {
})
expect(searchStore.isAnyFilterApplied).toEqual(false)
})
+
+ describe('Recent searches', () => {
+ it('are saved, without duplication and not exceeding the limit.', () => {
+ const searchStore = useSearchStore()
+ const featureFlagStore = useFeatureFlagStore()
+ featureFlagStore.toggleFeature('recent_searches', 'on')
+
+ searchStore.setSearchTerm('boop')
+ searchStore.setSearchTerm('bar')
+ searchStore.setSearchTerm('foo')
+ searchStore.setSearchTerm('baz')
+ searchStore.setSearchTerm('boom')
+ searchStore.setSearchTerm('foo')
+
+ expect(searchStore.recentSearches).toEqual([
+ 'foo',
+ 'boom',
+ 'baz',
+ 'bar',
+ ])
+ expect(searchStore.recentSearches.length).toEqual(
+ parseInt(env.savedSearchCount)
+ )
+ })
+ it('can be cleared', () => {
+ const searchStore = useSearchStore()
+ const featureFlagStore = useFeatureFlagStore()
+ featureFlagStore.toggleFeature('recent_searches', 'on')
+
+ // Clear up front in case of any preserved searches
+ searchStore.clearRecentSearches()
+
+ searchStore.setSearchTerm('boop')
+ searchStore.setSearchTerm('bar')
+
+ expect(searchStore.recentSearches).toEqual(['bar', 'boop'])
+
+ searchStore.clearRecentSearches()
+
+ expect(searchStore.recentSearches).toEqual([])
+ })
+ })
})
})