Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UrlSelect trigger value reset for non expo router projects #873

Merged
17 changes: 13 additions & 4 deletions packages/vscode-extension/src/webview/components/UrlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useProject } from "../providers/ProjectProvider";
import UrlSelect, { UrlItem } from "./UrlSelect";
import { IconButtonWithOptions } from "./IconButtonWithOptions";
import IconButton from "./shared/IconButton";
import { useDependencies } from "../providers/DependenciesProvider";

function ReloadButton({ disabled }: { disabled: boolean }) {
const { project } = useProject();
Expand All @@ -29,6 +30,7 @@ function ReloadButton({ disabled }: { disabled: boolean }) {

function UrlBar({ disabled }: { disabled?: boolean }) {
const { project, projectState } = useProject();
const { dependencies } = useDependencies();

const MAX_URL_HISTORY_SIZE = 20;
const MAX_RECENT_URL_SIZE = 5;
Expand All @@ -37,6 +39,7 @@ function UrlBar({ disabled }: { disabled?: boolean }) {
const [urlList, setUrlList] = useState<UrlItem[]>([]);
const [recentUrlList, setRecentUrlList] = useState<UrlItem[]>([]);
const [urlHistory, setUrlHistory] = useState<string[]>([]);
const [urlSelectValue, setUrlSelectValue] = useState<string>(urlList[0]?.id);

useEffect(() => {
function moveAsMostRecent(urls: UrlItem[], newUrl: UrlItem) {
Expand Down Expand Up @@ -76,10 +79,13 @@ function UrlBar({ disabled }: { disabled?: boolean }) {
}, [recentUrlList, urlHistory, backNavigationPath]);

const sortedUrlList = useMemo(() => {
return [...urlList].sort((a, b) => a.name.localeCompare(b.name));
const sorted = [...urlList].sort((a, b) => a.name.localeCompare(b.name));
setUrlSelectValue(urlList[0]?.id);
return sorted;
}, [urlList]);

const disabledAlsoWhenStarting = disabled || projectState.status === "starting";
const isExpoRouterProject = !dependencies.expoRouter?.isOptional;

return (
<>
Expand All @@ -88,7 +94,7 @@ function UrlBar({ disabled }: { disabled?: boolean }) {
label: "Go back",
side: "bottom",
}}
disabled={disabledAlsoWhenStarting || urlHistory.length < 2}
disabled={disabledAlsoWhenStarting || !isExpoRouterProject || urlHistory.length < 2}
onClick={() => {
setUrlHistory((prevUrlHistory) => {
const newUrlHistory = prevUrlHistory.slice(1);
Expand All @@ -103,6 +109,9 @@ function UrlBar({ disabled }: { disabled?: boolean }) {
<IconButton
onClick={() => {
project.goHome("/{}");
if (!isExpoRouterProject) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this being placed as a dependency of a user action as we'll have to remember to update it every time we add a new navigation option I would much rather have wrapper.js send an event every time application is loaded and then some global logic would handle setting the correct navigation state to the url bar.

Having said that I think we should move most of the logic in this file to the extension code to allow passing state between diffrent panel locations any wayand my suggestion will be easier to active then.

setUrlSelectValue(""); // sets UrlSelect trigger to a placeholder
}
}}
tooltip={{
label: "Go to main screen",
Expand All @@ -117,8 +126,8 @@ function UrlBar({ disabled }: { disabled?: boolean }) {
}}
recentItems={recentUrlList}
items={sortedUrlList}
value={urlList[0]?.id}
disabled={disabledAlsoWhenStarting || urlList.length < 2}
value={urlSelectValue}
disabled={disabledAlsoWhenStarting || urlList.length < (isExpoRouterProject ? 2 : 1)}
/>
</>
);
Expand Down
Loading