-
-
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.
✨ Added localstorage hook and dismissable welcome message
- Loading branch information
Luke Carr
committed
Apr 4, 2022
1 parent
67a1148
commit 761c35a
Showing
15 changed files
with
165 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
build-frontend: | ||
cd frontend && pnpm build | ||
pnpm --dir frontend build | ||
|
||
build: | ||
go build -o bin/tiny-todo main.go | ||
make build-frontend && go build -o bin/tiny-todo main.go | ||
|
||
run: | ||
make build-frontend && go run main.go serve |
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,43 @@ | ||
import { useState } from 'preact/hooks' | ||
|
||
export function useLocalStorage<T = any>(key: string, initialValue: T): [T, (value: T) => void] { | ||
// State to store our value | ||
// Pass initial state function to useState so logic is only executed once | ||
const [storedValue, setStoredValue] = useState<T>(() => { | ||
if (typeof window === "undefined") { | ||
return initialValue | ||
} | ||
|
||
try { | ||
// Get from local storage by key | ||
const item = window.localStorage.getItem(key) | ||
// Parse stored json or if none return initialValue | ||
return item ? JSON.parse(item) : initialValue | ||
} catch (error) { | ||
// If error also return initialValue | ||
console.log(error) | ||
return initialValue | ||
} | ||
}) | ||
|
||
// Return a wrapped version of useState's setter function that ... | ||
// ... persists the new value to localStorage. | ||
const setValue = (value: T) => { | ||
try { | ||
// Allow value to be a function so we have same API as useState | ||
const valueToStore = | ||
value instanceof Function ? value(storedValue) : value | ||
// Save state | ||
setStoredValue(valueToStore) | ||
// Save to local storage | ||
if (typeof window !== "undefined") { | ||
window.localStorage.setItem(key, JSON.stringify(valueToStore)) | ||
} | ||
} catch (error) { | ||
// A more advanced implementation would handle the error case | ||
console.log(error) | ||
} | ||
}; | ||
|
||
return [storedValue, setValue] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,17 @@ | ||
import { useLocalStorage } from 'hooks/local-storage' | ||
|
||
import type { FunctionalComponent } from 'preact' | ||
|
||
const Welcome: FunctionalComponent = () => { | ||
const [hidden, setHidden] = useLocalStorage('hideWelcome', false) | ||
|
||
if (hidden) return null | ||
|
||
return <section class="mt-4 border-l-4 border-gray-600 p-2 pl-3 font-semibold bg-gray-100"> | ||
<h3>Welcome to tiny-todo!</h3> | ||
<p>A simple todo app that's: tiny 🐜, lightweight 🔦🏋️♀️, and performant ⚡. Built using Go & Preact!</p> | ||
<button class="text-sm font-semibold hover:font-bold underline underline-dotted underline-black" onClick={() => setHidden(true)}>don't show again</button> | ||
</section> | ||
} | ||
|
||
export default Welcome |
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
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