-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent layout shift by using SettingsContext
- Loading branch information
Showing
5 changed files
with
62 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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,46 @@ | ||
import { createContext, useEffect, useState } from "react"; | ||
import { GetSettings, SetSetting } from "@wailsjs/go/main/App"; | ||
import { config } from "@wailsjs/go/models"; | ||
|
||
type Settings = config.AppConfig; | ||
|
||
type SettingsContextType = { | ||
settings: Settings | null; | ||
set: <T extends keyof Settings>(key: T, value: Settings[T]) => void; | ||
}; | ||
|
||
export const SettingsContext = createContext<SettingsContextType | null>(null); | ||
|
||
export function SettingsProvider({ children }: { children: React.ReactNode }) { | ||
const [settings, setSettings] = useState<Settings | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchSettings = async () => { | ||
try { | ||
const res = await GetSettings(); | ||
setSettings(res); | ||
} catch (e) { | ||
console.error("Failed to fetch settings:", e); | ||
} | ||
}; | ||
|
||
fetchSettings(); | ||
}, []); | ||
|
||
const set = <T extends keyof Settings>(key: T, value: Settings[T]) => { | ||
SetSetting(key, value) | ||
.then(() => { | ||
setSettings((prevSettings) => { | ||
if (!prevSettings) return null; | ||
return { ...prevSettings, [key]: value }; | ||
}); | ||
}) | ||
.catch(console.error); | ||
}; | ||
|
||
return ( | ||
<SettingsContext.Provider value={{ settings, set }}> | ||
{children} | ||
</SettingsContext.Provider> | ||
); | ||
} |
This file contains 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 |
---|---|---|
@@ -1,38 +1,11 @@ | ||
import { useState, useEffect } from "react"; | ||
import { config } from "@wailsjs/go/models"; | ||
import { GetSettings, SetSetting } from "@wailsjs/go/main/App"; | ||
import { useContext } from "react"; | ||
import { SettingsContext } from "@/components/providers/settings-provider"; | ||
|
||
type Settings = config.AppConfig; | ||
export default function useSettings() { | ||
const context = useContext(SettingsContext); | ||
if (context === null) { | ||
throw new Error("useSettings must be used within a SettingsProvider"); | ||
} | ||
|
||
export function useSettings() { | ||
const [settings, setSettings] = useState<Settings | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchSettings = async () => { | ||
try { | ||
const res = await GetSettings(); | ||
setSettings(res); | ||
} catch (e) { | ||
console.error("Failed to fetch settings:", e); | ||
} | ||
}; | ||
|
||
fetchSettings(); | ||
}, []); | ||
|
||
const set = <T extends keyof Settings>(key: T, value: Settings[T]) => { | ||
SetSetting(key, value) | ||
.then(() => { | ||
setSettings((prevSettings) => { | ||
if (!prevSettings) return null; | ||
return { ...prevSettings, [key]: value }; | ||
}); | ||
}) | ||
.catch(console.error); | ||
}; | ||
|
||
return { | ||
settings, | ||
set, | ||
}; | ||
return context; | ||
} |
This file contains 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 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