-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73f4d87
commit d4fd93a
Showing
5 changed files
with
102 additions
and
16 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
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,21 +1,44 @@ | ||
import { BaseStorage, createStorage, StorageType } from './base'; | ||
|
||
type Theme = 'light' | 'dark'; | ||
type SidebarPreference = boolean; | ||
|
||
type ThemeStorage = BaseStorage<Theme> & { | ||
toggle: () => Promise<void>; | ||
toggleTheme: () => Promise<void>; | ||
}; | ||
|
||
const storage = createStorage<Theme>('theme-storage-key', 'dark', { | ||
type SidebarStorage = BaseStorage<SidebarPreference> & { | ||
toggleSidebar: () => Promise<void>; | ||
}; | ||
|
||
// Storage for theme preference | ||
const themeStorage = createStorage<Theme>('theme-storage-key', 'dark', { | ||
storageType: StorageType.Local, | ||
liveUpdate: true, | ||
}); | ||
|
||
// Storage for sidebar preference | ||
const sidebarStorage = createStorage<SidebarPreference>('sidebar-storage-key', true, { | ||
storageType: StorageType.Local, | ||
liveUpdate: true, | ||
}); | ||
|
||
// Example theme storage with a toggle function | ||
export const exampleThemeStorage: ThemeStorage = { | ||
...storage, | ||
toggle: async () => { | ||
await storage.set(currentTheme => { | ||
return currentTheme === 'dark' ? 'dark' : 'dark'; | ||
...themeStorage, | ||
toggleTheme: async () => { | ||
await themeStorage.set(currentTheme => { | ||
return currentTheme === 'dark' ? 'light' : 'dark'; | ||
}); | ||
}, | ||
}; | ||
|
||
// Example sidebar storage with a toggle function | ||
export const exampleSidebarStorage: SidebarStorage = { | ||
...sidebarStorage, | ||
toggleSidebar: async () => { | ||
await sidebarStorage.set(currentSidebar => { | ||
return currentSidebar === true ? false : true; | ||
}); | ||
}, | ||
}; |
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 |
---|---|---|
@@ -1,8 +1,43 @@ | ||
import '@src/Options.css'; | ||
import { useEffect, useState } from 'react'; | ||
import { withErrorBoundary, withSuspense } from '@extension/shared'; | ||
import { exampleSidebarStorage } from '@extension/storage'; // Re-import the storage | ||
|
||
const Options = () => { | ||
return <div>Options page</div>; | ||
const [openSidebar, setOpenSidebar] = useState<boolean>(true); | ||
|
||
// Fetch the stored preference on component mount | ||
useEffect(() => { | ||
const fetchSidebarPreference = async () => { | ||
const storedValue = await exampleSidebarStorage.get(); | ||
if (typeof storedValue !== 'undefined') { | ||
setOpenSidebar(storedValue); | ||
} else { | ||
// Set default value to true if no preference is found | ||
await exampleSidebarStorage.set(true); | ||
setOpenSidebar(true); | ||
} | ||
}; | ||
fetchSidebarPreference(); | ||
}, []); | ||
|
||
// Handle toggle change | ||
const handleToggle = async (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = event.target.checked; | ||
setOpenSidebar(value); | ||
await exampleSidebarStorage.set(value); | ||
}; | ||
|
||
return ( | ||
<div className="options-container"> | ||
<h1>Extension Options</h1> | ||
<label> | ||
<input type="checkbox" checked={openSidebar} onChange={handleToggle} /> | ||
Open Sidebar instead of Popup | ||
</label> | ||
</div> | ||
); | ||
}; | ||
|
||
export default withErrorBoundary(withSuspense(Options, <div> Loading ... </div>), <div> Error Occur </div>); | ||
// Wrapping the component with error boundary and suspense | ||
export default withErrorBoundary(withSuspense(Options, <div>Loading...</div>), <div>Error Occurred</div>); |