refactor: settings into schema based thing

This commit is contained in:
Xylight committed 2026-04-03 14:34:40 -07:00
1 parent 5cfec11dbb
commit 6542266bed
1 file changed
+107 -153
+107 -153
View File
@@ -4,17 +4,11 @@ import type { CommentSortType, ListingType, SortType } from '$lib/api/types'
import type { Filter } from '$lib/feature/post/filters.svelte'
import { locale } from './i18n'
// TODO extract settings into submodules
export type View = 'cozy' | 'compact'
export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true'
// Returns a proper boolean or null. Used to set boolean values from env var strings while allowing nullish coalescing to set default values.
const toBool = (str: string | undefined) => {
if (!str) {
return null
}
if (!str) return null
return str.toLowerCase() === 'true'
}
@@ -23,174 +17,136 @@ interface Preset {
content: string
}
interface Settings {
settingsVer: number
expandableImages: boolean
// should have been named "fade" read posts
markReadPosts: boolean
showInstances: {
user: boolean
community: boolean
comments: boolean
}
type Leaf<T = any> = { default: T; env?: string }
view: View
defaultSort: {
sort: SortType
feed: ListingType
comments: CommentSortType
}
hidePosts: {
deleted: boolean
removed: boolean
}
expandSidebar: boolean
expand: {
communities: boolean
moderates: boolean
favorites: boolean
about: boolean
stats: boolean
team: boolean
accounts: boolean
}
displayNames: boolean
nsfwBlur: boolean
moderation: {
presets: Preset[]
defaultRemoveAction: 'comment' | 'message' | null
}
modlogCardView: boolean | undefined
debugInfo: boolean
expandImages: boolean
font: 'inter' | 'system' | 'browser' | 'serifs'
leftAlign: boolean
newWidth: boolean
markPostsAsRead: boolean
openLinksInNewTab: boolean
crosspostOriginalLink: boolean
embeds: {
clickToView: boolean
youtube: 'youtube' | 'invidious' | 'piped'
invidious: string | undefined
piped: string | undefined
}
dock: {
paletteHotkey: string
autoHide: boolean
}
posts: {
deduplicateEmbed: boolean
compactFeatured: boolean
showHidden: boolean
noVirtualize: boolean
reverseActions: boolean
titleOpensUrl: boolean
}
filters: Filter[]
forms: {
autosubmitAutofill: boolean
}
infiniteScroll: boolean
language: string | null
useRtl: boolean
parseTags: boolean
logoColorMonth: number | null
absoluteDates: boolean
messages: {
fullMarkdown: boolean
}
voteRatioBar: boolean
type InferSettings<S> = {
-readonly [K in keyof S]: S[K] extends Leaf<infer T> ? T : InferSettings<S[K]>
}
export const defaultSettings: Settings = {
settingsVer: 8,
expandableImages: toBool(env.PUBLIC_EXPANDABLE_IMAGES) ?? true,
markReadPosts: toBool(env.PUBLIC_MARK_READ_POSTS) ?? true,
function isLeaf(node: unknown): node is Leaf {
return node != null && typeof node === 'object' && 'default' in node
}
function resolveSchema(schema: Record<string, any>): any {
const result: Record<string, any> = {}
for (const [key, node] of Object.entries(schema)) {
if (isLeaf(node)) {
if (node.env) {
// i hate typescript
const envVal = (env as any)[node.env]
if (typeof node.default === 'boolean') {
result[key] = toBool(envVal) ?? node.default
} else {
result[key] = envVal ?? node.default
}
} else {
result[key] = node.default
}
} else {
result[key] = resolveSchema(node)
}
}
return result
}
const settingsSchema = {
settingsVer: { default: 8 },
expandableImages: { default: true, env: 'PUBLIC_EXPANDABLE_IMAGES' },
markReadPosts: { default: true, env: 'PUBLIC_MARK_READ_POSTS' },
showInstances: {
user: toBool(env.PUBLIC_SHOW_INSTANCES_USER) ?? true,
community: toBool(env.PUBLIC_SHOW_INSTANCES_COMMUNITY) ?? true,
comments: toBool(env.PUBLIC_SHOW_INSTANCES_COMMENTS) ?? true,
user: { default: true, env: 'PUBLIC_SHOW_INSTANCES_USER' },
community: { default: true, env: 'PUBLIC_SHOW_INSTANCES_COMMUNITY' },
comments: { default: true, env: 'PUBLIC_SHOW_INSTANCES_COMMENTS' },
},
defaultSort: {
sort: (env.PUBLIC_DEFAULT_FEED_SORT ?? 'Active') as SortType,
feed: (env.PUBLIC_DEFAULT_FEED ?? 'Local') as ListingType,
comments: (env.PUBLIC_DEFAULT_COMMENT_SORT ?? 'Hot') as CommentSortType,
sort: { default: 'Active' as SortType, env: 'PUBLIC_DEFAULT_FEED_SORT' },
feed: { default: 'Local' as ListingType, env: 'PUBLIC_DEFAULT_FEED' },
comments: {
default: 'Hot' as CommentSortType,
env: 'PUBLIC_DEFAULT_COMMENT_SORT',
},
},
hidePosts: {
deleted: toBool(env.PUBLIC_HIDE_DELETED) ?? false,
removed: toBool(env.PUBLIC_HIDE_REMOVED) ?? false,
deleted: { default: false, env: 'PUBLIC_HIDE_DELETED' },
removed: { default: false, env: 'PUBLIC_HIDE_REMOVED' },
},
expandSidebar: toBool(env.PUBLIC_EXPAND_SIDEBAR) ?? true,
expandSidebar: { default: true, env: 'PUBLIC_EXPAND_SIDEBAR' },
expand: {
communities: toBool(env.PUBLIC_EXPAND_COMMUNITIES) ?? true,
favorites: toBool(env.PUBLIC_EXPAND_FAVORITES) ?? true,
moderates: toBool(env.PUBLIC_EXPAND_MODERATES) ?? true,
about: false,
stats: false,
team: false,
accounts: true,
communities: { default: true, env: 'PUBLIC_EXPAND_COMMUNITIES' },
favorites: { default: true, env: 'PUBLIC_EXPAND_FAVORITES' },
moderates: { default: true, env: 'PUBLIC_EXPAND_MODERATES' },
about: { default: false },
stats: { default: false },
team: { default: false },
accounts: { default: true },
},
displayNames: toBool(env.PUBLIC_DISPLAY_NAMES) ?? true,
nsfwBlur: toBool(env.PUBLIC_NSFW_BLUR) ?? true,
displayNames: { default: true, env: 'PUBLIC_DISPLAY_NAMES' },
nsfwBlur: { default: true, env: 'PUBLIC_NSFW_BLUR' },
moderation: {
presets: [
{
title: 'Preset 1',
content: `Your submission in *"{{post}}"* was removed for {{reason}}.`,
},
],
defaultRemoveAction: null,
presets: {
default: [
{
title: 'Preset 1',
content: `Your submission in *"{{post}}"* was removed for {{reason}}.`,
},
] as Preset[],
},
defaultRemoveAction: { default: null as 'comment' | 'message' | null },
},
modlogCardView: toBool(env.PUBLIC_MODLOG_CARD_VIEW) ?? undefined,
debugInfo: toBool(env.PUBLIC_DEBUG_INFO) ?? false,
expandImages: toBool(env.PUBLIC_EXPAND_IMAGES) ?? true,
view: (env.PUBLIC_VIEW as View) ?? 'compact',
font: (env.PUBLIC_FONT as 'inter') ?? 'inter',
leftAlign: toBool(env.PUBLIC_LEFT_ALIGN) ?? false,
newWidth: toBool(env.PUBLIC_LIMIT_LAYOUT_WIDTH) ?? true,
markPostsAsRead: toBool(env.PUBLIC_MARK_POSTS_AS_READ) ?? true,
openLinksInNewTab: false,
crosspostOriginalLink: true,
modlogCardView: {
default: (toBool(env.PUBLIC_MODLOG_CARD_VIEW) ?? undefined) as
| boolean
| undefined,
},
debugInfo: { default: false, env: 'PUBLIC_DEBUG_INFO' },
expandImages: { default: true, env: 'PUBLIC_EXPAND_IMAGES' },
view: { default: 'compact' as View, env: 'PUBLIC_VIEW' },
font: {
default: 'inter' as 'inter' | 'system' | 'browser' | 'serifs',
env: 'PUBLIC_FONT',
},
leftAlign: { default: false, env: 'PUBLIC_LEFT_ALIGN' },
newWidth: { default: true, env: 'PUBLIC_LIMIT_LAYOUT_WIDTH' },
markPostsAsRead: { default: true, env: 'PUBLIC_MARK_POSTS_AS_READ' },
openLinksInNewTab: { default: false },
crosspostOriginalLink: { default: true },
embeds: {
clickToView: true,
youtube: 'youtube',
invidious: undefined,
piped: undefined,
clickToView: { default: true },
youtube: { default: 'youtube' as 'youtube' | 'invidious' | 'piped' },
invidious: { default: undefined as string | undefined },
piped: { default: undefined as string | undefined },
},
dock: {
paletteHotkey: '/',
autoHide: true,
paletteHotkey: { default: '/' },
autoHide: { default: true },
},
posts: {
deduplicateEmbed: toBool(env.PUBLIC_DEDUPLICATE_EMBED) ?? true,
compactFeatured: toBool(env.PUBLIC_COMPACT_FEATURED) ?? true,
showHidden: false,
noVirtualize: false,
reverseActions: toBool(env.PUBLIC_REVERSE_ACTIONS) ?? false,
titleOpensUrl: toBool(env.PUBLIC_TITLE_OPENS_URL) ?? false,
deduplicateEmbed: { default: true, env: 'PUBLIC_DEDUPLICATE_EMBED' },
compactFeatured: { default: true, env: 'PUBLIC_COMPACT_FEATURED' },
showHidden: { default: false },
noVirtualize: { default: false },
reverseActions: { default: false, env: 'PUBLIC_REVERSE_ACTIONS' },
titleOpensUrl: { default: false, env: 'PUBLIC_TITLE_OPENS_URL' },
},
filters: [],
filters: { default: [] as Filter[] },
forms: {
autosubmitAutofill: false,
autosubmitAutofill: { default: false },
},
infiniteScroll: true,
language: env.PUBLIC_LANGUAGE ?? null,
useRtl: false,
parseTags: true,
logoColorMonth: null,
absoluteDates: false,
infiniteScroll: { default: true },
language: { default: null as string | null, env: 'PUBLIC_LANGUAGE' },
useRtl: { default: false },
parseTags: { default: true },
logoColorMonth: { default: null as number | null },
absoluteDates: { default: false },
messages: {
fullMarkdown: toBool(env.PUBLIC_FULL_MARKDOWN) ?? false,
fullMarkdown: { default: false, env: 'PUBLIC_FULL_MARKDOWN' },
},
voteRatioBar: false,
voteRatioBar: { default: false },
}
export type Settings = InferSettings<typeof settingsSchema>
export const defaultSettings: Settings = resolveSchema(settingsSchema)
function createSettingsState(initial: Settings): Settings {
let settings = $state(initial)
if (browser) {
@@ -206,9 +162,7 @@ function createSettingsState(initial: Settings): Settings {
return settings
}
export const settings = createSettingsState(
JSON.parse(JSON.stringify(defaultSettings)),
)
export const settings = createSettingsState(structuredClone(defaultSettings))
$effect.root(() => {
$effect(() => {