Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Website] Add a search params hook #77

Open
beefchimi opened this issue Jan 22, 2024 · 0 comments
Open

[Website] Add a search params hook #77

beefchimi opened this issue Jan 22, 2024 · 0 comments
Labels
💾 App Changes to the demo application

Comments

@beefchimi
Copy link
Owner

I want to be able to toggle some "Debug UI", so adding a hook to watch for window > popstate would be good... I attempted this however, and it did not work.

Some code for reference:

<script setup lang="ts">
import {watch} from 'vue';
import {useSearchParams} from '@/hooks';

const state = useSearchParams();

watch(state, (incoming) => {
  console.log('incoming keys', Object.keys(incoming));
  console.log('incoming values', Object.values(incoming));
});

function updateUrl(key: string, value?: string | number | boolean) {
  const params = new URLSearchParams(window.location.search);
  const safeValue = `${value}`;

  if (!value || params.has(key, safeValue)) {
    params.delete(key);
  } else {
    params.set(key, `${value}`);
  }

  const parsedParams = params.toString();
  history.pushState({}, '', `?${parsedParams}`);
}
</script>

<template>
  <button type="button" @click="() => updateUrl('one', 2)">One</button>
  <button type="button" @click="() => updateUrl('foo', 'bar')">Two</button>
  <button type="button" @click="() => updateUrl('cheese', true)">Three</button>
  <button type="button" @click="() => updateUrl('nope', false)">Four</button>
  <button type="button" @click="() => updateUrl('forget')">Five</button>
</template>
import {reactive, onMounted, onUnmounted} from 'vue';

import {supportDom} from '@earwurm/utilities';
import {useSupported} from './useSupported';

export type UrlParams = Record<string, string[] | string>;

function read() {
  const rawParams = window.location.search || '';
  return new URLSearchParams(rawParams);
}

// Adapted from:
// https://github.com/vueuse/vueuse/blob/main/packages/core/useUrlSearchParams
export function useSearchParams() {
  const isSupported = useSupported(supportDom);
  const state = reactive<UrlParams>({});

  if (!isSupported) return state;

  function updateState(params: URLSearchParams) {
    const unusedKeys = new Set(Object.keys(state));

    for (const key of params.keys()) {
      const paramsForKey = params.getAll(key);

      state[key] =
        paramsForKey.length > 1 ? paramsForKey : params.get(key) || '';

      unusedKeys.delete(key);
    }

    Array.from(unusedKeys).forEach((key) => delete state[key]);
  }

  function handlePopState(event: PopStateEvent) {
    console.log('PopState > event', event);

    const params = read();
    updateState(params);
  }

  const initial = read();

  if (initial.keys().next().value) {
    updateState(initial);
  } else {
    Object.assign(state, {});
  }

  onMounted(() => {
    window.addEventListener('popstate', handlePopState);
  });

  onUnmounted(() => {
    window.removeEventListener('popstate', handlePopState);
  });

  return state;
}
@beefchimi beefchimi added the 💾 App Changes to the demo application label Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💾 App Changes to the demo application
Projects
None yet
Development

No branches or pull requests

1 participant