Skip to content

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

refactor: move reused source to ui-store

Harsh Karandecf1074c139c1
diff --git a/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.tsx b/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.tsx
index d37f6245343..5a306b8dab0 100644
--- a/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.tsx
+++ b/studio/components/layouts/ProjectLayout/NavigationBar/NavigationBar.tsx
@@ -1,4 +1,4 @@
-import React, { FC, useEffect, useState } from 'react'
+import React, { FC, useState } from 'react'
import Link from 'next/link'
import { observer } from 'mobx-react-lite'
import { useRouter } from 'next/router'
@@ -21,27 +21,7 @@ const NavigationBar: FC<Props> = ({}) => {
const productRoutes = generateProductRoutes(projectRef)
const otherRoutes = generateOtherRoutes(projectRef)
- const [value, setValue] = useState('')
-
- useEffect(() => {
- const localStorageThemeOption = window.localStorage.getItem('theme') as string
- 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)
- }
+ const [value, setValue] = useState(ui.themeOption)
return (
<div className="h-screen w-14 flex flex-col justify-between p-2 overflow-y-hidden bg-sidebar-light dark:bg-sidebar-dark border-r dark:border-dark">
@@ -101,7 +81,13 @@ const NavigationBar: FC<Props> = ({}) => {
<Dropdown.Misc key="theme">
<div className="w-[240px] py-1 flex items-center justify-between">
<Typography.Text>Theme</Typography.Text>
- <Select value={value} onChange={onThemeOptionChange}>
+ <Select
+ value={value}
+ onChange={(e: any) => {
+ setValue(e.target.value)
+ ui.onThemeOptionChange(e.target.value)
+ }}
+ >
<Select.Option value="system">System default</Select.Option>
<Select.Option value="dark">Dark</Select.Option>
<Select.Option value="light">Light</Select.Option>
diff --git a/studio/hooks/misc/useStore.tsx b/studio/hooks/misc/useStore.tsx
index 046c4051b2e..4060949ce31 100644
--- a/studio/hooks/misc/useStore.tsx
+++ b/studio/hooks/misc/useStore.tsx
@@ -34,7 +34,7 @@ export const StoreProvider: FC<StoreProvider> = ({ children, rootStore }) => {
}, [theme, monaco])
const matchMediaEvent = useCallback(() => {
- window.localStorage.getItem('theme') === 'system' &&
+ ui.themeOption === 'system' &&
ui.setTheme(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
}, [])
diff --git a/studio/pages/account/me.tsx b/studio/pages/account/me.tsx
index f0a4a625684..7668db3415a 100644
--- a/studio/pages/account/me.tsx
+++ b/studio/pages/account/me.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useState } from 'react'
+import React, { useState } from 'react'
import { observer } from 'mobx-react-lite'
import { IconMoon, IconSun, Select, Typography } from '@supabase/ui'
@@ -78,28 +78,8 @@ const ProfileCard = observer(() => {
})
function ThemeSettings() {
- const [value, setValue] = useState('')
const { ui } = useStore()
-
- useEffect(() => {
- 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)
- }
+ const [value, setValue] = useState(ui.themeOption)
return (
<Panel
@@ -117,7 +97,10 @@ function ThemeSettings() {
layout="horizontal"
style={{ width: '50%' }}
icon={value === 'light' ? <IconSun /> : value === 'dark' ? <IconMoon /> : undefined}
- onChange={onThemeOptionChange}
+ onChange={(e: any) => {
+ setValue(e.target.value)
+ ui.onThemeOptionChange(e.target.value)
+ }}
>
<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 eae272deebf..728a0a99171 100644
--- a/studio/stores/UiStore.ts
+++ b/studio/stores/UiStore.ts
@@ -7,6 +7,7 @@ import Telemetry from 'lib/telemetry'
export interface IUiStore {
language: 'en_US'
theme: 'dark' | 'light'
+ themeOption: 'dark' | 'light' | 'system'
isDarkTheme: boolean
selectedProject?: Project
@@ -16,6 +17,7 @@ export interface IUiStore {
load: () => void
setTheme: (theme: 'dark' | 'light') => void
+ onThemeOptionChange: (themeOption: 'dark' | 'light' | 'system') => void
setProjectRef: (ref?: string) => void
setOrganizationSlug: (slug?: string) => void
setNotification: (notification: Notification) => string
@@ -25,6 +27,7 @@ export default class UiStore implements IUiStore {
rootStore: IRootStore
language: 'en_US' = 'en_US'
theme: 'dark' | 'light' = 'dark'
+ themeOption: 'dark' | 'light' | 'system' = 'dark'
selectedProjectRef?: string
selectedOrganizationSlug?: string
@@ -73,12 +76,18 @@ export default class UiStore implements IUiStore {
load() {
if (typeof window === 'undefined') return
const localStorageThemeOption = window.localStorage.getItem('theme')
- if (localStorageThemeOption === 'system')
+ if (localStorageThemeOption === 'system') {
+ this.themeOption = localStorageThemeOption
return this.setTheme(
window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
)
- if (localStorageThemeOption === 'light') return this.setTheme('light')
+ }
+ if (localStorageThemeOption === 'light') {
+ this.themeOption = localStorageThemeOption
+ return this.setTheme('light')
+ }
window.localStorage.setItem('theme', 'dark')
+ this.themeOption = 'dark'
this.setTheme('dark')
}
@@ -87,6 +96,18 @@ export default class UiStore implements IUiStore {
document.body.className = theme
}
+ onThemeOptionChange(themeOption: 'dark' | 'light' | 'system') {
+ this.themeOption = themeOption
+ if (themeOption === 'system') {
+ window.localStorage.setItem('theme', 'system')
+ return this.setTheme(
+ window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
+ )
+ }
+ window.localStorage.setItem('theme', themeOption)
+ this.setTheme(themeOption)
+ }
+
setProjectRef(ref?: string) {
this.selectedProjectRef = ref
}