-
-
Notifications
You must be signed in to change notification settings - Fork 29
refactor(settings): restructure settings page #292
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
Open
hiitsrob
wants to merge
8
commits into
rivenmedia:main
Choose a base branch
from
hiitsrob:settings-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e2410fe
refactor(settings): restructure settings page
hiitsrob a370fe3
refactor(settings): improve type safety, accessibility, and code quality
hiitsrob e1409f2
refactor(settings): improve docstring coverage
hiitsrob e98b840
refactor(settings): remove tests
hiitsrob 9b3522c
refactor(settings): migrate to client-side reactive store architecture
hiitsrob 8d9724a
Merge branch 'main' into settings-refactor
hiitsrob eeaa0b0
fix: address code review feedback for settings refactor
hiitsrob beedab7
fix(settings): include sectionId in error response for debugging
hiitsrob 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
Some comments aren't visible on the classic Files Changed page.
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
Large diffs are not rendered by default.
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,45 @@ | ||
| <script lang="ts"> | ||
| import * as Alert from "$lib/components/ui/alert"; | ||
| import { Button } from "$lib/components/ui/button"; | ||
| import AlertTriangle from "@lucide/svelte/icons/alert-triangle"; | ||
| import RefreshCw from "@lucide/svelte/icons/refresh-cw"; | ||
|
|
||
| interface Props { | ||
| title?: string; | ||
| message: string; | ||
| hint?: string; | ||
| onRetry?: () => void; | ||
| } | ||
|
|
||
| let { | ||
| title = "Unable to Connect", | ||
| message, | ||
| hint = "Please check that the Riven backend is running and accessible.", | ||
| onRetry | ||
| }: Props = $props(); | ||
|
|
||
| function handleRetry() { | ||
| if (onRetry) { | ||
| onRetry(); | ||
| } else { | ||
| window.location.reload(); | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <div class="flex h-full items-center justify-center p-8"> | ||
| <Alert.Root variant="destructive" class="max-w-md"> | ||
| <AlertTriangle class="h-5 w-5" /> | ||
| <Alert.Title class="text-lg">{title}</Alert.Title> | ||
| <Alert.Description class="mt-2 space-y-4"> | ||
| <p>{message}</p> | ||
| {#if hint} | ||
| <p class="text-sm opacity-80">{hint}</p> | ||
| {/if} | ||
| <Button onclick={handleRetry} variant="outline" size="sm" class="mt-2 gap-2"> | ||
| <RefreshCw class="h-4 w-4" /> | ||
| Try Again | ||
| </Button> | ||
| </Alert.Description> | ||
| </Alert.Root> | ||
| </div> |
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,138 @@ | ||
| <script lang="ts"> | ||
| import { Field, getValueSnapshot, setValue, type Schema } from "@sjsf/form"; | ||
| import * as Card from "$lib/components/ui/card"; | ||
| import { Button } from "$lib/components/ui/button"; | ||
| import { exportSettings } from "./export-settings"; | ||
| import { toast } from "svelte-sonner"; | ||
| import FileDown from "@lucide/svelte/icons/file-down"; | ||
| import CollapsibleCard from "./collapsible-card.svelte"; | ||
| import { getSchemaAtPath, getServiceFields } from "./schema-utils"; | ||
| import type { SettingsFormState, AppSettings } from "./form-defaults"; | ||
| import type { SettingsSection } from "./settings-sections"; | ||
|
|
||
| interface Props { | ||
| form: SettingsFormState; | ||
| schema: Schema; | ||
| section: SettingsSection; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars -- props passed for interface consistency | ||
| let { form, schema, section: _section }: Props = $props(); | ||
|
|
||
| type FieldPath = Parameters<typeof Field>[1]["path"]; | ||
|
|
||
| const formValue = $derived(getValueSnapshot(form) as AppSettings); | ||
|
|
||
| // Get descriptions from schema | ||
| const loggingSchema = $derived(getSchemaAtPath(schema, "logging")); | ||
| const streamSchema = $derived(getSchemaAtPath(schema, "stream")); | ||
| const indexerSchema = $derived(getSchemaAtPath(schema, "indexer")); | ||
| const subtitleSchema = $derived(getSchemaAtPath(schema, "post_processing.subtitle")); | ||
|
|
||
| // Get field lists from schema | ||
| const loggingFields = $derived(getServiceFields(schema, "logging")); | ||
| const streamFields = $derived(getServiceFields(schema, "stream")); | ||
| const indexerFields = $derived(getServiceFields(schema, "indexer")); | ||
| const subtitleFields = $derived(getServiceFields(schema, "post_processing.subtitle")); | ||
|
|
||
| // Section states | ||
| const logging = $derived(formValue?.logging); | ||
| const subtitle = $derived(formValue?.post_processing?.subtitle); | ||
|
|
||
| function toggleLogging(enabled: boolean) { | ||
| setValue(form, { | ||
| logging: { ...logging, enabled } | ||
| } as AppSettings); | ||
| } | ||
|
|
||
| function toggleSubtitle(enabled: boolean) { | ||
| setValue(form, { | ||
| post_processing: { | ||
| ...formValue?.post_processing, | ||
| subtitle: { ...subtitle, enabled } | ||
| } | ||
| } as AppSettings); | ||
| } | ||
|
|
||
| function handleExport() { | ||
| try { | ||
| const settings = getValueSnapshot(form); | ||
| const timestamp = new Date().toISOString().slice(0, 10); | ||
| exportSettings(settings, `riven-settings-${timestamp}.json`); | ||
| toast.success("Settings exported"); | ||
| } catch (error) { | ||
| console.error("Failed to export settings:", error); | ||
| toast.error("Failed to export settings"); | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <div class="space-y-4"> | ||
| <!-- File Logging --> | ||
| <CollapsibleCard | ||
| title={loggingSchema?.title || "File Logging"} | ||
| description={loggingSchema?.description} | ||
| hasToggle | ||
| enabled={logging?.enabled ?? false} | ||
| onToggle={toggleLogging} | ||
| contentClass="grid gap-4 sm:grid-cols-2"> | ||
| {#each loggingFields as fieldName (fieldName)} | ||
| <Field {form} path={["logging", fieldName] as FieldPath} /> | ||
| {/each} | ||
| </CollapsibleCard> | ||
|
|
||
| <!-- Streaming --> | ||
| <CollapsibleCard | ||
| title={streamSchema?.title || "Streaming"} | ||
| description={streamSchema?.description} | ||
| contentClass="grid gap-4 sm:grid-cols-2"> | ||
| {#each streamFields as fieldName (fieldName)} | ||
| <Field {form} path={["stream", fieldName] as FieldPath} /> | ||
| {/each} | ||
| </CollapsibleCard> | ||
|
|
||
| <!-- Indexer (not collapsible) --> | ||
| <Card.Root> | ||
| <Card.Header class="pb-2"> | ||
| <Card.Title class="text-base">{indexerSchema?.title || "Indexer"}</Card.Title> | ||
| {#if indexerSchema?.description} | ||
| <Card.Description>{indexerSchema.description}</Card.Description> | ||
| {/if} | ||
| </Card.Header> | ||
| <Card.Content class="space-y-4"> | ||
| {#each indexerFields as fieldName (fieldName)} | ||
| <Field {form} path={["indexer", fieldName] as FieldPath} /> | ||
| {/each} | ||
| </Card.Content> | ||
| </Card.Root> | ||
|
|
||
| <!-- Subtitles --> | ||
| <CollapsibleCard | ||
| title={subtitleSchema?.title || "Subtitles"} | ||
| description={subtitleSchema?.description} | ||
| hasToggle | ||
| enabled={subtitle?.enabled ?? false} | ||
| onToggle={toggleSubtitle} | ||
| contentClass="space-y-4"> | ||
| {#each subtitleFields as fieldName (fieldName)} | ||
| <Field {form} path={["post_processing", "subtitle", fieldName] as FieldPath} /> | ||
| {/each} | ||
| </CollapsibleCard> | ||
|
|
||
| <!-- Export --> | ||
| <Card.Root> | ||
| <Card.Header> | ||
| <Card.Title class="text-base">Export Settings</Card.Title> | ||
| <Card.Description> | ||
| Download your settings as a JSON file. Sensitive values (API keys, tokens) will be | ||
| redacted. | ||
| </Card.Description> | ||
| </Card.Header> | ||
| <Card.Content> | ||
| <Button variant="outline" onclick={handleExport}> | ||
| <FileDown class="mr-2 h-4 w-4" /> | ||
| Export Settings | ||
| </Button> | ||
| </Card.Content> | ||
| </Card.Root> | ||
| </div> | ||
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,101 @@ | ||
| <script lang="ts"> | ||
| import * as Card from "$lib/components/ui/card"; | ||
| import { Switch } from "$lib/components/ui/switch"; | ||
| import ChevronDown from "@lucide/svelte/icons/chevron-down"; | ||
| import type { Snippet } from "svelte"; | ||
|
|
||
| interface Props { | ||
| title: string; | ||
| description?: string; | ||
| hasToggle?: boolean; | ||
| enabled?: boolean; | ||
| onToggle?: (enabled: boolean) => void; | ||
| /** Initial expanded state - if hasToggle is true and no explicit value, defaults to enabled state */ | ||
| defaultExpanded?: boolean; | ||
| /** Content to render inside the card */ | ||
| children: Snippet; | ||
| /** Additional CSS classes for the Card.Content wrapper */ | ||
| contentClass?: string; | ||
| } | ||
|
|
||
| let { | ||
| title, | ||
| description, | ||
| hasToggle = false, | ||
| enabled = false, | ||
| onToggle, | ||
| defaultExpanded, | ||
| children, | ||
| contentClass = "" | ||
| }: Props = $props(); | ||
|
|
||
| // Track if user has manually overridden the expanded state | ||
| let userExpandedState = $state<boolean | null>(null); | ||
|
|
||
| // Compute effective expanded state: | ||
| // - If user has set a preference, use that | ||
| // - Otherwise, if hasToggle, follow enabled state | ||
| // - Otherwise use defaultExpanded (defaults to true for non-toggle cards) | ||
| const expanded = $derived( | ||
| userExpandedState ?? (hasToggle ? enabled : (defaultExpanded ?? true)) | ||
| ); | ||
|
|
||
| function setExpanded(value: boolean) { | ||
| userExpandedState = value; | ||
| } | ||
|
|
||
| function handleToggle(checked: boolean) { | ||
| onToggle?.(checked); | ||
| // When enabling, reset user state so it follows enabled | ||
| if (checked) userExpandedState = null; | ||
| } | ||
|
|
||
| // If enabled becomes true and user had collapsed it, reset to follow enabled | ||
| $effect(() => { | ||
| if (enabled && userExpandedState === false) { | ||
| userExpandedState = null; | ||
| } | ||
| }); | ||
| </script> | ||
|
|
||
| <Card.Root class="overflow-hidden"> | ||
| <div | ||
| role="button" | ||
| tabindex="0" | ||
| class="hover:bg-muted/50 flex w-full cursor-pointer items-center justify-between p-4 text-left transition-colors" | ||
| onclick={() => setExpanded(!expanded)} | ||
| onkeydown={(e) => { | ||
| if (e.key === "Enter" || e.key === " ") { | ||
| e.preventDefault(); | ||
| setExpanded(!expanded); | ||
| } | ||
| }}> | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| <div class="flex items-center gap-3"> | ||
| {#if hasToggle} | ||
| <span role="presentation" onclick={(e: MouseEvent) => e.stopPropagation()}> | ||
| <Switch checked={enabled} onCheckedChange={handleToggle} /> | ||
| </span> | ||
| {/if} | ||
| <div> | ||
| <h4 class="font-medium">{title}</h4> | ||
| {#if description && (!hasToggle || !enabled)} | ||
| <p class="text-muted-foreground text-sm">{description}</p> | ||
| {/if} | ||
| </div> | ||
| </div> | ||
| <ChevronDown | ||
| class="text-muted-foreground h-5 w-5 transition-transform duration-200 {expanded | ||
| ? 'rotate-180' | ||
| : ''}" /> | ||
| </div> | ||
|
|
||
| <div | ||
| class="grid transition-[grid-template-rows] duration-200 ease-out" | ||
| style="grid-template-rows: {expanded ? '1fr' : '0fr'}"> | ||
| <div class="overflow-hidden"> | ||
| <Card.Content class="border-t pt-4 {contentClass}"> | ||
| {@render children()} | ||
| </Card.Content> | ||
| </div> | ||
| </div> | ||
| </Card.Root> | ||
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,66 @@ | ||
| const SENSITIVE_KEY_PATTERNS = [ | ||
| /api[_-]?key/i, | ||
| /token/i, | ||
| /secret/i, | ||
| /password/i, | ||
| /credential/i, | ||
| /auth/i, | ||
| /private/i, | ||
| /bearer/i, | ||
| /[_-]?key$/i, // Matches camelCase (apiKey, privateKey) and snake_case (api_key) | ||
| /^rss$/i // RSS feeds can contain auth tokens | ||
| ]; | ||
|
|
||
| // Matches connection strings with embedded credentials: scheme://user:password@host | ||
| const CONNECTION_STRING_PATTERN = /^[a-z][a-z0-9+.-]*:\/\/[^:]+:[^@]+@/i; | ||
|
|
||
| function isSensitiveKey(key: string): boolean { | ||
| return SENSITIVE_KEY_PATTERNS.some((pattern) => pattern.test(key)); | ||
| } | ||
|
|
||
| function isSensitiveValue(value: string): boolean { | ||
| return CONNECTION_STRING_PATTERN.test(value); | ||
| } | ||
|
|
||
| function obfuscateObject(obj: unknown, parentKey = ""): unknown { | ||
| if (obj === null || obj === undefined) { | ||
| return obj; | ||
| } | ||
|
|
||
| if (Array.isArray(obj)) { | ||
| return obj.map((item, index) => obfuscateObject(item, `${parentKey}[${index}]`)); | ||
| } | ||
|
|
||
| if (typeof obj === "object") { | ||
| const result: Record<string, unknown> = {}; | ||
| for (const [key, value] of Object.entries(obj)) { | ||
| if (typeof value === "string" && value.length > 0) { | ||
| if (isSensitiveKey(key) || isSensitiveValue(value)) { | ||
| result[key] = `redacted-${key}`; | ||
| } else { | ||
| result[key] = value; | ||
| } | ||
|
hiitsrob marked this conversation as resolved.
Outdated
|
||
| } else { | ||
| result[key] = obfuscateObject(value, key); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| return obj; | ||
| } | ||
|
|
||
| export function exportSettings(settings: unknown, filename = "riven-settings.json"): void { | ||
| const obfuscated = obfuscateObject(settings); | ||
| const json = JSON.stringify(obfuscated, null, 2); | ||
| const blob = new Blob([json], { type: "application/json" }); | ||
| const url = URL.createObjectURL(blob); | ||
|
|
||
| const link = document.createElement("a"); | ||
| link.href = url; | ||
| link.download = filename; | ||
| document.body.appendChild(link); | ||
| link.click(); | ||
| document.body.removeChild(link); | ||
| URL.revokeObjectURL(url); | ||
| } | ||
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.