-
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.
fix: prevent the unexpected disclaimer modal is popping up on iOS
- created `useLocalStorage`. - deleted `disclaimer` store. - created `withDisclaimerAgreement` HoC. - relocated `DisclaimerModal` to `hocs/withDisclaimerAgreement`. - removed unnecessary state in `DisclaimerModal`. - updated `Button` to style buttons in invalid forms. - and more...
- Loading branch information
Showing
9 changed files
with
789 additions
and
696 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 was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from "react"; | ||
import DisclaimerModal from "./DisclaimerModal"; | ||
import useDisclaimerAgreement from "./useDisclaimerAgreement"; | ||
|
||
function withDisclaimerAgreement<P extends object>( | ||
Component: React.ComponentType<P>, | ||
) { | ||
function WrappedComponent(props: P) { | ||
const { isDisclaimerAgreed, agreeDisclaimer } = useDisclaimerAgreement(); | ||
return ( | ||
<> | ||
<DisclaimerModal | ||
isOpen={!isDisclaimerAgreed} | ||
onAgree={() => { | ||
agreeDisclaimer(); | ||
}} | ||
/> | ||
{isDisclaimerAgreed && <Component {...props} />} | ||
</> | ||
); | ||
} | ||
return WrappedComponent; | ||
} | ||
|
||
export default withDisclaimerAgreement; |
27 changes: 27 additions & 0 deletions
27
src/hocs/withDisclaimerAgreement/useDisclaimerAgreement.ts
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,27 @@ | ||
import useLocalStorage from "hooks/useLocalStorage"; | ||
import { useMemo } from "react"; | ||
|
||
const useDisclaimerAgreement = () => { | ||
const [disclaimerLastSeen, setDisclaimerLastSeen] = useLocalStorage( | ||
"disclaimer", | ||
0, | ||
); | ||
|
||
const isDisclaimerAgreed = useMemo(() => { | ||
if (!disclaimerLastSeen) return false; | ||
const date = new Date(); | ||
date.setDate(date.getDate() - 3); | ||
return new Date(disclaimerLastSeen) > date; | ||
}, [disclaimerLastSeen]); | ||
|
||
const agreeDisclaimer = () => { | ||
setDisclaimerLastSeen(Date.now()); | ||
}; | ||
|
||
return { | ||
isDisclaimerAgreed, | ||
agreeDisclaimer, | ||
}; | ||
}; | ||
|
||
export default useDisclaimerAgreement; |
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,50 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
|
||
const useLocalStorage = <T = string>(key: string, initialValue: T) => { | ||
const [storedValue, setStoredValue] = useState(() => { | ||
try { | ||
const item = window.localStorage.getItem(key); | ||
return item; | ||
} catch { | ||
return null; | ||
} | ||
}); | ||
|
||
const value = useMemo<T>(() => { | ||
try { | ||
return storedValue ? JSON.parse(storedValue) : initialValue; | ||
} catch { | ||
return storedValue; | ||
} | ||
}, [initialValue, storedValue]); | ||
|
||
const setValue: React.Dispatch<React.SetStateAction<T>> = (newValue) => { | ||
try { | ||
const valueToStore = | ||
newValue instanceof Function ? newValue(value) : newValue; | ||
setStoredValue(JSON.stringify(valueToStore)); | ||
window.localStorage.setItem(key, JSON.stringify(valueToStore)); | ||
} catch { | ||
// ignore write errors | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const handleStorageChange = () => { | ||
try { | ||
const item = window.localStorage.getItem(key); | ||
setStoredValue(item); | ||
} catch { | ||
setStoredValue(null); | ||
} | ||
}; | ||
window.addEventListener("storage", handleStorageChange); | ||
return () => { | ||
window.removeEventListener("storage", handleStorageChange); | ||
}; | ||
}, [initialValue, key]); | ||
|
||
return [value, setValue] as const; | ||
}; | ||
|
||
export default useLocalStorage; |
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 was deleted.
Oops, something went wrong.