mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 08:47:23 +00:00
Only set app config that's actually changed
This commit is contained in:
2 files changed
+9
-7
No files matched your search
@@ -14,10 +14,10 @@ interface AppProviderProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Zod schema for AppConfig validation
|
// Zod schema for AppConfig validation
|
||||||
const AppConfigSchema: z.ZodType<AppConfig, z.ZodTypeDef, unknown> = z.object({
|
const AppConfigSchema = z.object({
|
||||||
theme: z.enum(['dark', 'light', 'system']),
|
theme: z.enum(['dark', 'light', 'system']),
|
||||||
relayUrl: z.string().url(),
|
relayUrl: z.string().url(),
|
||||||
});
|
}) satisfies z.ZodType<AppConfig>;
|
||||||
|
|
||||||
export function AppProvider(props: AppProviderProps) {
|
export function AppProvider(props: AppProviderProps) {
|
||||||
const {
|
const {
|
||||||
@@ -28,23 +28,25 @@ export function AppProvider(props: AppProviderProps) {
|
|||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
// App configuration state with localStorage persistence
|
// App configuration state with localStorage persistence
|
||||||
const [config, setConfig] = useLocalStorage<AppConfig>(
|
const [rawConfig, setConfig] = useLocalStorage<Partial<AppConfig>>(
|
||||||
storageKey,
|
storageKey,
|
||||||
defaultConfig,
|
{},
|
||||||
{
|
{
|
||||||
serialize: JSON.stringify,
|
serialize: JSON.stringify,
|
||||||
deserialize: (value: string) => {
|
deserialize: (value: string) => {
|
||||||
const parsed = JSON.parse(value);
|
const parsed = JSON.parse(value);
|
||||||
return AppConfigSchema.parse(parsed);
|
return AppConfigSchema.partial().parse(parsed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Generic config updater with callback pattern
|
// Generic config updater with callback pattern
|
||||||
const updateConfig = (updater: (currentConfig: AppConfig) => AppConfig) => {
|
const updateConfig = (updater: (currentConfig: Partial<AppConfig>) => Partial<AppConfig>) => {
|
||||||
setConfig(updater);
|
setConfig(updater);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const config = { ...defaultConfig, ...rawConfig };
|
||||||
|
|
||||||
const appContextValue: AppContextType = {
|
const appContextValue: AppContextType = {
|
||||||
config,
|
config,
|
||||||
updateConfig,
|
updateConfig,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export interface AppContextType {
|
|||||||
/** Current application configuration */
|
/** Current application configuration */
|
||||||
config: AppConfig;
|
config: AppConfig;
|
||||||
/** Update configuration using a callback that receives current config and returns new config */
|
/** Update configuration using a callback that receives current config and returns new config */
|
||||||
updateConfig: (updater: (currentConfig: AppConfig) => AppConfig) => void;
|
updateConfig: (updater: (currentConfig: Partial<AppConfig>) => Partial<AppConfig>) => void;
|
||||||
/** Optional list of preset relays to display in the RelaySelector */
|
/** Optional list of preset relays to display in the RelaySelector */
|
||||||
presetRelays?: { name: string; url: string }[];
|
presetRelays?: { name: string; url: string }[];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user