Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add layout properties to the UI store and cookie to store them #1955

Merged
merged 5 commits into from
Nov 15, 2022

Conversation

obulat
Copy link
Contributor

@obulat obulat commented Nov 3, 2022

Fixes

Fixes #974 by @sarayourfriend

Description

This PR adds the following layout properties to the ui pinia store:

  • isDesktopLayout: whether the site should use the desktop or mobile layout (i.e., different headers, displaying menus in a modal on mobile or in a popover/sidebar on desktop).
  • isMobileUa: whether the User Agent is mobile. This is currently not used for layout, and I'm not sure whether we really need it or not. One thing I suspect this will be necessary for is any mobile-specific handling of the audio or video players.
  • innerFilterVisible: to keep track of the filters state. The components should use isFilterVisible as that getter updates the isFilterDismissed if the screen width has changed and we need to update the cookie state accordingly.
  • isFilterDismissed: on a desktop layout, the filter sidebar is always open by default. However, if the user dismisses it, we want to save this preference across requests.

The values are initialized from a cookie (and the request properties) in the middleware. Then, on every change of the layout, user agent or filter dismissed value, we save the value to the state and to the cookie.

Additional details

Unlike the approach outlined in the linked issue, this PR does not save the exact breakpoint. Instead, it saves whether the layout should be desktop or mobile (based on the current threshold: md for old_header and lg for new_header). This way, we can reuse the isMinScreen composable that uses matchQuery for this instead of adding a separate resize listener. Besides, we were going to use the current breakpoint to compute whether we should use a desktop or mobile layout anyways.

This PR adds two composables one composable:

  • to save data in cookies. It uses the NuxtApp app as a parameter to enable setting the cookies from outside of the setup function, namely, from the ui store. This turned out not necessary because Pinia Nuxt plugin can access app using this.$nuxt.app.
  • to update the layout values. This functionality should be used in the layout files that wrap all other components: the default.vue and blank.vue layouts.

Testing Instructions

I've added the unit tests for the ui store. The changes to the store will also be tested in #1956 which uses the ui store to set the layout properties.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@github-actions
Copy link

github-actions bot commented Nov 3, 2022

Storybook and Tailwind configuration previews: Ready

Storybook: https://wordpress.github.io/openverse-frontend/_preview/1955
Tailwind: https://wordpress.github.io/openverse-frontend/_preview/1955/tailwind

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

@github-actions
Copy link

github-actions bot commented Nov 3, 2022

Size Change: -56 B (0%)

Total Size: 823 kB

Filename Size Change
./.nuxt/dist/client/232.js 1.85 kB +1.58 kB (+581%) 🆘
./.nuxt/dist/client/232.modern.js 0 B -277 B (removed) 🏆
./.nuxt/dist/client/233.js 0 B -1.85 kB (removed) 🏆
./.nuxt/dist/client/app.js 131 kB +471 B (0%)
./.nuxt/dist/client/app.modern.js 122 kB +477 B (0%)
./.nuxt/dist/client/commons/app.modern.js 77 kB -12 B (0%)
./.nuxt/dist/client/components/v-all-results-grid.js 5.15 kB +2.39 kB (+87%) 🆘
./.nuxt/dist/client/components/v-all-results-grid.modern.js 5.01 kB +2.29 kB (+84%) 🆘
./.nuxt/dist/client/components/v-all-results-grid/pages/search/audio/pages/search/index.js 0 B -3.02 kB (removed) 🏆
./.nuxt/dist/client/components/v-all-results-grid/pages/search/audio/pages/search/index.modern.js 0 B -2.91 kB (removed) 🏆
./.nuxt/dist/client/components/v-grid-skeleton.js 1.62 kB +13 B (+1%)
./.nuxt/dist/client/pages/search/audio.js 3.75 kB +2.51 kB (+203%) 🆘
./.nuxt/dist/client/pages/search/audio.modern.js 3.65 kB +2.42 kB (+196%) 🆘
./.nuxt/dist/client/pages/search/index.js 508 B -2.45 kB (-83%) 🏆
./.nuxt/dist/client/pages/search/index.modern.js 513 B -2.42 kB (-83%) 🏆
./.nuxt/dist/client/runtime.js 2.69 kB -21 B (-1%)
./.nuxt/dist/client/runtime.modern.js 2.69 kB -23 B (-1%)
./.nuxt/dist/client/vendors/app.js 62.4 kB +63 B (0%)
./.nuxt/dist/client/vendors/app.modern.js 61.8 kB +152 B (0%)
./.nuxt/dist/client/231.js 272 B +272 B (new file) 🆕
./.nuxt/dist/client/231.modern.js 277 B +277 B (new file) 🆕
ℹ️ View Unchanged
Filename Size Change
./.nuxt/dist/client/commons/app.js 88.3 kB -6 B (0%)
./.nuxt/dist/client/components/loading-icon.js 745 B 0 B
./.nuxt/dist/client/components/loading-icon.modern.js 748 B -3 B (0%)
./.nuxt/dist/client/components/table-sort-icon.js 509 B 0 B
./.nuxt/dist/client/components/table-sort-icon.modern.js 513 B 0 B
./.nuxt/dist/client/components/v-audio-cell.js 357 B 0 B
./.nuxt/dist/client/components/v-audio-cell.modern.js 359 B -2 B (-1%)
./.nuxt/dist/client/components/v-audio-details.js 1.81 kB +3 B (0%)
./.nuxt/dist/client/components/v-audio-details.modern.js 1.79 kB -1 B (0%)
./.nuxt/dist/client/components/v-audio-track-skeleton.js 1.01 kB +1 B (0%)
./.nuxt/dist/client/components/v-audio-track-skeleton.modern.js 1.01 kB -1 B (0%)
./.nuxt/dist/client/components/v-audio-track.js 5.13 kB +4 B (0%)
./.nuxt/dist/client/components/v-audio-track.modern.js 5.07 kB +1 B (0%)
./.nuxt/dist/client/components/v-back-to-search-results-link.js 537 B 0 B
./.nuxt/dist/client/components/v-back-to-search-results-link.modern.js 542 B +1 B (0%)
./.nuxt/dist/client/components/v-bone.js 685 B -1 B (0%)
./.nuxt/dist/client/components/v-bone.modern.js 690 B 0 B
./.nuxt/dist/client/components/v-box-layout.js 1.2 kB -1 B (0%)
./.nuxt/dist/client/components/v-box-layout.modern.js 1.2 kB +1 B (0%)
./.nuxt/dist/client/components/v-content-link.js 1.11 kB -1 B (0%)
./.nuxt/dist/client/components/v-content-link.modern.js 1.09 kB +3 B (0%)
./.nuxt/dist/client/components/v-content-page.js 467 B 0 B
./.nuxt/dist/client/components/v-content-page.modern.js 471 B 0 B
./.nuxt/dist/client/components/v-content-report-button.js 778 B 0 B
./.nuxt/dist/client/components/v-content-report-button.modern.js 784 B 0 B
./.nuxt/dist/client/components/v-content-report-form.js 3.76 kB +5 B (0%)
./.nuxt/dist/client/components/v-content-report-form.modern.js 3.56 kB -3 B (0%)
./.nuxt/dist/client/components/v-content-report-popover.js 4.41 kB +3 B (0%)
./.nuxt/dist/client/components/v-content-report-popover.modern.js 4.22 kB -3 B (0%)
./.nuxt/dist/client/components/v-copy-button.js 3.99 kB 0 B
./.nuxt/dist/client/components/v-copy-button.modern.js 4 kB +1 B (0%)
./.nuxt/dist/client/components/v-copy-license.js 999 B 0 B
./.nuxt/dist/client/components/v-copy-license.modern.js 1 kB 0 B
./.nuxt/dist/client/components/v-copy-license/components/v-error-image/components/v-media-reuse/components/v-search-grid/09090664.js 9.5 kB -2 B (0%)
./.nuxt/dist/client/components/v-copy-license/components/v-error-image/components/v-media-reuse/components/v-search-grid/09090664.modern.js 9.48 kB +1 B (0%)
./.nuxt/dist/client/components/v-dmca-notice.js 747 B -1 B (0%)
./.nuxt/dist/client/components/v-dmca-notice.modern.js 753 B 0 B
./.nuxt/dist/client/components/v-error-image.js 1.69 kB -4 B (0%)
./.nuxt/dist/client/components/v-error-image.modern.js 1.68 kB 0 B
./.nuxt/dist/client/components/v-error-section.js 372 B 0 B
./.nuxt/dist/client/components/v-error-section.modern.js 375 B -1 B (0%)
./.nuxt/dist/client/components/v-external-search-form.js 3.09 kB +2 B (0%)
./.nuxt/dist/client/components/v-external-search-form.modern.js 3.06 kB 0 B
./.nuxt/dist/client/components/v-external-source-list.js 2.55 kB +1 B (0%)
./.nuxt/dist/client/components/v-external-source-list.modern.js 2.52 kB -2 B (0%)
./.nuxt/dist/client/components/v-full-layout.js 1.49 kB +2 B (0%)
./.nuxt/dist/client/components/v-full-layout.modern.js 1.49 kB -1 B (0%)
./.nuxt/dist/client/components/v-grid-skeleton.modern.js 1.62 kB +6 B (0%)
./.nuxt/dist/client/components/v-image-cell-square.js 1.02 kB +1 B (0%)
./.nuxt/dist/client/components/v-image-cell-square.modern.js 1.02 kB -1 B (0%)
./.nuxt/dist/client/components/v-image-cell.js 1.43 kB +1 B (0%)
./.nuxt/dist/client/components/v-image-cell.modern.js 1.42 kB 0 B
./.nuxt/dist/client/components/v-image-details.js 1.44 kB -3 B (0%)
./.nuxt/dist/client/components/v-image-details.modern.js 1.44 kB +1 B (0%)
./.nuxt/dist/client/components/v-image-grid.js 2.54 kB +2 B (0%)
./.nuxt/dist/client/components/v-image-grid.modern.js 2.42 kB +3 B (0%)
./.nuxt/dist/client/components/v-license-tab-panel.js 521 B -1 B (0%)
./.nuxt/dist/client/components/v-license-tab-panel.modern.js 525 B 0 B
./.nuxt/dist/client/components/v-load-more.js 790 B 0 B
./.nuxt/dist/client/components/v-load-more.modern.js 682 B 0 B
./.nuxt/dist/client/components/v-media-license.js 800 B 0 B
./.nuxt/dist/client/components/v-media-license.modern.js 808 B 0 B
./.nuxt/dist/client/components/v-media-reuse.js 1.62 kB -1 B (0%)
./.nuxt/dist/client/components/v-media-reuse.modern.js 1.62 kB +2 B (0%)
./.nuxt/dist/client/components/v-media-tag.js 430 B 0 B
./.nuxt/dist/client/components/v-media-tag.modern.js 434 B 0 B
./.nuxt/dist/client/components/v-no-results.js 2.75 kB +1 B (0%)
./.nuxt/dist/client/components/v-no-results.modern.js 2.72 kB -1 B (0%)
./.nuxt/dist/client/components/v-radio.js 1.51 kB -1 B (0%)
./.nuxt/dist/client/components/v-radio.modern.js 1.47 kB -2 B (0%)
./.nuxt/dist/client/components/v-related-audio.js 1.23 kB +2 B (0%)
./.nuxt/dist/client/components/v-related-audio.modern.js 1.23 kB -5 B (0%)
./.nuxt/dist/client/components/v-related-images.js 3.1 kB +3 B (0%)
./.nuxt/dist/client/components/v-related-images.modern.js 2.98 kB +4 B (0%)
./.nuxt/dist/client/components/v-report-desc-form.js 965 B -1 B (0%)
./.nuxt/dist/client/components/v-report-desc-form.modern.js 964 B 0 B
./.nuxt/dist/client/components/v-row-layout.js 1.7 kB -1 B (0%)
./.nuxt/dist/client/components/v-row-layout.modern.js 1.71 kB +4 B (0%)
./.nuxt/dist/client/components/v-scroll-button.js 812 B -1 B (0%)
./.nuxt/dist/client/components/v-scroll-button.modern.js 818 B -1 B (0%)
./.nuxt/dist/client/components/v-search-grid.js 5.43 kB +3 B (0%)
./.nuxt/dist/client/components/v-search-grid.modern.js 5.38 kB -4 B (0%)
./.nuxt/dist/client/components/v-search-results-title.js 658 B 0 B
./.nuxt/dist/client/components/v-search-results-title.modern.js 651 B -2 B (0%)
./.nuxt/dist/client/components/v-search-type-radio.js 793 B +1 B (0%)
./.nuxt/dist/client/components/v-search-type-radio.modern.js 768 B 0 B
./.nuxt/dist/client/components/v-server-timeout.js 299 B 0 B
./.nuxt/dist/client/components/v-server-timeout.modern.js 303 B 0 B
./.nuxt/dist/client/components/v-sketch-fab-viewer.js 997 B +1 B (0%)
./.nuxt/dist/client/components/v-sketch-fab-viewer.modern.js 895 B 0 B
./.nuxt/dist/client/components/v-skip-to-content-container.js 888 B -1 B (0%)
./.nuxt/dist/client/components/v-skip-to-content-container.modern.js 894 B +2 B (0%)
./.nuxt/dist/client/components/v-snackbar.js 1.18 kB -1 B (0%)
./.nuxt/dist/client/components/v-snackbar.modern.js 1.19 kB 0 B
./.nuxt/dist/client/components/v-sources-table.js 15.7 kB -2 B (0%)
./.nuxt/dist/client/components/v-sources-table.modern.js 15.8 kB +2 B (0%)
./.nuxt/dist/client/components/v-warning-suppressor.js 298 B 0 B
./.nuxt/dist/client/components/v-warning-suppressor.modern.js 303 B +1 B (0%)
./.nuxt/dist/client/pages/about.js 1.06 kB +1 B (0%)
./.nuxt/dist/client/pages/about.modern.js 1.07 kB 0 B
./.nuxt/dist/client/pages/audio/_id.js 4.89 kB 0 B
./.nuxt/dist/client/pages/audio/_id.modern.js 4.72 kB +4 B (0%)
./.nuxt/dist/client/pages/external-sources.js 1.45 kB +1 B (0%)
./.nuxt/dist/client/pages/external-sources.modern.js 1.46 kB 0 B
./.nuxt/dist/client/pages/feedback.js 1.24 kB +1 B (0%)
./.nuxt/dist/client/pages/feedback.modern.js 1.24 kB 0 B
./.nuxt/dist/client/pages/image/_id.js 5.3 kB -6 B (0%)
./.nuxt/dist/client/pages/image/_id.modern.js 7.26 kB +9 B (0%)
./.nuxt/dist/client/pages/index.js 4.97 kB +2 B (0%)
./.nuxt/dist/client/pages/index.modern.js 4.86 kB 0 B
./.nuxt/dist/client/pages/preferences.js 1.22 kB -2 B (0%)
./.nuxt/dist/client/pages/preferences.modern.js 1.2 kB -3 B (0%)
./.nuxt/dist/client/pages/search-help.js 1.54 kB +1 B (0%)
./.nuxt/dist/client/pages/search-help.modern.js 1.55 kB 0 B
./.nuxt/dist/client/pages/search.js 2.72 kB -3 B (0%)
./.nuxt/dist/client/pages/search.modern.js 2.57 kB 0 B
./.nuxt/dist/client/pages/search/image.js 2.79 kB -2 B (0%)
./.nuxt/dist/client/pages/search/image.modern.js 2.67 kB +2 B (0%)
./.nuxt/dist/client/pages/search/model-3d.js 243 B 0 B
./.nuxt/dist/client/pages/search/model-3d.modern.js 247 B 0 B
./.nuxt/dist/client/pages/search/search-page.types.js 266 B +1 B (0%)
./.nuxt/dist/client/pages/search/search-page.types.modern.js 271 B 0 B
./.nuxt/dist/client/pages/search/video.js 240 B 0 B
./.nuxt/dist/client/pages/search/video.modern.js 243 B 0 B
./.nuxt/dist/client/pages/sources.js 1.44 kB +2 B (0%)
./.nuxt/dist/client/pages/sources.modern.js 1.45 kB +1 B (0%)

compressed-size-action

src/stores/ui.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exciting and looking great!

src/stores/ui.ts Outdated Show resolved Hide resolved
src/stores/ui.ts Outdated Show resolved Hide resolved
src/stores/ui.ts Outdated Show resolved Hide resolved
src/stores/ui.ts Outdated Show resolved Hide resolved
@obulat obulat force-pushed the add/ui_state_cookie branch 2 times, most recently from 7037c00 to e832116 Compare November 4, 2022 11:48
@obulat obulat marked this pull request as ready for review November 4, 2022 12:45
@obulat obulat requested a review from a team as a code owner November 4, 2022 12:45
@obulat obulat requested review from zackkrida and dhruvkb November 4, 2022 12:45
@obulat obulat force-pushed the add/ui_state_cookie branch from e832116 to 5b57ece Compare November 4, 2022 13:06
@obulat obulat changed the title Add cookie-based layout properties to the UI store Add layout properties to the UI store and cookie to store them Nov 7, 2022
@obulat obulat added 🟧 priority: high Stalls work on the project or its dependents ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository labels Nov 7, 2022
@openverse-bot
Copy link
Contributor

Based on the high urgency of this PR, the following reviewers are being gently reminded to review this PR:

@zackkrida
@dhruvkb
This reminder is being automatically generated due to the urgency configuration.

Excluding weekend1 days, this PR was updated 2 day(s) ago. PRs labelled with high urgency are expected to be reviewed within 2 weekday(s)2.

@obulat, if this PR is not ready for a review, please draft it to prevent reviewers from getting further unnecessary pings.

Footnotes

  1. Specifically, Saturday and Sunday.

  2. For the purpose of these reminders we treat Monday - Friday as weekdays. Please note that the that generates these reminders runs at midnight UTC on Monday - Friday. This means that depending on your timezone, you may be pinged outside of the expected range.

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@obulat Were you thinking of refactoring useCookies? I think the API could be improved if we used the approach I suggested elsewhere. Rather than passing the name parameter to useCookies, we could get and set the cookie by name in the closure functions the composable produces. It would also remove the need to have to pass an explicit generic parameter to useCookies like we'll need to now to get the right types for the closures.

Additionally, I am wondering if useCookies needs to expose a reactive object for the cookies in case the values change. For example, consider if we are relying on the value of the isDesktopLayout value rather than computed breakpoints. If we adjust the width of the viewport then will isDesktopLayout be recomputed (to false, if we go below a desktop layout width) and then cause a re-render? Right now I think the value might be static, even with the Pinia store, because the store values aren't hooked in (as far as I can tell) to a media query watcher.

@obulat
Copy link
Contributor Author

obulat commented Nov 10, 2022

@obulat Were you thinking of refactoring useCookies? I think the API could be improved if we used the approach I suggested elsewhere. Rather than passing the name parameter to useCookies, we could get and set the cookie by name in the closure functions the composable produces. It would also remove the need to have to pass an explicit generic parameter to useCookies like we'll need to now to get the right types for the closures.

Additionally, I am wondering if useCookies needs to expose a reactive object for the cookies in case the values change. For example, consider if we are relying on the value of the isDesktopLayout value rather than computed breakpoints. If we adjust the width of the viewport then will isDesktopLayout be recomputed (to false, if we go below a desktop layout width) and then cause a re-render? Right now I think the value might be static, even with the Pinia store, because the store values aren't hooked in (as far as I can tell) to a media query watcher.

Yes, I will refactor based on your suggestion, @sarayourfriend.

As for the second question, I've added the isMinScreen composable to the 2 layouts we use, with a watcher that sets the cookies when the value changes. The changes are in the companion PR, #1956

 const isMobileUa = useBrowserIsMobile()
const shouldPassInSSR = uiStore.isDesktopLayout ?? !isMobileUa
const desktopBreakpoint = isNewHeaderEnabled.value ? 'lg' : 'md'
const isDesktop = isMinScreen(desktopBreakpoint, { shouldPassInSSR })
const uiCookies = useCookies<UiStateCookie>(app, 'ui')
watch(isDesktop, (isDesktop) => {
      uiStore.updateBreakpoint(isDesktop, isMobileUa, uiCookies.setCookie)
    })

'reactive object for the cookies' sounds really interesting, and I will see if that can be used during the refactoring.

@obulat obulat force-pushed the add/ui_state_cookie branch 2 times, most recently from d1cc631 to 9b599fa Compare November 12, 2022 07:27
@@ -16,7 +20,7 @@ interface Options {
* Reactive Media Query.
*/
export function useMediaQuery(
query: string,
query: MaybeComputedRef<string>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To update the desktop threshold based on the new_header flag status, we need to pass a computed value here.

/* UI store */

const uiStore = useUiStore($pinia)
const isMobileUa = app.$ua ? app.$ua.isMobile : false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is expected that isMobileUa can be set once per request, and we do not need to change its value or update a cookie after that.

src/stores/ui.ts Outdated
* and `md` with the `old_header`).
* @param setCookieFn - sets the app cookie.
*/
updateBreakpoint(isDesktopLayout: boolean, setCookieFn: CookieSetter) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a check for isMobileUa here and only set the isDesktopLayout to true if isMobileUa is false. However, I'm not sure if it makes sense to use a mobile layout for lg mobile screens.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, that it doesn't make sense. To me, it would make sense to rely exclusively on the breakpoint information if we have it and fall back to UA sniffing only when we don't yet know any breakpoint information from the cookie.

src/stores/ui.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM! Tests well locally. Only requested change is to use unref instead of resolveRef in the watcher/effect callbacks especially.

src/composables/use-media-query.ts Outdated Show resolved Hide resolved
src/composables/use-media-query.ts Outdated Show resolved Hide resolved
src/stores/ui.ts Outdated
* and `md` with the `old_header`).
* @param setCookieFn - sets the app cookie.
*/
updateBreakpoint(isDesktopLayout: boolean, setCookieFn: CookieSetter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, that it doesn't make sense. To me, it would make sense to rely exclusively on the breakpoint information if we have it and fall back to UA sniffing only when we don't yet know any breakpoint information from the cookie.

@obulat obulat force-pushed the add/ui_state_cookie branch from a56d375 to e1c1860 Compare November 14, 2022 09:29
Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, works just as expected locally.

src/composables/use-cookies.ts Outdated Show resolved Hide resolved
secure: isProd,
}

export const useCookies = (app: NuxtAppOptions) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naïve question: why not use the useCookies composable from VueUse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you fro asking this question, @dhruvkb !

I've started answering why we can't use it, and then found a much nicer solution.

We use the cookie-universal-nuxt which injects $cookies to the Nuxt app. This means that we can only set cookies in components and not in a Pinia store because you can't access app from outside of a setup function.

That said, researching this issue, I've found that $nuxt was added to the Pinia Nuxt plugin, so we can set the cookies from the store by calling this.$nuxt.app.$cookies.set().

src/types/cookies.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look great to me, always nice to cut out some extra code (though I have to admit I found the composable nice to work with, especially w/r/t to typing).

To get cookies-universal-nuxt's types working, can you please add cookies-universal-nuxt to the types array in tsconfig.json? Otherwise $cookies is typed as any 😱

Create use-cookies composable
Move all the UI cookie updates to the UI store
Add cookieOptions
@obulat obulat force-pushed the add/ui_state_cookie branch from dd5279c to 630d501 Compare November 15, 2022 03:59
@WordPress WordPress deleted a comment from github-actions bot Nov 15, 2022
@obulat obulat merged commit 8221e6d into main Nov 15, 2022
@obulat obulat deleted the add/ui_state_cookie branch November 15, 2022 04:55
github-actions bot pushed a commit that referenced this pull request Nov 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature 🟧 priority: high Stalls work on the project or its dependents
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create useUiStateCookie composable
4 participants