Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
fix: plugin API viewport.query destroys published pages (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 authored Dec 21, 2022
1 parent b050aa3 commit de8c012
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/components/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ overrides:
- error
- paths:
- '@reearth/theme'
- files: molecules/Visualizer/**/*
rules:
'no-restricted-imports':
- error
- paths:
- name: react-router-dom
message: Cannot use react-router-dom in published pages. Don't use it.
20 changes: 4 additions & 16 deletions src/components/molecules/Visualizer/useViewport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useEffect, RefObject } from "react";
import { useSearchParams } from "react-router-dom";
import { useState, useEffect, RefObject, useMemo } from "react";

const viewportMobileMaxWidth = 768;

Expand All @@ -15,11 +14,12 @@ type Props = {
};

export default ({ wrapperRef }: Props) => {
const query = useMemo(() => paramsToObject(new URLSearchParams(window.location.search)), []);
const [viewport, setViewport] = useState<Viewport>({
width: undefined,
height: undefined,
isMobile: undefined,
query: {},
query,
});

useEffect(() => {
Expand Down Expand Up @@ -62,22 +62,10 @@ export default ({ wrapperRef }: Props) => {
};
}, [wrapperRef]);

const [searchParams] = useSearchParams();

useEffect(() => {
setViewport(viewport => {
const query = paramsToObject(searchParams.entries());
return {
...viewport,
query,
};
});
}, [searchParams]);

return viewport;
};

function paramsToObject(entries: IterableIterator<[string, string]>) {
function paramsToObject(entries: URLSearchParams) {
const result: Record<string, string> = {};
for (const [key, value] of entries) {
result[key] = value;
Expand Down

0 comments on commit de8c012

Please sign in to comment.