mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
fix(web): settings accordion open state (#7504)
This commit is contained in:
parent
84fe41df31
commit
93f0a866a3
5 changed files with 108 additions and 107 deletions
|
|
@ -0,0 +1,35 @@
|
|||
<script lang="ts" context="module">
|
||||
export type AccordionState = Set<string>;
|
||||
|
||||
const { get: getAccordionState, set: setAccordionState } = createContext<Writable<AccordionState>>();
|
||||
export { getAccordionState };
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { writable, type Writable } from 'svelte/store';
|
||||
import { createContext } from '$lib/utils/context';
|
||||
import { page } from '$app/stores';
|
||||
import { handlePromiseError } from '$lib/utils';
|
||||
import { goto } from '$app/navigation';
|
||||
|
||||
const getParamValues = (param: string) => {
|
||||
return new Set(($page.url.searchParams.get(param) || '').split(' ').filter((x) => x !== ''));
|
||||
};
|
||||
|
||||
export let queryParam: string;
|
||||
export let state: Writable<AccordionState> = writable(getParamValues(queryParam));
|
||||
setAccordionState(state);
|
||||
|
||||
$: if (queryParam && $state) {
|
||||
const searchParams = new URLSearchParams($page.url.searchParams);
|
||||
if ($state.size > 0) {
|
||||
searchParams.set(queryParam, [...$state].join(' '));
|
||||
} else {
|
||||
searchParams.delete(queryParam);
|
||||
}
|
||||
|
||||
handlePromiseError(goto(`?${searchParams.toString()}`, { replaceState: true, noScroll: true, keepFocus: true }));
|
||||
}
|
||||
</script>
|
||||
|
||||
<slot />
|
||||
|
|
@ -1,28 +1,28 @@
|
|||
<script lang="ts">
|
||||
import { page } from '$app/stores';
|
||||
import { QueryParameter } from '$lib/constants';
|
||||
import { hasParamValue, handlePromiseError, updateParamList } from '$lib/utils';
|
||||
import { slide } from 'svelte/transition';
|
||||
import { getAccordionState } from './setting-accordion-state.svelte';
|
||||
|
||||
const accordionState = getAccordionState();
|
||||
|
||||
export let title: string;
|
||||
export let subtitle = '';
|
||||
export let key: string;
|
||||
export let isOpen = false;
|
||||
export let isOpen = $accordionState.has(key);
|
||||
|
||||
const syncFromUrl = () => (isOpen = hasParamValue(QueryParameter.IS_OPEN, key));
|
||||
const syncToUrl = (isOpen: boolean) => updateParamList({ param: QueryParameter.IS_OPEN, value: key, add: isOpen });
|
||||
$: setIsOpen(isOpen);
|
||||
|
||||
isOpen ? handlePromiseError(syncToUrl(true)) : syncFromUrl();
|
||||
$: $page.url && syncFromUrl();
|
||||
|
||||
const toggle = async () => {
|
||||
isOpen = !isOpen;
|
||||
await syncToUrl(isOpen);
|
||||
const setIsOpen = (isOpen: boolean) => {
|
||||
if (isOpen) {
|
||||
$accordionState = $accordionState.add(key);
|
||||
} else {
|
||||
$accordionState.delete(key);
|
||||
$accordionState = $accordionState;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<div class="border-b-[1px] border-gray-200 py-4 dark:border-gray-700">
|
||||
<button on:click={toggle} class="flex w-full place-items-center justify-between text-left">
|
||||
<button on:click={() => (isOpen = !isOpen)} class="flex w-full place-items-center justify-between text-left">
|
||||
<div>
|
||||
<h2 class="font-medium text-immich-primary dark:text-immich-dark-primary">
|
||||
{title}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue