-
Notifications
You must be signed in to change notification settings - Fork 7
Add cachedWritableStores to shared lib, and update hideZeroBalanceVaults to use the cached version #1698
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
Merged
+318
−227
Merged
Add cachedWritableStores to shared lib, and update hideZeroBalanceVaults to use the cached version #1698
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
45ac91b
move cachedwritablestore to ui-comp
hardingjam ada479e
index
hardingjam 2422c7b
hide zero bal vaults
hardingjam 157a963
Move cachedWritableStore to shared lib, test hideZeroBalanceVaults
hardingjam acf5f5c
add
hardingjam 887a313
go
hardingjam b4201c6
replace
hardingjam f598c2a
store
hardingjam a4d0f39
add
hardingjam fce4eed
update
hardingjam a98fae7
format
hardingjam f0f6e4e
do nothing
hardingjam b1fca38
Merge branch 'main' into Cache-stores-in-webapp
hardingjam b30e576
update
hardingjam 5101234
rm err
hardingjam 0519341
add jsdoc
hardingjam bb464c7
format
hardingjam 587eb54
Merge branch 'main' into Cache-stores-in-webapp
hardingjam aa225b2
remove from taur-app
hardingjam 6b44948
cachedWritableOptionalStore
hardingjam afcbd4b
formatted
hardingjam 6704f25
no change
hardingjam 0926719
type safing
hardingjam 41b32d0
Merge branch 'main' into Cache-stores-in-webapp
hardingjam 9005e8f
Merge branch 'main' into Cache-stores-in-webapp
hardyjosh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
packages/ui-components/src/lib/storesGeneric/cachedWritableStore.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { writable } from 'svelte/store'; | ||
|
||
/** | ||
* Creates a writable Svelte store that persists its value to localStorage. | ||
* | ||
* @template T - The type of the value stored in the store | ||
* @param {string} key - The localStorage key used to store the value | ||
* @param {T} defaultValue - The default value to use when no value is found in localStorage | ||
* @param {function(T): string} serialize - Function to convert the store value to a string for storage | ||
* @param {function(string): T} deserialize - Function to convert the stored string back to the original type | ||
* @returns {import('svelte/store').Writable<T>} A writable store that automatically syncs with localStorage | ||
* | ||
* @example | ||
* // Create a store for a boolean value | ||
* const darkMode = cachedWritableStore( | ||
* 'darkMode', | ||
* false, | ||
* value => JSON.stringify(value), | ||
* str => JSON.parse(str) | ||
* ); | ||
* | ||
* // Create a store for a complex object | ||
* const userPreferences = cachedWritableStore( | ||
* 'userPrefs', | ||
* { theme: 'light', fontSize: 14 }, | ||
* value => JSON.stringify(value), | ||
* str => JSON.parse(str) | ||
* ); | ||
*/ | ||
export function cachedWritableStore<T>( | ||
key: string, | ||
defaultValue: T, | ||
serialize: (value: T) => string, | ||
deserialize: (serialized: string) => T | ||
) { | ||
const getCache = () => { | ||
try { | ||
const cached = localStorage.getItem(key); | ||
return cached !== null ? deserialize(cached) : defaultValue; | ||
} catch { | ||
return defaultValue; | ||
} | ||
}; | ||
const setCache = (value?: T) => { | ||
try { | ||
if (value !== undefined) { | ||
localStorage.setItem(key, serialize(value)); | ||
} else { | ||
localStorage.removeItem(key); | ||
} | ||
} catch { | ||
// Silently ignore localStorage errors to allow the application to function | ||
// without persistence in environments where localStorage is unavailable | ||
} | ||
}; | ||
|
||
const data = writable<T>(getCache()); | ||
|
||
data.subscribe((value) => { | ||
setCache(value); | ||
}); | ||
|
||
return data; | ||
} | ||
|
||
export const cachedWritableString = (key: string, defaultValue = '') => | ||
cachedWritableStore<string>( | ||
key, | ||
defaultValue, | ||
(v) => v, | ||
(v) => v | ||
); | ||
export const cachedWritableInt = (key: string, defaultValue = 0) => | ||
cachedWritableStore<number>( | ||
key, | ||
defaultValue, | ||
(v) => v.toString(), | ||
(v) => { | ||
const parsed = Number.parseInt(v); | ||
return isNaN(parsed) ? defaultValue : parsed; | ||
} | ||
); | ||
/** | ||
* Creates a writable store that can hold an optional value of type T and persists to localStorage. | ||
* | ||
* @template T - The type of the value stored | ||
* @param {string} key - The localStorage key to use for persistence | ||
* @param {T | undefined} defaultValue - The default value if nothing is found in localStorage | ||
* @param {function} serialize - Function to convert the value to a string for storage | ||
* @param {function} deserialize - Function to convert the stored string back to a value | ||
* @returns A writable store that persists to localStorage and can hold undefined values | ||
*/ | ||
export const cachedWritableOptionalStore = <T>( | ||
key: string, | ||
defaultValue: T | undefined = undefined, | ||
serialize: (value: T) => string, | ||
deserialize: (serialized: string) => T | ||
) => | ||
cachedWritableStore<T | undefined>( | ||
key, | ||
defaultValue, | ||
(v) => (v !== undefined ? serialize(v) : ''), | ||
(v) => (v !== '' ? deserialize(v) : undefined) | ||
); | ||
|
||
/** | ||
* Creates a writable store that can hold an optional number value and persists to localStorage. | ||
* | ||
* @param {string} key - The localStorage key to use for persistence | ||
* @param {number | undefined} defaultValue - The default value if nothing is found in localStorage | ||
* @returns A writable store that persists to localStorage and can hold an optional number | ||
*/ | ||
export const cachedWritableIntOptional = (key: string, defaultValue = undefined) => | ||
cachedWritableOptionalStore<number>( | ||
key, | ||
defaultValue, | ||
(v) => v.toString(), | ||
(v) => { | ||
const parsed = Number.parseInt(v); | ||
return isNaN(parsed) ? (defaultValue ?? 0) : parsed; | ||
} | ||
); | ||
|
||
/** | ||
* Creates a writable store that can hold an optional string value and persists to localStorage. | ||
* | ||
* @param {string} key - The localStorage key to use for persistence | ||
* @param {string | undefined} defaultValue - The default value if nothing is found in localStorage | ||
* @returns A writable store that persists to localStorage and can hold an optional string | ||
*/ | ||
export const cachedWritableStringOptional = (key: string, defaultValue = undefined) => | ||
cachedWritableOptionalStore<string>( | ||
key, | ||
defaultValue, | ||
(v) => v, | ||
(v) => v | ||
); | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { cachedWritableStore } from '@rainlanguage/ui-components'; | ||
|
||
/** | ||
* A persistent store that controls whether vaults with zero balance should be hidden in the UI. | ||
* | ||
* This setting is saved to local storage and persists between sessions. | ||
* | ||
* @default true - Zero balance vaults are hidden by default | ||
* @returns A writable store containing a boolean value | ||
*/ | ||
export const hideZeroBalanceVaults = cachedWritableStore<boolean>( | ||
'settings.hideZeroBalanceVaults', | ||
true, // default value is true | ||
(value) => JSON.stringify(value), | ||
(str) => { | ||
try { | ||
const value = JSON.parse(str); | ||
return typeof value === 'boolean' ? value : true; | ||
} catch { | ||
return true; | ||
} | ||
} | ||
); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { ConfigSource } from '@rainlanguage/orderbook'; | ||
|
||
export const mockConfigSource: ConfigSource = { | ||
networks: { | ||
mainnet: { | ||
rpc: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID', | ||
'chain-id': 1, | ||
label: 'Ethereum Mainnet', | ||
currency: 'ETH', | ||
}, | ||
}, | ||
subgraphs: { | ||
mainnet: 'https://api.thegraph.com/subgraphs/name/mainnet', | ||
}, | ||
orderbooks: { | ||
orderbook1: { | ||
address: '0xOrderbookAddress1', | ||
network: 'mainnet', | ||
subgraph: 'uniswap', | ||
label: 'Orderbook 1', | ||
}, | ||
}, | ||
deployers: { | ||
deployer1: { | ||
address: '0xDeployerAddress1', | ||
network: 'mainnet', | ||
label: 'Deployer 1', | ||
}, | ||
}, | ||
metaboards: { | ||
metaboard1: 'https://example.com/metaboard1', | ||
}, | ||
accounts: { | ||
name_one: 'address_one', | ||
name_two: 'address_two', | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.