Skip to content

Commit

Permalink
Merge pull request #26 from yushiang-demo/enhnace-shareLink-encoded-t…
Browse files Browse the repository at this point in the history
…o-hash

Enhance: pass edit data from hash insteaed of query string.
  • Loading branch information
tsengyushiang committed Aug 19, 2023
2 parents 55069a0 + 5a171b3 commit d9841e9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://github.com/olivierlacan/keep-a
### Changed

- Change `styled-component` babel setting to SWC according to [official document](https://styled-components.com/docs/advanced#with-swc). (https://github.com/yushiang-demo/PanoToMesh/pull/16)
- Change share link to load data from hash instead of query string. (https://github.com/yushiang-demo/PanoToMesh/pull/26)

### Fixed

Expand Down
17 changes: 9 additions & 8 deletions apps/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Input from "../components/Input";
import Icons from "../components/Icon";
import Toolbar from "../components/Toolbar";
import RatioLockedDiv from "../components/RatioLockedDiv";
import { encodeString } from "../helpers/pako";
import { useStoreDataToHash } from "../hooks/useHash";

const downloadImage = (filename, url) => {
const a = document.createElement("a");
Expand Down Expand Up @@ -64,6 +64,10 @@ const Editor = ({ src }) => {
panorama,
panoramaOrigin,
};
const hash = useStoreDataToHash({
...props,
panorama: imageSrc,
});

const loadPanoramaFromLocal = () => {
const fileInput = document.createElement("input");
Expand Down Expand Up @@ -97,13 +101,10 @@ const Editor = ({ src }) => {
};

const onShare = () => {
const raw = {
...props,
panorama: imageSrc,
};
const rawString = JSON.stringify(raw);
const encodedString = encodeString(rawString);
window.open(`/?data=${encodedString}`);
const newTab = window.open(`/#${hash}`);
if (!newTab) {
alert("Pop-up blocker prevented opening the new tab.");
}
};

return (
Expand Down
33 changes: 33 additions & 0 deletions hooks/useHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState, useEffect } from "react";
import { encodeString, decodeString } from "../helpers/pako";

export const useStoreDataToHash = (data) => {
const [hash, setHash] = useState("");

useEffect(() => {
const rawString = JSON.stringify(data);
const encodedString = encodeString(rawString);
setHash(encodedString);
window.location.hash = encodedString;
}, [data]);

return hash;
};

export const useDecodedHash = () => {
const [data, setData] = useState(null);

useEffect(() => {
try {
const hash = window.location.hash;
if (hash) {
const decoded = decodeString(hash);
setData(JSON.parse(decoded));
}
} catch (e) {
console.error(e);
}
}, []);

return data;
};
24 changes: 0 additions & 24 deletions hooks/useQueryString.js

This file was deleted.

4 changes: 2 additions & 2 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from "react";
import Viewer from "../apps/viewer";
import useQueryString from "../hooks/useQueryString";
import { useDecodedHash } from "../hooks/useHash";

const Apps = () => {
const { data } = useQueryString();
const data = useDecodedHash();
return <Viewer data={data} />;
};

Expand Down

0 comments on commit d9841e9

Please sign in to comment.