# Adding New Tools & Components This guide covers creating new networking tools and components for the app. The process involves creating a Svelte component, adding it to the navigation, and integrating it with the app's systems. ## Tool Structure Overview Tools in the app follow a consistent structure: 1. **Utility Functions** (`src/lib/utils/`) - Core calculation logic 2. **Tool Component** (`src/lib/components/tools/`) - UI and interaction 3. **Page Component** (`src/routes/section/tool-name/+page.svelte`) - Route wrapper 4. **Navigation Entry** (`src/lib/constants/nav.ts`) - Menu integration 5. **Tests** (`tests/unit/` and `tests/e2e/`) - Quality assurance ## Step-by-Step Guide ### 1. Create Utility Functions Start by implementing the core business logic in `src/lib/utils/`: ```typescript // src/lib/utils/my-network-tool.ts export interface MyToolResult { input: string; output: string; details: string[]; } export function calculateMyTool(input: string): MyToolResult { // Input validation if (!input || input.trim() === '') { throw new Error('Input is required'); } // Core calculation logic const result = performCalculation(input); return { input: input.trim(), output: result, details: getCalculationDetails(result) }; } function performCalculation(input: string): string { // Your calculation logic here return processedInput; } ``` ### 2. Create Tool Component Implement the UI component in `src/lib/components/tools/`: ```svelte

My Network Tool

Description of what this tool does and how to use it.

{#if error}
{error}
{/if}
{#if result}

Results

Output: {result.output}
{#if result.details.length > 0}

Details:

    {#each result.details as detail}
  • {detail}
  • {/each}
{/if}
{/if}
``` ### 3. Create Page Route Create the route file that wraps your component: ```svelte ``` ### 4. Add to Navigation Update the navigation configuration in [`src/lib/constants/nav.ts`](https://github.com/Lissy93/networking-toolbox/blob/main/src/lib/constants/nav.ts): ```typescript // Add to the appropriate section in SUB_NAV { href: '/networking/my-tool', label: 'My Network Tool', description: 'Brief description of what the tool does', keywords: ['keyword1', 'keyword2', 'calculation', 'networking'], icon: 'calculator' // Choose from icon-map.ts } ``` ### 5. Add Icon (if needed) If you need a custom icon, add it to [`src/lib/constants/icon-map.ts`](https://github.com/Lissy93/networking-toolbox/blob/main/src/lib/constants/icon-map.ts): ```typescript export const iconMap: Record = { // ... existing icons 'my-tool-icon': ` `, }; ``` ### 6. Write Tests Create unit tests for your utility functions: ```typescript // tests/unit/utils/my-network-tool.test.ts import { describe, test, expect } from 'vitest'; import { calculateMyTool } from '$lib/utils/my-network-tool'; describe('My Network Tool', () => { test('should calculate correctly with valid input', () => { const result = calculateMyTool('valid-input'); expect(result.input).toBe('valid-input'); expect(result.output).toBeDefined(); expect(result.details).toBeInstanceOf(Array); }); test('should throw error with invalid input', () => { expect(() => calculateMyTool('')).toThrow('Input is required'); expect(() => calculateMyTool('invalid')).toThrow(); }); test('should handle edge cases', () => { // Test boundary conditions const result = calculateMyTool('edge-case-input'); expect(result.output).toBe('expected-output'); }); }); ``` Add E2E tests for the complete user flow: ```typescript // tests/e2e/tools/my-network-tool.spec.ts import { test, expect } from '@playwright/test'; test.describe('My Network Tool', () => { test('should calculate results when valid input provided', async ({ page }) => { await page.goto('/networking/my-tool'); // Fill input await page.fill('input[id="tool-input"]', 'test-input'); // Verify results appear await expect(page.locator('.results-section')).toBeVisible(); await expect(page.locator('.result-item .value')).toContainText('expected-output'); }); test('should show error for invalid input', async ({ page }) => { await page.goto('/networking/my-tool'); await page.fill('input[id="tool-input"]', 'invalid-input'); await expect(page.locator('.error-message')).toBeVisible(); }); test('should copy results to clipboard', async ({ page }) => { await page.goto('/networking/my-tool'); await page.fill('input[id="tool-input"]', 'valid-input'); await page.click('.copy-btn'); // Verify copy success (check icon change) await expect(page.locator('.copy-btn svg')).toContainText('check'); }); }); ``` ## Component Patterns & Best Practices ### State Management Use Svelte 5's `$state` for reactive state: ```typescript // Good: Reactive state let input = $state(''); let result = $state(null); let loading = $state(false); // Good: Reactive calculations $effect(() => { if (input) { loading = true; try { result = calculateSomething(input); } finally { loading = false; } } }); ``` ### Input Validation Implement consistent input validation: ```typescript // Good: Comprehensive validation function validateInput(input: string): ValidationResult { if (!input || input.trim() === '') { return { valid: false, error: 'Input is required' }; } if (input.length > 1000) { return { valid: false, error: 'Input too long' }; } if (!inputPattern.test(input)) { return { valid: false, error: 'Invalid format' }; } return { valid: true }; } ``` ### Error Handling Provide meaningful error messages: ```typescript // Good: Specific error messages try { const result = complexCalculation(input); return result; } catch (error) { if (error instanceof ValidationError) { throw new Error(`Invalid input: ${error.message}`); } else if (error instanceof CalculationError) { throw new Error(`Calculation failed: ${error.details}`); } else { throw new Error('An unexpected error occurred'); } } ``` ### Accessibility Ensure your tools are accessible: ```svelte
Enter a CIDR notation like 192.168.1.0/24
{#if error} {/if}
``` ### Performance Optimize for large calculations: ```typescript // Good: Debounced calculations let debounceTimer: number; $effect(() => { clearTimeout(debounceTimer); if (input) { debounceTimer = setTimeout(() => { performExpensiveCalculation(input); }, 300); } }); ``` ## Integration Checklist Before submitting your new tool: - [ ] **Utility functions** tested with 85%+ coverage - [ ] **Component** follows Svelte 5 patterns - [ ] **Navigation** entry added with appropriate keywords - [ ] **Icon** available (existing or custom) - [ ] **E2E tests** cover main user flows - [ ] **Error handling** provides helpful messages - [ ] **Accessibility** features implemented - [ ] **Documentation** includes usage examples - [ ] **Performance** optimized for typical inputs ## Common Patterns ### IP Address Tools ```typescript // Pattern for IP-related tools export function validateIPAddress(ip: string): boolean { const ipv4Pattern = /^(\d{1,3}\.){3}\d{1,3}$/; const ipv6Pattern = /^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$/; return ipv4Pattern.test(ip) || ipv6Pattern.test(ip); } ``` ### CIDR Tools ```typescript // Pattern for CIDR-related tools export function parseCIDR(cidr: string): { ip: string; prefix: number } { const [ip, prefixStr] = cidr.split('/'); const prefix = parseInt(prefixStr, 10); if (!validateIPAddress(ip) || prefix < 0 || prefix > 32) { throw new Error('Invalid CIDR notation'); } return { ip, prefix }; } ``` ### DNS Tools ```typescript // Pattern for DNS-related tools export function validateDomain(domain: string): boolean { const domainPattern = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; return domainPattern.test(domain) && domain.length <= 253; } ``` Your new tool will automatically integrate with: - **Global search** - Keywords make it discoverable - **Bookmarks system** - Users can save favourite tools - **Accessibility** - CSS variables and ARIA support - **Offline mode** - Service worker caching - **Theme system** - CSS variables adapt to all themes IPv6 addresses can contain up to 39 characters in their compressed form. Consider this when designing input validation for IPv6-related tools.