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