Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

chore(client): replace @solid-primitives/storage with custom persistence signal (#1272)

Corentin Thomassetf6dadee378c1
diff --git a/apps/papra-client/package.json b/apps/papra-client/package.json
index 66506cc4..2fc62f98 100644
--- a/apps/papra-client/package.json
+++ b/apps/papra-client/package.json
@@ -31,7 +31,6 @@
"@modular-forms/solid": "^0.25.1",
"@papra/search-parser": "workspace:*",
"@pdfslick/solid": "^2.3.0",
- "@solid-primitives/storage": "^4.3.2",
"@solidjs/router": "^0.14.10",
"@tanstack/solid-query": "^5.90.3",
"@tanstack/solid-table": "^8.21.3",
diff --git a/apps/papra-client/src/modules/i18n/i18n.provider.tsx b/apps/papra-client/src/modules/i18n/i18n.provider.tsx
index cfaeb4fd..dab77819 100644
--- a/apps/papra-client/src/modules/i18n/i18n.provider.tsx
+++ b/apps/papra-client/src/modules/i18n/i18n.provider.tsx
@@ -1,14 +1,7 @@
import type { Accessor, ParentComponent, Setter } from 'solid-js';
import type { TranslationsDictionary } from './locales.types';
-import { makePersisted } from '@solid-primitives/storage';
-import {
- createContext,
- createEffect,
- createResource,
- createSignal,
- Show,
- useContext,
-} from 'solid-js';
+import { createContext, createEffect, createResource, Show, useContext } from 'solid-js';
+import { createPersistedSignal } from '@/modules/shared/signals/persistence/persistence.signals';
import { translations as defaultTranslations } from '../../locales/en.dictionary';
import { locales } from './i18n.constants';
import {
@@ -55,8 +48,8 @@ export const I18nProvider: ParentComponent = (props) => {
preferredLocales: navigator.languages.map((x) => new Intl.Locale(x)),
supportedLocales: locales.map((x) => new Intl.Locale(x.key)),
});
- const [getLocale, setLocale] = makePersisted(createSignal<Locale>(browserLocale), {
- name: 'papra_locale',
+ const [getLocale, setLocale] = createPersistedSignal<Locale>(browserLocale, {
+ key: 'papra_locale',
storage: localStorage,
});
diff --git a/apps/papra-client/src/modules/organizations/composables/use-last-organization.ts b/apps/papra-client/src/modules/organizations/composables/use-last-organization.ts
index ec637464..d21f5c30 100644
--- a/apps/papra-client/src/modules/organizations/composables/use-last-organization.ts
+++ b/apps/papra-client/src/modules/organizations/composables/use-last-organization.ts
@@ -1,11 +1,10 @@
-import { makePersisted } from '@solid-primitives/storage';
-import { createSignal } from 'solid-js';
+import { createPersistedSignal } from '@/modules/shared/signals/persistence/persistence.signals';
export function useLastOrganization() {
// TODO: migrate the persistence key to use the buildLocalStorageKey function from the persistence models
- const [getLatestOrganizationId, setLatestOrganizationId] = makePersisted(
- createSignal<string | null>(null),
- { name: 'papra_current_organization_id', storage: localStorage },
+ const [getLatestOrganizationId, setLatestOrganizationId] = createPersistedSignal<string | null>(
+ null,
+ { key: 'papra_current_organization_id', storage: localStorage },
);
return {
diff --git a/apps/papra-client/src/modules/organizations/composables/useCurrentOrganization.tsx b/apps/papra-client/src/modules/organizations/composables/useCurrentOrganization.tsx
index 39a89433..033b35f1 100644
--- a/apps/papra-client/src/modules/organizations/composables/useCurrentOrganization.tsx
+++ b/apps/papra-client/src/modules/organizations/composables/useCurrentOrganization.tsx
@@ -1,8 +1,8 @@
import type { ParentComponent } from 'solid-js';
import type { Organization } from '../organizations.types';
-import { makePersisted } from '@solid-primitives/storage';
import { useQuery } from '@tanstack/solid-query';
-import { createContext, createSignal, Show, useContext } from 'solid-js';
+import { createContext, Show, useContext } from 'solid-js';
+import { createPersistedSignal } from '@/modules/shared/signals/persistence/persistence.signals';
import { fetchOrganizations } from '../organizations.services';
const currentOrganizationContext = createContext<{
@@ -22,9 +22,9 @@ export function useCurrentOrganization() {
}
export const CurrentOrganizationProvider: ParentComponent = (props) => {
- const [getCurrentOrganizationId, setCurrentOrganizationId] = makePersisted(
- createSignal<string | null>(null),
- { name: 'papra_current_organization_id', storage: localStorage },
+ const [getCurrentOrganizationId, setCurrentOrganizationId] = createPersistedSignal<string | null>(
+ null,
+ { key: 'papra_current_organization_id', storage: localStorage },
);
const query = useQuery(() => ({
diff --git a/apps/papra-client/src/modules/shared/signals/persistence/persistence.signals.test.ts b/apps/papra-client/src/modules/shared/signals/persistence/persistence.signals.test.ts
new file mode 100644
index 00000000..a8991b51
--- /dev/null
+++ b/apps/papra-client/src/modules/shared/signals/persistence/persistence.signals.test.ts
@@ -0,0 +1,188 @@
+import { describe, expect, test } from 'vitest';
+import { createPersistedSignal } from './persistence.signals';
+
+function createTestStorage(initialEntries: Record<string, string> = {}) {
+ const storage = new Map<string, string>(Object.entries(initialEntries));
+
+ return {
+ getItem(key: string) {
+ return storage.get(key) ?? null;
+ },
+ setItem(key: string, value: string) {
+ storage.set(key, value);
+ },
+ removeItem(key: string) {
+ storage.delete(key);
+ },
+ getStorage() {
+ return Object.fromEntries(storage.entries());
+ },
+ };
+}
+
+describe('persistence.signals', () => {
+ describe('createPersistedSignal', () => {
+ describe('it behaves like a regular signal', () => {
+ test('it is created with the initial value and reflects updates', () => {
+ const storage = createTestStorage();
+ const [getValue, setValue] = createPersistedSignal('initial', { key: 'test-key', storage });
+
+ expect(getValue()).to.eql('initial');
+
+ setValue('updated');
+ expect(getValue()).to.eql('updated');
+ });
+
+ test('it supports the functional updater form, like createSignal', () => {
+ const storage = createTestStorage();
+ const [getValue, setValue] = createPersistedSignal(1, { key: 'test-key', storage });
+
+ setValue((previous) => previous + 1);
+
+ expect(getValue()).to.eql(2);
+ expect(storage.getStorage()).to.eql({ 'test-key': '2' });
+ });
+
+ test('the setter returns the new value, like createSignal', () => {
+ const storage = createTestStorage();
+ const [, setValue] = createPersistedSignal('initial', { key: 'test-key', storage });
+
+ expect(setValue('updated')).to.eql('updated');
+ });
+ });
+
+ describe('the initial value is never written to storage on its own', () => {
+ test('creating the signal does not write anything to storage', () => {
+ const storage = createTestStorage();
+ createPersistedSignal('initial', { key: 'test-key', storage });
+
+ expect(storage.getStorage()).to.eql({});
+ });
+
+ test('storage is only written once a value is explicitly set', () => {
+ const storage = createTestStorage();
+ const [, setValue] = createPersistedSignal('initial', { key: 'test-key', storage });
+
+ expect(storage.getStorage()).to.eql({});
+
+ setValue('updated');
+ expect(storage.getStorage()).to.eql({ 'test-key': '"updated"' });
+ });
+ });
+
+ describe('a previously persisted value is restored when the signal is created', () => {
+ test('the stored value takes precedence over the initial value', () => {
+ const storage = createTestStorage({ 'test-key': '"stored"' });
+ const [getValue] = createPersistedSignal('initial', { key: 'test-key', storage });
+
+ expect(getValue()).to.eql('stored');
+ });
+
+ test('non-string values are deserialized from their JSON representation', () => {
+ const storage = createTestStorage({ 'test-key': '42' });
+ const [getValue] = createPersistedSignal<number | null>(null, { key: 'test-key', storage });
+
+ expect(getValue()).to.eql(42);
+ });
+
+ test('the initial value is used when the stored value cannot be deserialized', () => {
+ const storage = createTestStorage({ 'test-key': 'not-json' });
+ const [getValue] = createPersistedSignal('initial', { key: 'test-key', storage });
+
+ expect(getValue()).to.eql('initial');
+ });
+ });
+
+ describe('every explicit set is persisted', () => {
+ test('a value is persisted even when it is equal to the initial value', () => {
+ // This guards against tying persistence to a (possibly dynamic) initial value: an explicit
+ // choice that happens to match the default must still survive a reload.
+ const storage = createTestStorage();
+ const [, setValue] = createPersistedSignal('en', { key: 'test-key', storage });
+
+ setValue('en');
+
+ expect(storage.getStorage()).to.eql({ 'test-key': '"en"' });
+ });
+
+ test('setting null removes the key from storage instead of writing it', () => {
+ const storage = createTestStorage();
+ const [, setValue] = createPersistedSignal<string | null>(null, {
+ key: 'test-key',
+ storage,
+ });
+
+ setValue('something');
+ expect(storage.getStorage()).to.eql({ 'test-key': '"something"' });
+
+ setValue(null);
+ expect(storage.getStorage()).to.eql({});
+ });
+
+ test('setting undefined removes the key from storage instead of writing it', () => {
+ const storage = createTestStorage({ 'test-key': '"something"' });
+ const [getValue, setValue] = createPersistedSignal<string | undefined>(undefined, {
+ key: 'test-key',
+ storage,
+ });
+
+ setValue(undefined);
+
+ expect(storage.getStorage()).to.eql({});
+
+ expect(getValue()).to.eql(undefined);
+ });
+ });
+
+ describe('it is resilient to storage and serialization failures', () => {
+ test('a value that cannot be serialized does not throw', () => {
+ const storage = createTestStorage();
+ const [getValue, setValue] = createPersistedSignal<unknown>('initial', {
+ key: 'test-key',
+ storage,
+ serialize: () => {
+ throw new Error('cannot serialize');
+ },
+ });
+
+ expect(() => setValue('updated')).not.toThrow();
+ // The in-memory signal is still updated even when persistence fails.
+ expect(getValue()).to.eql('updated');
+ });
+
+ test('a storage that throws on write does not throw', () => {
+ const storage = {
+ getItem: () => null,
+ setItem: () => {
+ throw new Error('quota exceeded');
+ },
+ removeItem: () => {},
+ };
+
+ const [getValue, setValue] = createPersistedSignal('initial', { key: 'test-key', storage });
+
+ expect(() => setValue('updated')).not.toThrow();
+ expect(getValue()).to.eql('updated');
+ });
+ });
+
+ describe('serialization can be customized', () => {
+ test('custom serialize and deserialize functions are used to round-trip the value', () => {
+ const options = {
+ key: 'test-key',
+ serialize: (value: { count: number }) => `count=${value.count}`,
+ deserialize: (value: string) => ({ count: Number(value.replace('count=', '')) }),
+ };
+ const storage = createTestStorage();
+
+ const [, setValue] = createPersistedSignal({ count: 0 }, { ...options, storage });
+ setValue({ count: 3 });
+
+ expect(storage.getStorage()).to.eql({ 'test-key': 'count=3' });
+
+ const [getRestored] = createPersistedSignal({ count: 0 }, { ...options, storage });
+ expect(getRestored()).to.eql({ count: 3 });
+ });
+ });
+ });
+});
diff --git a/apps/papra-client/src/modules/shared/signals/persistence/persistence.signals.ts b/apps/papra-client/src/modules/shared/signals/persistence/persistence.signals.ts
new file mode 100644
index 00000000..1ad3c55d
--- /dev/null
+++ b/apps/papra-client/src/modules/shared/signals/persistence/persistence.signals.ts
@@ -0,0 +1,52 @@
+import type { Setter } from 'solid-js';
+import { createSignal } from 'solid-js';
+
+export function createPersistedSignal<T>(
+ initialValue: T,
+ {
+ key,
+ storage = window.localStorage,
+ serialize = JSON.stringify,
+ deserialize = JSON.parse,
+ }: {
+ key: string;
+ storage?: Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;
+ serialize?: (value: T) => string;
+ deserialize?: (value: string) => T;
+ },
+) {
+ const readInitialValue = (): T => {
+ const storedValue = storage.getItem(key);
+
+ if (storedValue === null) {
+ return initialValue;
+ }
+
+ try {
+ return deserialize(storedValue);
+ } catch {
+ return initialValue;
+ }
+ };
+
+ const persist = (value: T) => {
+ try {
+ if (value == null) {
+ storage.removeItem(key);
+ return;
+ }
+
+ storage.setItem(key, serialize(value));
+ } catch {}
+ };
+
+ const [getValue, setValue] = createSignal<T>(readInitialValue());
+
+ const setAndPersist = ((value?: unknown) => {
+ const newValue = (setValue as (value?: unknown) => T)(value);
+ persist(newValue);
+ return newValue;
+ }) as Setter<T>;
+
+ return [getValue, setAndPersist] as const;
+}
diff --git a/apps/papra-client/src/modules/subscriptions/components/usage-warning-card.tsx b/apps/papra-client/src/modules/subscriptions/components/usage-warning-card.tsx
index 4c106665..19c70b0e 100644
--- a/apps/papra-client/src/modules/subscriptions/components/usage-warning-card.tsx
+++ b/apps/papra-client/src/modules/subscriptions/components/usage-warning-card.tsx
@@ -1,8 +1,8 @@
import type { Component } from 'solid-js';
-import { makePersisted } from '@solid-primitives/storage';
import { useQuery } from '@tanstack/solid-query';
-import { createSignal, Show } from 'solid-js';
+import { Show } from 'solid-js';
import { useI18n } from '@/modules/i18n/i18n.provider';
+import { createPersistedSignal } from '@/modules/shared/signals/persistence/persistence.signals';
import { Button } from '@/modules/ui/components/button';
import { fetchOrganizationUsage } from '../subscriptions.services';
import { UpgradeDialog } from './upgrade-dialog.component';
@@ -13,8 +13,8 @@ export const UsageWarningCard: Component<{ organizationId: string }> = (props) =
const { t } = useI18n();
const getOrganizationId = () => props.organizationId;
// TODO: mutualize the creation of the storage key
- const [getDismissedDate, setDismissedDate] = makePersisted(createSignal<number | null>(null), {
- name: `papra:${getOrganizationId()}:usage-warning-dismissed`,
+ const [getDismissedDate, setDismissedDate] = createPersistedSignal<number | null>(null, {
+ key: `papra:${getOrganizationId()}:usage-warning-dismissed`,
storage: localStorage,
});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index c8ebcecc..5e30b82a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -158,7 +158,7 @@ importers:
dependencies:
'@better-auth/expo':
specifier: 'catalog:'
'@corentinth/chisels':
specifier: 'catalog:'
version: 2.1.0
@@ -325,9 +325,6 @@ importers:
'@pdfslick/solid':
specifier: ^2.3.0
- '@solid-primitives/storage':
- specifier: ^4.3.2
- version: 4.3.4([email protected])
'@solidjs/router':
specifier: ^0.14.10
version: 0.14.10([email protected])
@@ -2722,7 +2719,7 @@ packages:
resolution: {integrity: sha512-+Lla7nYSiHZirgK+U/uYzsLv/X+HaJienbD5AKX1UQZHYfWaP+9uuQluRB4GrEVWF0GZ7vEVp/jzaOT9k/SQlg==}
- engines: {node: '>=0.10.0'}
+ engines: {'0': node >=0.10.0}
resolution: {integrity: sha512-hY/OdRaJMs5WsVPuVSZ+RLH3VObJmL/pv5CGCHEZHN2PxZjSZSdctyKV8UcFBXTF0yIKNAJ9XLs1dlNYXHh4Cw==}
@@ -4578,18 +4575,6 @@ packages:
peerDependencies:
solid-js: ^1.6.12
- '@solid-primitives/[email protected]':
- resolution: {integrity: sha512-GSxPAIuyxhJWOcv7n10iv3aid5oHN3KUgyA9IV0GYWlPpgyGs43aS9E85b0VXDLoH+D4ThNK8+2WEJ8B/S6Ccg==}
- peerDependencies:
- '@tauri-apps/plugin-store': '*'
- solid-js: ^1.6.12
- solid-start: '*'
- peerDependenciesMeta:
- '@tauri-apps/plugin-store':
- optional: true
- solid-start:
- optional: true
-
'@solid-primitives/[email protected]':
resolution: {integrity: sha512-00BbAiXV66WwjHuKZc3wr0+GLb9C24mMUmi3JdTpNFgHBbrQGrIHubmZDg36c5/7wH+E0GQtOOanwQS063PO+A==}
peerDependencies:
@@ -11857,37 +11842,18 @@ snapshots:
'@bcoe/[email protected]': {}
- dependencies:
- '@better-auth/utils': 0.4.0
- '@better-fetch/fetch': 1.1.21
- '@opentelemetry/semantic-conventions': 1.41.1
- '@standard-schema/spec': 1.1.0
- better-call: 1.3.5([email protected])
- jose: 6.2.3
- kysely: 0.28.17
- nanostores: 1.3.0
- zod: 4.4.3
-
dependencies:
'@better-auth/utils': 0.4.0
'@better-fetch/fetch': 1.1.21
'@opentelemetry/semantic-conventions': 1.41.1
'@standard-schema/spec': 1.1.0
- better-call: 1.3.5([email protected])
+ better-call: 1.3.5([email protected])
jose: 6.2.3
kysely: 0.28.17
nanostores: 1.3.0
zod: 4.4.3
- dependencies:
- '@better-auth/utils': 0.4.0
- optionalDependencies:
- drizzle-orm: 0.45.2(@libsql/[email protected])([email protected])
-
dependencies:
@@ -11895,9 +11861,9 @@ snapshots:
optionalDependencies:
drizzle-orm: 0.45.2(@libsql/[email protected])([email protected])
dependencies:
'@better-fetch/fetch': 1.1.21
better-call: 1.3.5([email protected])
@@ -11921,13 +11887,6 @@ snapshots:
expo-network: 8.0.8([email protected])([email protected])
- dependencies:
- '@better-auth/utils': 0.4.0
- optionalDependencies:
- kysely: 0.28.17
-
dependencies:
@@ -11935,42 +11894,21 @@ snapshots:
optionalDependencies:
kysely: 0.28.17
- dependencies:
- '@better-auth/utils': 0.4.0
-
dependencies:
'@better-auth/utils': 0.4.0
- dependencies:
- '@better-auth/utils': 0.4.0
-
dependencies:
'@better-auth/utils': 0.4.0
- dependencies:
- '@better-auth/utils': 0.4.0
-
dependencies:
'@better-auth/utils': 0.4.0
- dependencies:
- '@better-auth/utils': 0.4.0
- '@better-fetch/fetch': 1.1.21
-
dependencies:
@@ -14813,11 +14751,6 @@ snapshots:
'@solid-primitives/utils': 6.4.0([email protected])
solid-js: 1.9.13
- '@solid-primitives/[email protected]([email protected])':
- dependencies:
- '@solid-primitives/utils': 6.4.0([email protected])
- solid-js: 1.9.13
-
'@solid-primitives/[email protected]([email protected])':
dependencies:
'@solid-primitives/utils': 6.3.2([email protected])
@@ -16101,18 +16034,18 @@ snapshots:
dependencies:
- '@better-auth/memory-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
- '@better-auth/mongo-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
- '@better-auth/prisma-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
- '@better-auth/telemetry': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])(@better-fetch/[email protected])
+ '@better-auth/memory-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
+ '@better-auth/mongo-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
+ '@better-auth/prisma-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
+ '@better-auth/telemetry': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])(@better-fetch/[email protected])
'@better-auth/utils': 0.4.0
'@better-fetch/fetch': 1.1.21
'@noble/ciphers': 2.2.0
'@noble/hashes': 2.0.1
- better-call: 1.3.5([email protected])
+ better-call: 1.3.5([email protected])
defu: 6.1.4
jose: 6.2.3
kysely: 0.28.17