fix spectrum ssr and remember sortkey for user

This commit is contained in:
Chuck Dries 2022-06-22 00:31:18 -07:00
parent dbb71561d7
commit f909c03aee
No known key found for this signature in database
GPG Key ID: A00B7AEAE1DC5BE6
3 changed files with 56 additions and 34 deletions

View File

@ -1,5 +1,5 @@
import * as React from "react"; import * as React from "react";
import { darkTheme, Provider } from "@adobe/react-spectrum"; import { darkTheme, Provider, SSRProvider } from "@adobe/react-spectrum";
import "./src/styles/global.css"; import "./src/styles/global.css";
const env = const env =
@ -28,19 +28,20 @@ export const onRouteUpdate = function () {
// p: MyParagraph, // p: MyParagraph,
// }; // };
export const wrapRootElement = ({ element }) => ( export const wrapRootElement = ({ element }) => (
<SSRProvider>
<Provider <Provider
UNSAFE_style={{ UNSAFE_style={{
background: "transparent !important", background: "transparent !important",
color: 'unset !important' color: "unset !important",
}} }}
colorScheme="dark" colorScheme="dark"
scale="medium"
theme={darkTheme} theme={darkTheme}
> >
{element} {element}
</Provider> </Provider>
</SSRProvider>
); );
// {/* // <MDXProvider components={components}>{element}</MDXProvider> */} // {/* // <MDXProvider components={components}>{element}</MDXProvider> */}

View File

@ -1,16 +1,20 @@
import * as React from "react"; import * as React from "react";
import { darkTheme, Provider } from "@adobe/react-spectrum"; import { darkTheme, Provider } from "@adobe/react-spectrum";
import "./src/styles/global.css"; import "./src/styles/global.css";
import { SSRProvider } from "@react-aria/ssr";
export const wrapRootElement = ({ element }) => ( export const wrapRootElement = ({ element }) => (
<SSRProvider>
<Provider <Provider
UNSAFE_style={{ UNSAFE_style={{
background: "transparent !important", background: "transparent !important",
color: 'unset !important' color: "unset !important",
}} }}
colorScheme="dark" colorScheme="dark"
scale="medium"
theme={darkTheme} theme={darkTheme}
> >
{element} {element}
</Provider> </Provider>
</SSRProvider>
); );

View File

@ -14,7 +14,21 @@ const SORT_KEYS = {
}; };
const GalleryPage = ({ data }) => { const GalleryPage = ({ data }) => {
const [sortKey, setSortKey] = React.useState("hue"); const [sortKey, _setSortKey] = React.useState("hue");
const setSortKey = React.useCallback(
(key) => {
localStorage?.setItem("photogallery.sortkey", key);
_setSortKey(key);
},
[_setSortKey]
);
React.useEffect(() => {
const _sortKey = localStorage.getItem("photogallery.sortkey");
if (_sortKey) {
setSortKey(_sortKey)
}
}, [setSortKey])
const images = React.useMemo( const images = React.useMemo(
() => () =>
@ -22,17 +36,22 @@ const GalleryPage = ({ data }) => {
R.map((edge) => edge.node), R.map((edge) => edge.node),
sortKey === "date" sortKey === "date"
? R.sort((node1, node2) => { ? R.sort((node1, node2) => {
const date1 = new Date(R.path(["fields", "imageMeta", "dateTaken"], node1)); const date1 = new Date(
const date2 = new Date(R.path(["fields", "imageMeta", "dateTaken"], node2)); R.path(["fields", "imageMeta", "dateTaken"], node1)
return -1 * (date1.getTime() - date2.getTime()) );
const date2 = new Date(
R.path(["fields", "imageMeta", "dateTaken"], node2)
);
return -1 * (date1.getTime() - date2.getTime());
}) })
: R.sortBy(R.path(SORT_KEYS[sortKey])) : R.sortBy(R.path(SORT_KEYS[sortKey]))
)(data.allFile.edges), )(data.allFile.edges),
[data, sortKey] [data, sortKey]
); );
const showDebug = typeof window !== "undefined" && const showDebug =
window.location.search.includes("debug=true") typeof window !== "undefined" &&
window.location.search.includes("debug=true");
return ( return (
<> <>
@ -73,9 +92,7 @@ const GalleryPage = ({ data }) => {
selectedKey={sortKey} selectedKey={sortKey}
> >
<Item key="hue">Hue</Item> <Item key="hue">Hue</Item>
{showDebug && ( {showDebug && <Item key="hue_debug">Dominant hue[debug]</Item>}
<Item key="hue_debug">Dominant hue[debug]</Item>
)}
<Item key="date">Date</Item> <Item key="date">Date</Item>
</Picker> </Picker>
</div> </div>
@ -89,7 +106,7 @@ const GalleryPage = ({ data }) => {
xl: 5, xl: 5,
// '2xl': 6.1, // '2xl': 6.1,
}} }}
debug={sortKey === 'hue_debug'} debug={sortKey === "hue_debug"}
images={images} images={images}
/> />
</div> </div>