Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

refactor: Redirects improvements (#2992)

Oleg Isonena3e1d18f1030
diff --git a/apps/builder/app/builder/features/project-settings/project-settings.stories.tsx b/apps/builder/app/builder/features/project-settings/project-settings.stories.tsx
index e0f0b5425c..9f370c4015 100644
--- a/apps/builder/app/builder/features/project-settings/project-settings.stories.tsx
+++ b/apps/builder/app/builder/features/project-settings/project-settings.stories.tsx
@@ -1,9 +1,10 @@
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import { $isProjectSettingsOpen } from "~/shared/nano-states/seo";
-import { ProjectSettings } from "./project-settings";
+import { ProjectSettingsView } from "./project-settings";
+import { $pages } from "~/shared/nano-states";
export default {
- component: ProjectSettings,
+ component: ProjectSettingsView,
};
$isProjectSettingsOpen.set(true);
@@ -16,7 +17,38 @@ const createRouter = (element: JSX.Element) =>
},
]);
-export const ProjectSettingsExample = () => {
- const router = createRouter(<ProjectSettings />);
+export const General = () => {
+ const router = createRouter(
+ <ProjectSettingsView currentSection="General" isOpen />
+ );
+ return <RouterProvider router={router} />;
+};
+
+export const Redirects = () => {
+ $pages.set({
+ homePage: {
+ id: "pageId",
+ name: "My Name",
+ path: "/",
+ title: `"My Title"`,
+ meta: {},
+ rootInstanceId: "body",
+ },
+ pages: [],
+ folders: [],
+ redirects: [
+ { old: "/old", new: "/new" },
+ { old: "/old", new: "https://google.com" },
+ {
+ old: "/oldddddddddddd/ddddddddddd/dddddddd/dddddd",
+ new: "https://gooooooooooooooooooooooooooooooooooooogle.com",
+ status: "302",
+ },
+ ],
+ });
+
+ const router = createRouter(
+ <ProjectSettingsView currentSection="Redirects" isOpen />
+ );
return <RouterProvider router={router} />;
};
diff --git a/apps/builder/app/builder/features/project-settings/project-settings.tsx b/apps/builder/app/builder/features/project-settings/project-settings.tsx
index 97acef5e11..5b6882cece 100644
--- a/apps/builder/app/builder/features/project-settings/project-settings.tsx
+++ b/apps/builder/app/builder/features/project-settings/project-settings.tsx
@@ -21,26 +21,28 @@ import { SectionMarketplace } from "./section-marketplace";
const focusOutline = focusRingStyle();
-const settingNames = ["General", "Redirects"];
+const sectionNames = ["General", "Redirects"];
if (isFeatureEnabled("marketplace")) {
- settingNames.push("Marketplace");
+ sectionNames.push("Marketplace");
}
+type SectionName = (typeof sectionNames)[number];
+
const leftPanelWidth = theme.spacing[26];
const rightPanelWidth = theme.spacing[35];
-const ProjectSettingsView = ({
+export const ProjectSettingsView = ({
+ currentSection,
+ onSectionChange,
isOpen,
onOpenChange,
}: {
+ currentSection: SectionName;
+ onSectionChange?: (section: SectionName) => void;
isOpen: boolean;
- onOpenChange: (isOpen: boolean) => void;
+ onOpenChange?: (isOpen: boolean) => void;
}) => {
- const [selectedSetting, setSelectedSetting] = useState<
- (typeof settingNames)[number]
- >(settingNames[0]);
-
return (
<Dialog open={isOpen} onOpenChange={onOpenChange}>
<DialogContent
@@ -64,15 +66,15 @@ const ProjectSettingsView = ({
borderRight: `1px solid ${theme.colors.borderMain}`,
}}
>
- {settingNames.map((name, index) => {
+ {sectionNames.map((name, index) => {
return (
<ListItem
- current={selectedSetting === name}
+ current={currentSection === name}
asChild
index={index}
key={name}
onSelect={() => {
- setSelectedSetting(name);
+ onSectionChange?.(name);
}}
>
<Flex
@@ -101,9 +103,9 @@ const ProjectSettingsView = ({
</List>
<ScrollArea>
<Grid gap={2} css={{ my: theme.spacing[5] }}>
- {selectedSetting === "General" && <SectionGeneral />}
- {selectedSetting === "Redirects" && <SectionRedirects />}
- {selectedSetting === "Marketplace" &&
+ {currentSection === "General" && <SectionGeneral />}
+ {currentSection === "Redirects" && <SectionRedirects />}
+ {currentSection === "Marketplace" &&
isFeatureEnabled("marketplace") && <SectionMarketplace />}
<div />
</Grid>
@@ -120,10 +122,15 @@ const ProjectSettingsView = ({
export const ProjectSettings = () => {
const isOpen = useStore($isProjectSettingsOpen);
+ const [currentSection, setCurrentSection] = useState<SectionName>(
+ sectionNames[0]
+ );
return (
<ProjectSettingsView
isOpen={isOpen}
+ currentSection={currentSection}
+ onSectionChange={setCurrentSection}
onOpenChange={$isProjectSettingsOpen.set}
/>
);
diff --git a/apps/builder/app/builder/features/project-settings/section-redirects.tsx b/apps/builder/app/builder/features/project-settings/section-redirects.tsx
index e7d640ada2..8e25881ee7 100644
--- a/apps/builder/app/builder/features/project-settings/section-redirects.tsx
+++ b/apps/builder/app/builder/features/project-settings/section-redirects.tsx
@@ -10,6 +10,9 @@ import {
SmallIconButton,
InputErrorsTooltip,
Select,
+ Link,
+ truncate,
+ Tooltip,
} from "@webstudio-is/design-system";
import { ArrowRightIcon, TrashIcon } from "@webstudio-is/icons";
import { useState, type ChangeEvent, useRef } from "react";
@@ -20,9 +23,10 @@ import {
} from "@webstudio-is/sdk";
import { useStore } from "@nanostores/react";
import { getExistingRoutePaths } from "../sidebar-left/panels/pages/page-utils";
-import { $pages } from "~/shared/nano-states";
+import { $pages, $project } from "~/shared/nano-states";
import { serverSyncStore } from "~/shared/sync";
import { flushSync } from "react-dom";
+import { getPublishedUrl } from "~/shared/router-utils";
export const SectionRedirects = () => {
const [redirects, setRedirects] = useState(
@@ -37,6 +41,8 @@ export const SectionRedirects = () => {
const pages = useStore($pages);
const existingPaths = getExistingRoutePaths(pages);
const oldPathRef = useRef<HTMLInputElement>(null);
+ const projectData = useStore($project);
+ const publishedUrl = new URL(getPublishedUrl(projectData?.domain ?? ""));
const redirectKeys = Object.keys(redirects);
const isValidRedirects =
@@ -229,16 +235,42 @@ export const SectionRedirects = () => {
overflow: "hidden",
}}
>
- <Flex gap="2">
- <Text>
- {redirect.old}, {redirect.status ?? "301"}
- </Text>
- <ArrowRightIcon size={16} />
- <Text truncate>{redirect.new}</Text>
+ <Flex gap="2" align="center">
+ <Tooltip content={redirect.old}>
+ <Link
+ underline="hover"
+ href={new URL(
+ redirect.old,
+ publishedUrl
+ ).toString()}
+ css={truncate()}
+ target="_blank"
+ >
+ {redirect.old}
+ </Link>
+ </Tooltip>
+ <Text>{redirect.status ?? "301"}</Text>
+ <Flex shrink={false} align="center">
+ <ArrowRightIcon size={16} aria-disabled />
+ </Flex>
+ <Tooltip content={redirect.new}>
+ <Link
+ underline="hover"
+ href={new URL(
+ redirect.new,
+ publishedUrl
+ ).toString()}
+ css={truncate()}
+ target="_blank"
+ >
+ {redirect.new}
+ </Link>
+ </Tooltip>
</Flex>
<SmallIconButton
variant="destructive"
icon={<TrashIcon />}
+ aria-label={`Delete redirect from ${redirect.old}`}
onClick={() => handleDeleteRedirect(index)}
/>
</Flex>
diff --git a/packages/design-system/src/utilities.ts b/packages/design-system/src/utilities.ts
index 2318c1322e..0b9de81eb6 100644
--- a/packages/design-system/src/utilities.ts
+++ b/packages/design-system/src/utilities.ts
@@ -1,5 +1,6 @@
import { useEffect, useMemo, useRef } from "react";
import warnOnce from "warn-once";
+import type { CSS } from "./stitches.config";
export const canvasPointerEventsPropertyName = "--canvas-pointer-events";
@@ -82,7 +83,7 @@ export const useDisableCanvasPointerEvents = () => {
return enableDisable;
};
-export const truncate = () => ({
+export const truncate = (): CSS => ({
whiteSpace: "nowrap",
textOverflow: "ellipsis",
overflow: "hidden",