Implemented settings backup/restore

This commit is contained in:
Alicia Sykes committed 2025-10-24 07:58:57 +01:00
1 parent 0be304b9f1
commit bb0058a148
6 files changed
+231 -14

No files matched your search

+2 -2
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import { site } from '$lib/constants/site';
import { SITE_ICON } from '$lib/config/customizable-settings';
import { SITE_ICON, SITE_DESCRIPTION } from '$lib/config/customizable-settings';
import Icon from '$lib/components/global/Icon.svelte';
import GlobalSearch from '$lib/components/global/GlobalSearch.svelte';
import BurgerMenu from '$lib/components/furniture/BurgerMenu.svelte';
@@ -31,7 +31,7 @@
</div>
<div>
<h1><a href="/">{site.title}</a></h1>
<p class="subtitle">The sysadmin's Swiss Army knife</p>
<p class="subtitle">{SITE_DESCRIPTION || "The sysadmin's Swiss Army knife"}</p>
</div>
</div>
@@ -11,6 +11,8 @@
import { primaryColor } from '$lib/stores/primaryColor';
import { fontScale, fontScaleOptions, type FontScaleLevel } from '$lib/stores/fontScale';
import { storage } from '$lib/utils/localStorage';
import * as config from '$lib/config/customizable-settings';
import SegmentedControl from '$lib/components/global/SegmentedControl.svelte';
interface Props {
standalone?: boolean;
@@ -46,9 +48,12 @@
let siteCustomizationErrors = $state<string[]>([]);
let lastStoreValue = $state('');
let lastColorStoreValue = $state('');
let envVarsCopied = $state(false);
let envFormat = $state<'env' | 'docker'>('env');
let showExportSettings = $state(false);
// Constants
const PRIMARY_A11Y_OPTIONS = ['reduce-motion'];
const PRIMARY_A11Y_OPTIONS: string[] = [];
const COLOR_PALETTE = [
'#1a75ff',
'#7711ff',
@@ -78,6 +83,9 @@
const primaryOptions = $derived(
$accessibilitySettings.options.filter((opt) => PRIMARY_A11Y_OPTIONS.includes(opt.id)),
);
// const primaryOptions = $derived(
// $accessibilitySettings.options.filter((opt) => PRIMARY_A11Y_OPTIONS.includes(opt.id)),
// );
const additionalOptions = $derived(
$accessibilitySettings.options.filter((opt) => !PRIMARY_A11Y_OPTIONS.includes(opt.id)),
);
@@ -174,7 +182,44 @@
window.location.reload();
}
},
copyEnvVars: async () => {
try {
await navigator.clipboard.writeText(envVarsString);
envVarsCopied = true;
setTimeout(() => (envVarsCopied = false), 2000);
} catch {
const textArea = document.createElement('textarea');
textArea.value = envVarsString;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
envVarsCopied = true;
setTimeout(() => (envVarsCopied = false), 2000);
}
},
};
// Live-updating env vars string - reacts to store changes
const envVarsString = $derived.by(() => {
// Trigger reactivity on store changes
void $currentTheme;
void $currentFontScale;
void $currentHomepageLayout;
void $currentNavbarDisplay;
void $primaryColor;
void $siteCustomization;
const envVars = config.getUserSettingsList();
if (envFormat === 'docker') {
const filtered = envVars.filter(({ value }) => value !== '');
return 'environment:\n' + filtered.map(({ name, value }) => ` - ${name}=${value}`).join('\n');
}
return envVars.map(({ name, value }) => `${name}='${value}'`).join('\n');
});
</script>
{#snippet validationMessages(errors: string[], warnings: string[])}
@@ -515,6 +560,55 @@
</div>
{/if}
<!-- Docs for saving settings -->
{#if standalone}
<div class="settings-section saving-section">
<h3>Syncing Settings and Backup/Restore</h3>
<p class="line-1">
Your settings are saved in your browser's local storage, and so they will be retained even after you quit the
app.
</p>
<p>
Since we don't require login/signup to use the app, there is currently no way to automatically sync your
settings across devices. But if you're self-hosting Networking Toolbox, you can apply settings in your config,
by including the following environment variables. This way, you're settings will be applied to all users across
all devices.
</p>
<button
class="show-more-btn"
onclick={() => (showExportSettings = !showExportSettings)}
aria-expanded={showExportSettings}
>
<Icon name={showExportSettings ? 'chevron-up' : 'chevron-down'} size="sm" />
<span>Export Settings</span>
</button>
{#if showExportSettings}
<div class="env-vars-section" transition:slide={{ duration: 300 }}>
<div class="env-header">
<h4>Environment Variables</h4>
<SegmentedControl
options={[
{ value: 'env', label: '.env' },
{ value: 'docker', label: 'docker-compose' },
]}
bind:value={envFormat}
/>
</div>
<p class="section-description">
Current environment variable values. Copy and paste into your config file for self-hosted instances.
</p>
<pre class="env-block"><code>{envVarsString}</code></pre>
<button class="action-btn apply" onclick={handlers.copyEnvVars}>
<Icon name={envVarsCopied ? 'check' : 'copy'} size="sm" />
{envVarsCopied ? 'Copied!' : 'Copy to Clipboard'}
</button>
</div>
{/if}
</div>
{/if}
<!-- Navigation Links (only in dropdown mode) -->
{#if !standalone}
<div class="settings-section settings-links">
@@ -595,6 +689,14 @@
&.info-more-section {
grid-column: span 2;
}
&.saving-section {
grid-column: 1/-1;
.line-1 {
color: var(--text-tertiary);
font-size: var(--font-size-md);
font-style: italic;
}
}
.theme-option {
flex-direction: column;
@@ -611,6 +713,17 @@
grid-column: span 1;
}
}
@media (max-width: 990px) {
&.site-branding-section {
grid-column: span 1;
}
&.delete-section {
grid-column: 1 / -1;
}
&.info-more-section {
grid-column: 1 / -1;
}
}
}
.theme-options {
@@ -990,8 +1103,8 @@
}
.show-more-btn {
width: 100%;
margin-top: var(--spacing-sm);
max-width: 16rem;
margin: var(--spacing-sm) auto;
justify-content: center;
&[aria-expanded='true'] {
@@ -1187,6 +1300,8 @@
background: var(--color-primary);
color: var(--bg-primary);
border-color: var(--color-primary);
float: right;
width: fit-content !important;
&:hover {
background: color-mix(in srgb, var(--color-primary), black 10%);
@@ -1358,6 +1473,62 @@
}
}
.saving-section {
.show-more-btn {
margin-top: var(--spacing-md);
}
.env-vars-section {
margin-top: var(--spacing-md);
padding-top: var(--spacing-md);
border-top: 1px solid var(--border-primary);
.env-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: var(--spacing-sm);
h4 {
margin: 0;
font-size: var(--font-size-md);
font-weight: 600;
color: var(--text-primary);
}
}
.section-description {
font-size: var(--font-size-sm);
color: var(--text-secondary);
margin: 0 0 var(--spacing-md) 0;
}
.env-block {
width: 100%;
padding: var(--spacing-md);
background: var(--bg-tertiary);
border: 1px solid var(--border-primary);
border-radius: var(--radius-sm);
margin-bottom: var(--spacing-md);
overflow-x: auto;
code {
font-family: 'Courier New', monospace;
font-size: var(--font-size-sm);
color: var(--text-primary);
line-height: 1.6;
white-space: pre;
padding: 0;
}
}
.action-btn {
width: 100%;
justify-content: center;
}
}
}
@keyframes slideIn {
from {
opacity: 0;
+45
View File
@@ -102,3 +102,48 @@ export const BLOCK_PRIVATE_DNS_IPS = env.NTB_BLOCK_PRIVATE_DNS_IPS !== 'false';
export const ALLOWED_DNS_SERVERS = env.NTB_ALLOWED_DNS_SERVERS
? env.NTB_ALLOWED_DNS_SERVERS.split(',').map((ip) => ip.trim())
: DEFAULT_TRUSTED_DNS_SERVERS;
/**
* Get user settings list with values prioritized as:
* 1. User-set value from localStorage
* 2. Environment variable value
* 3. Empty string
*/
export function getUserSettingsList(): Array<{ name: string; value: string }> {
if (!browser) return [];
const getLocalStorageValue = (key: string): string | null => {
try {
return localStorage.getItem(key);
} catch {
return null;
}
};
const getUserCustomValue = (key: keyof typeof userCustom): string => {
return userCustom?.[key] || '';
};
// Read actual user preferences from localStorage
const userTheme = getLocalStorageValue('theme');
const userFontScale = getLocalStorageValue('font-scale');
const userHomepageLayout = getLocalStorageValue('homepage-layout');
const userNavbarDisplay = getLocalStorageValue('navbar-display');
const userPrimaryColor = getLocalStorageValue('user-primary-color');
return [
{ name: 'NTB_SITE_TITLE', value: getUserCustomValue('title') || env.NTB_SITE_TITLE || '' },
{ name: 'NTB_SITE_DESCRIPTION', value: getUserCustomValue('description') || env.NTB_SITE_DESCRIPTION || '' },
{ name: 'NTB_SITE_ICON', value: getUserCustomValue('iconUrl') || env.NTB_SITE_ICON || '' },
{ name: 'NTB_HOMEPAGE_LAYOUT', value: userHomepageLayout || env.NTB_HOMEPAGE_LAYOUT || '' },
{ name: 'NTB_NAVBAR_DISPLAY', value: userNavbarDisplay || env.NTB_NAVBAR_DISPLAY || '' },
{ name: 'NTB_DEFAULT_THEME', value: userTheme || env.NTB_DEFAULT_THEME || '' },
{ name: 'NTB_DEFAULT_LANGUAGE', value: env.NTB_DEFAULT_LANGUAGE || '' },
{ name: 'NTB_PRIMARY_COLOR', value: userPrimaryColor || env.NTB_PRIMARY_COLOR || '' },
{ name: 'NTB_FONT_SCALE', value: userFontScale || env.NTB_FONT_SCALE || '' },
{ name: 'NTB_SHOW_TIPS_ON_HOMEPAGE', value: env.NTB_SHOW_TIPS_ON_HOMEPAGE || '' },
{ name: 'NTB_ALLOW_CUSTOM_DNS', value: env.NTB_ALLOW_CUSTOM_DNS || '' },
{ name: 'NTB_BLOCK_PRIVATE_DNS_IPS', value: env.NTB_BLOCK_PRIVATE_DNS_IPS || '' },
{ name: 'NTB_ALLOWED_DNS_SERVERS', value: env.NTB_ALLOWED_DNS_SERVERS || '' },
];
}
+1 -1
View File
@@ -3,7 +3,7 @@ import { SITE_TITLE, SITE_DESCRIPTION } from '$lib/config/customizable-settings'
export const site = {
name: SITE_TITLE || 'Networking Toolbox',
title: SITE_TITLE || 'Networking Toolbox',
description: 'A free set of online tools to help with IP addressing and subnetting.',
description: SITE_DESCRIPTION || 'A free set of online tools to help with IP addressing and subnetting.',
longDescription:
'Comprehensive IP address calculator with subnet calculations, CIDR conversion, IP format conversion, and network reference tools.',
heroDescription: SITE_DESCRIPTION || 'Your companion for all-things networking',
+8 -7
View File
@@ -24,14 +24,15 @@
<!-- Links #1: Live Demo, DockerHub, CodeBerg Mirror, Sponsor, More Apps -->
<section class="contents">
<div>
<h3>On this page</h3>
<h3>About</h3>
<ul>
<li><a href="#api">API</a></li>
<li><a href="#self-hosting">Self-Hosting</a></li>
<li><a href="#building">Developing</a></li>
<li><a href="#author">Author</a></li>
<li><a href="#more-apps">Attributions</a></li>
<li><a href="#license">License</a></li>
<li><a href="/about/api">API</a></li>
<li><a href="/about/deploying">Self-Hosting</a></li>
<li><a href="/about/building">Developing</a></li>
<li><a href="/about/support">Get Support</a></li>
<li><a href="/about/attributions">Attributions</a></li>
<li><a href="/about/author">About Author</a></li>
<li><a href="/about/legal/license">License</a></li>
</ul>
</div>
<div>
+1 -1
View File
@@ -106,7 +106,7 @@
display: flex;
align-items: center;
gap: var(--spacing-sm);
margin-bottom: var(--spacing-lg);
margin: var(--spacing-lg) 0 var(--spacing-md) 0;
svg {
color: var(--color-primary);
}