Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

refactor: remove type system from set-theme

Harsh Karande39a3239eed90
diff --git a/studio/hooks/misc/useStore.tsx b/studio/hooks/misc/useStore.tsx
index 67b85bc81ca..046c4051b2e 100644
--- a/studio/hooks/misc/useStore.tsx
+++ b/studio/hooks/misc/useStore.tsx
@@ -33,7 +33,10 @@ export const StoreProvider: FC<StoreProvider> = ({ children, rootStore }) => {
}
}, [theme, monaco])
- const matchMediaEvent = useCallback(() => ui.theme === 'system' && ui.setTheme('system'), [])
+ const matchMediaEvent = useCallback(() => {
+ window.localStorage.getItem('theme') === 'system' &&
+ ui.setTheme(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
+ }, [])
useEffect(() => {
ui.load()
diff --git a/studio/pages/account/me.tsx b/studio/pages/account/me.tsx
index 70b34764b2f..f0a4a625684 100644
--- a/studio/pages/account/me.tsx
+++ b/studio/pages/account/me.tsx
@@ -78,14 +78,29 @@ const ProfileCard = observer(() => {
})
function ThemeSettings() {
- const [theme, setTheme] = useState('')
+ const [value, setValue] = useState('')
const { ui } = useStore()
useEffect(() => {
- const localStorageTheme = window.localStorage.getItem('theme')
- if (localStorageTheme) setTheme(localStorageTheme)
+ const localStorageThemeOption = window.localStorage.getItem('theme')
+ if (localStorageThemeOption) return setValue(localStorageThemeOption)
+ window.localStorage.setItem('theme', 'dark')
+ setValue('dark')
}, [])
+ const onThemeOptionChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
+ const themeOption = e.target.value as 'dark' | 'light' | 'system'
+ setValue(themeOption)
+ if (themeOption === 'system') {
+ window.localStorage.setItem('theme', 'system')
+ return ui.setTheme(
+ window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
+ )
+ }
+ window.localStorage.setItem('theme', themeOption)
+ ui.setTheme(themeOption)
+ }
+
return (
<Panel
title={[
@@ -96,16 +111,13 @@ function ThemeSettings() {
>
<Panel.Content>
<Select
- value={theme}
+ value={value}
label="Interface theme"
descriptionText="Choose a theme preference"
layout="horizontal"
style={{ width: '50%' }}
- icon={theme === 'light' ? <IconSun /> : theme === 'dark' ? <IconMoon /> : undefined}
- onChange={(e: any) => {
- setTheme(e.target.value)
- ui.setTheme(e.target.value)
- }}
+ icon={value === 'light' ? <IconSun /> : value === 'dark' ? <IconMoon /> : undefined}
+ onChange={onThemeOptionChange}
>
<Select.Option value="system">System default</Select.Option>
<Select.Option value="dark">Dark</Select.Option>
diff --git a/studio/stores/UiStore.ts b/studio/stores/UiStore.ts
index 56a079875c2..47131922f58 100644
--- a/studio/stores/UiStore.ts
+++ b/studio/stores/UiStore.ts
@@ -6,7 +6,7 @@ import Telemetry from 'lib/telemetry'
export interface IUiStore {
language: 'en_US'
- theme: 'dark' | 'light' | 'system'
+ theme: 'dark' | 'light'
isDarkTheme: boolean
selectedProject?: Project
@@ -16,7 +16,7 @@ export interface IUiStore {
load: () => void
toggleTheme: () => void
- setTheme: (theme: 'dark' | 'light' | 'system') => void
+ setTheme: (theme: 'dark' | 'light') => void
setProjectRef: (ref?: string) => void
setOrganizationSlug: (slug?: string) => void
setNotification: (notification: Notification) => string
@@ -25,7 +25,7 @@ export interface IUiStore {
export default class UiStore implements IUiStore {
rootStore: IRootStore
language: 'en_US' = 'en_US'
- theme: 'dark' | 'light' | 'system' = 'dark'
+ theme: 'dark' | 'light' = 'dark'
selectedProjectRef?: string
selectedOrganizationSlug?: string
@@ -68,33 +68,33 @@ export default class UiStore implements IUiStore {
}
get isDarkTheme() {
- return (
- this.theme === 'dark' ||
- (this.theme === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches)
- )
+ return this.theme === 'dark'
}
load() {
if (typeof window === 'undefined') return
- let savedTheme = (window.localStorage.getItem('theme') ?? 'dark') as 'dark' | 'light' | 'system'
- if (['dark', 'light', 'system'].includes(savedTheme)) return this.setTheme(savedTheme)
+ const localStorageThemeOption = window.localStorage.getItem('theme')
+ if (localStorageThemeOption === 'system')
+ return this.setTheme(
+ window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
+ )
+ if (localStorageThemeOption === 'light') return this.setTheme('light')
+ window.localStorage.setItem('theme', 'dark')
this.setTheme('dark')
}
toggleTheme() {
- if (this.theme === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches)
+ if (this.theme === 'dark') {
+ window.localStorage.setItem('theme', 'light')
return this.setTheme('light')
- if (this.theme === 'dark') return this.setTheme('light')
+ }
+ window.localStorage.setItem('theme', 'dark')
this.setTheme('dark')
}
- setTheme(theme: 'dark' | 'light' | 'system') {
+ setTheme(theme: 'dark' | 'light') {
this.theme = theme
- window.localStorage.setItem('theme', theme)
- if (theme !== 'system') return (document.body.className = theme)
- if (window.matchMedia('(prefers-color-scheme: dark)').matches)
- return (document.body.className = 'dark')
- document.body.className = 'light'
+ document.body.className = theme
}
setProjectRef(ref?: string) {