@c-rex/components 0.3.0-build.40 → 0.3.0-build.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -1
- package/src/autocomplete.tsx +20 -14
- package/src/documents/result-list-item.tsx +2 -1
- package/src/favorites/bookmark-button.tsx +20 -1
- package/src/favorites/favorite-button.tsx +11 -2
- package/src/favorites/favorites-context.tsx +4 -0
- package/src/footer/social-links-block.tsx +1 -0
- package/src/generated/create-client-request.tsx +0 -7
- package/src/info/information-unit-authors-grid-client.tsx +183 -0
- package/src/info/information-unit-metadata-grid-client.tsx +12 -2
- package/src/navbar/navbar.tsx +0 -2
- package/src/restriction-menu/__tests__/restriction-menu-logic.test.ts +148 -0
- package/src/restriction-menu/restriction-hierarchy.ts +1 -1
- package/src/restriction-menu/restriction-menu-item.tsx +13 -7
- package/src/restriction-menu/restriction-menu-logic.ts +51 -0
- package/src/restriction-menu/taxonomy-restriction-menu.tsx +21 -10
- package/src/results/filter-navbar.tsx +2 -1
- package/src/results/filter-sidebar/index.tsx +65 -49
- package/src/results/generic/search-results-client.tsx +1 -1
- package/src/results/information-unit-search-results-card-list.tsx +2 -1
- package/src/results/information-unit-search-results-cards.tsx +5 -4
- package/src/results/information-unit-search-results-table.tsx +6 -4
- package/src/results/pagination.tsx +3 -1
- package/src/stores/search-settings-store.ts +0 -4
- package/src/stores/ui-preferences-store.ts +128 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { create } from "zustand";
|
|
2
|
+
import { persist, createJSONStorage, StateStorage } from "zustand/middleware";
|
|
3
|
+
|
|
4
|
+
const cookieStorage: StateStorage = {
|
|
5
|
+
getItem: (name: string): string | null => {
|
|
6
|
+
if (typeof document === "undefined") return null;
|
|
7
|
+
const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));
|
|
8
|
+
return match ? decodeURIComponent(match[2] as string) : null;
|
|
9
|
+
},
|
|
10
|
+
setItem: (name: string, value: string): void => {
|
|
11
|
+
if (typeof document === "undefined") return;
|
|
12
|
+
const maxAge = 60 * 60 * 24 * 365;
|
|
13
|
+
document.cookie = `${name}=${encodeURIComponent(value)};path=/;max-age=${maxAge};SameSite=Lax`;
|
|
14
|
+
},
|
|
15
|
+
removeItem: (name: string): void => {
|
|
16
|
+
if (typeof document === "undefined") return;
|
|
17
|
+
document.cookie = `${name}=;path=/;max-age=0`;
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type UiPreferencesState = {
|
|
22
|
+
facetExcludeProperties: string[];
|
|
23
|
+
metadataExcludeProperties: string[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type UiPreferencesStore = UiPreferencesState & {
|
|
27
|
+
updatePreferences: (prefs: Partial<UiPreferencesState>) => void;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const defaultUiPreferences: UiPreferencesState = {
|
|
31
|
+
facetExcludeProperties: [],
|
|
32
|
+
metadataExcludeProperties: [],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function readLegacyValues(): Partial<UiPreferencesState> {
|
|
36
|
+
if (typeof document === "undefined") return {};
|
|
37
|
+
try {
|
|
38
|
+
const match = document.cookie.match(new RegExp(`(^| )c-rex-search-settings=([^;]+)`));
|
|
39
|
+
if (!match) return {};
|
|
40
|
+
const parsed = JSON.parse(decodeURIComponent(match[2] as string));
|
|
41
|
+
const state = parsed?.state || {};
|
|
42
|
+
const result: Partial<UiPreferencesState> = {};
|
|
43
|
+
if (Array.isArray(state.facetExcludeProperties)) {
|
|
44
|
+
result.facetExcludeProperties = state.facetExcludeProperties;
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(state.metadataExcludeProperties)) {
|
|
47
|
+
result.metadataExcludeProperties = state.metadataExcludeProperties;
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
} catch {
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const useUiPreferencesStore = create<UiPreferencesStore>()(
|
|
56
|
+
persist(
|
|
57
|
+
(set, get) => ({
|
|
58
|
+
...defaultUiPreferences,
|
|
59
|
+
updatePreferences: (prefs) => set((state) => ({ ...state, ...prefs })),
|
|
60
|
+
}),
|
|
61
|
+
{
|
|
62
|
+
name: "c-rex-ui-preferences",
|
|
63
|
+
storage: createJSONStorage(() => cookieStorage),
|
|
64
|
+
onRehydrateStorage: () => (state) => {
|
|
65
|
+
if (!state) return;
|
|
66
|
+
const isNew =
|
|
67
|
+
state.facetExcludeProperties.length === 0 &&
|
|
68
|
+
state.metadataExcludeProperties.length === 0;
|
|
69
|
+
if (isNew) {
|
|
70
|
+
const legacy = readLegacyValues();
|
|
71
|
+
if (Object.keys(legacy).length > 0) {
|
|
72
|
+
state.updatePreferences(legacy);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Read UI preferences from cookie on the server.
|
|
82
|
+
* Falls back to legacy `c-rex-search-settings` cookie for migration.
|
|
83
|
+
*/
|
|
84
|
+
export function getUiPreferencesFromCookie(
|
|
85
|
+
newCookieValue: string | undefined,
|
|
86
|
+
legacyCookieValue?: string | undefined
|
|
87
|
+
): UiPreferencesState {
|
|
88
|
+
if (newCookieValue) {
|
|
89
|
+
try {
|
|
90
|
+
const parsed = JSON.parse(newCookieValue);
|
|
91
|
+
const state = parsed?.state || {};
|
|
92
|
+
const result: UiPreferencesState = { ...defaultUiPreferences };
|
|
93
|
+
if (Array.isArray(state.facetExcludeProperties)) {
|
|
94
|
+
result.facetExcludeProperties = state.facetExcludeProperties;
|
|
95
|
+
}
|
|
96
|
+
if (Array.isArray(state.metadataExcludeProperties)) {
|
|
97
|
+
result.metadataExcludeProperties = state.metadataExcludeProperties;
|
|
98
|
+
}
|
|
99
|
+
if (
|
|
100
|
+
result.facetExcludeProperties.length === 0 &&
|
|
101
|
+
result.metadataExcludeProperties.length === 0 &&
|
|
102
|
+
legacyCookieValue
|
|
103
|
+
) {
|
|
104
|
+
return migrateFromLegacy(legacyCookieValue);
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
} catch {
|
|
108
|
+
// fall through to legacy
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (legacyCookieValue) {
|
|
112
|
+
return migrateFromLegacy(legacyCookieValue);
|
|
113
|
+
}
|
|
114
|
+
return { ...defaultUiPreferences };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function migrateFromLegacy(cookieValue: string): UiPreferencesState {
|
|
118
|
+
try {
|
|
119
|
+
const parsed = JSON.parse(cookieValue);
|
|
120
|
+
const state = parsed?.state || {};
|
|
121
|
+
return {
|
|
122
|
+
facetExcludeProperties: Array.isArray(state.facetExcludeProperties) ? state.facetExcludeProperties : [],
|
|
123
|
+
metadataExcludeProperties: Array.isArray(state.metadataExcludeProperties) ? state.metadataExcludeProperties : [],
|
|
124
|
+
};
|
|
125
|
+
} catch {
|
|
126
|
+
return { ...defaultUiPreferences };
|
|
127
|
+
}
|
|
128
|
+
}
|