mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-03 09:51:39 +00:00
83 lines
2.0 KiB
Templ
83 lines
2.0 KiB
Templ
package storage
|
|||
|
|
|
||
|
|
var storageHandle = templ.NewOnceHandle()
|
||
|
|
|
||
|
|
// Internal script templates for storage operations
|
||
|
|
|
||
|
|
script setLocalStorage(key string, value string) {
|
||
|
|
localStorage.setItem(key, value)
|
||
|
|
}
|
||
|
|
|
||
|
|
script getLocalStorage(key string) {
|
||
|
|
return localStorage.getItem(key)
|
||
|
|
}
|
||
|
|
|
||
|
|
script removeLocalStorage(key string) {
|
||
|
|
localStorage.removeItem(key)
|
||
|
|
}
|
||
|
|
|
||
|
|
script setSessionStorage(key string, value string) {
|
||
|
|
sessionStorage.setItem(key, value)
|
||
|
|
}
|
||
|
|
|
||
|
|
script getSessionStorage(key string) {
|
||
|
|
return sessionStorage.getItem(key)
|
||
|
|
}
|
||
|
|
|
||
|
|
script removeSessionStorage(key string) {
|
||
|
|
sessionStorage.removeItem(key)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Storage component that includes all storage-related scripts
|
||
|
|
templ StorageScripts() {
|
||
|
|
@storageHandle.Once() {
|
||
|
|
<script type="text/javascript">
|
||
|
|
// Storage helper functions
|
||
|
|
function setLocalStorageItem(key, value) {
|
||
|
|
localStorage.setItem(key, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
function getLocalStorageItem(key) {
|
||
|
|
return localStorage.getItem(key);
|
||
|
|
}
|
||
|
|
|
||
|
|
function removeLocalStorageItem(key) {
|
||
|
|
localStorage.removeItem(key);
|
||
|
|
}
|
||
|
|
|
||
|
|
function setSessionStorageItem(key, value) {
|
||
|
|
sessionStorage.setItem(key, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
function getSessionStorageItem(key) {
|
||
|
|
return sessionStorage.getItem(key);
|
||
|
|
}
|
||
|
|
|
||
|
|
function removeSessionStorageItem(key) {
|
||
|
|
sessionStorage.removeItem(key);
|
||
|
|
}
|
||
|
|
</script>
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Public components for storage operations
|
||
|
|
templ SetLocalStorageItem(key string, value any) {
|
||
|
|
@StorageScripts()
|
||
|
|
<script>
|
||
|
|
(() => {
|
||
|
|
const value = { templ.JSONString(value) };
|
||
|
|
setLocalStorageItem({ key }, JSON.stringify(value));
|
||
|
|
})();
|
||
|
|
</script>
|
||
|
|
}
|
||
|
|
|
||
|
|
templ SetSessionStorageItem(key string, value any) {
|
||
|
|
@StorageScripts()
|
||
|
|
<script>
|
||
|
|
(() => {
|
||
|
|
const value = { templ.JSONString(value) };
|
||
|
|
setSessionStorageItem({ key }, JSON.stringify(value));
|
||
|
|
})();
|
||
|
|
</script>
|
||
|
|
}
|