Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ui/src/error-boundary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ErrorBoundary as SentryErrorBoundary } from "@sentry/react";
import { FC, PropsWithChildren } from "react";

import { Grid } from "../grid";
import { Link } from "../link";
import { Link } from "../link-factory";
import { Typography } from "../typography";

type ErrorBoundaryProps = PropsWithChildren<{
Expand Down
22 changes: 22 additions & 0 deletions packages/ui/src/link-factory/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { FC, HTMLProps } from "react";
import { Link as ReactRouterLink, LinkProps } from "react-router-dom";

export const linkFactory =
(getLanguageCode: () => string, defaultLanguageCode: string): FC<HTMLProps<HTMLAnchorElement>> =>
// eslint-disable-next-line react/display-name
(props) => {
const languageCode = getLanguageCode();

if (props.href && (props.href.startsWith("/") || props.href.startsWith(location.origin))) {
return (
<ReactRouterLink
{...(props as LinkProps)}
to={defaultLanguageCode === languageCode ? props.href : `/${languageCode}${props.href}`}
/>
);
} else {
return <a {...props} />;
}
};

export const Link = linkFactory(() => "en", "en");
10 changes: 0 additions & 10 deletions packages/ui/src/link/index.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion web/src/apps/main/components/authors/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import Typography from "@material-ui/core/Typography";
import Skeleton from "@material-ui/lab/Skeleton";
import { FC } from "react";
import { useDispatch } from "react-redux";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { Dispatch } from "src/apps/main/redux";
import { fetchCurrentArticleAuthors } from "src/apps/main/redux/actions/articles-page";
import { ArticlesPageState } from "src/apps/main/redux/reducers/articles-page";
import { LinkV2 } from "src/components/link-v2";

const useStyles = makeStyles((theme) => ({
avatarsContainer: {
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/components/card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import makeStyles from "@material-ui/core/styles/makeStyles";
import Typography from "@material-ui/core/Typography";
import Skeleton from "@material-ui/lab/Skeleton";
import { FC } from "react";
import { LinkV2 } from "src/components/link-v2";
import { LinkV2 } from "src/apps/main/components/link-v2";

interface CardInfo {
image: string;
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/components/contributors/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import Typography from "@material-ui/core/Typography";
import Skeleton from "@material-ui/lab/Skeleton";
import { FC } from "react";
import { useDispatch } from "react-redux";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { Dispatch } from "src/apps/main/redux";
import { fetchCurrentArticleContributors } from "src/apps/main/redux/actions/articles-page";
import { ArticlesPageState } from "src/apps/main/redux/reducers/articles-page";
import { LinkV2 } from "src/components/link-v2";

const useStyles = makeStyles((theme) => ({
avatarsContainer: {
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/components/footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { makeStyles } from "@material-ui/core/styles";
import Typography from "@material-ui/core/Typography";
import { FC } from "react";
import { useSelector } from "react-redux";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { T } from "src/apps/main/components/t";
import { StateInterface } from "src/apps/main/redux";
import { FooterComponentState } from "src/apps/main/redux/reducers/footer-component";
import { LinkV2 } from "src/components/link-v2";

const useStyles = makeStyles((theme) => ({
root: {
Expand Down
5 changes: 5 additions & 0 deletions web/src/apps/main/components/link-v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { linkFactory } from "@dzcode.io/ui/dist/link-factory";

import { mainStore } from "../../redux";

export const LinkV2 = linkFactory(() => mainStore.getState().settings.language.code, "en");
2 changes: 1 addition & 1 deletion web/src/apps/main/components/markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useSelector } from "react-redux";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import prism from "react-syntax-highlighter/dist/cjs/styles/prism/prism";
import tomorrow from "react-syntax-highlighter/dist/cjs/styles/prism/tomorrow";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { StateInterface } from "src/apps/main/redux";
import { LinkV2 } from "src/components/link-v2";

export const Markdown: FC<ReactMarkdown> = ({ children, t, ...markdownProps }) => {
const theme = useTheme();
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/components/navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { useScrollPosition } from "@n8tb1t/use-scroll-position";
import { FC, Fragment, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { animated, useSpring } from "react-spring";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { T } from "src/apps/main/components/t";
import { Dispatch, StateInterface } from "src/apps/main/redux";
import { SettingsState } from "src/apps/main/redux/reducers/settings";
import logo from "src/assets/svg/logo-wide.svg";
import { LinkV2 } from "src/components/link-v2";

import { IOSSwitch } from "./ios-switch";
import { LanguageSwitch } from "./lang-switch";
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/components/sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import SpeedDialIcon from "@material-ui/lab/SpeedDialIcon";
import TreeItem from "@material-ui/lab/TreeItem";
import TreeView from "@material-ui/lab/TreeView";
import { FC, Fragment } from "react";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { SpeedDial } from "src/apps/main/components/speed-dial";
import { SidebarTreeItem } from "src/apps/main/types";
import { LinkV2 } from "src/components/link-v2";

const useStyles = makeStyles((theme) =>
createStyles({
Expand Down
39 changes: 31 additions & 8 deletions web/src/apps/main/entry/app.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import "./style.scss";

import { allLanguages, LanguageEntity } from "@dzcode.io/models/dist/language";
import { ErrorBoundary } from "@dzcode.io/ui/dist/error-boundary";
import Container from "@material-ui/core/Container";
import { ComponentType, FC, lazy, Suspense, useEffect } from "react";
import { Route, RouteProps, Switch, useLocation } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { Route, RouteProps, Switch, useLocation, useRouteMatch } from "react-router-dom";
import { Footer } from "src/apps/main/components/footer";
import { Navbar } from "src/apps/main/components/navbar";
import { Theme } from "src/apps/main/components/theme";
import { getEnv } from "src/common/utils";
import { urlLanguageRegEx } from "src/common/utils/language";
import { Loading } from "src/components/loading";

import { Dispatch, StateInterface } from "../redux";
import { SettingsState } from "../redux/reducers/settings";

interface RouteInterface extends RouteProps {
import: Promise<{ default: ComponentType }>;
}
Expand Down Expand Up @@ -50,17 +56,29 @@ const routes: RouteInterface[] = [
];

export const App: FC = () => {
if (getEnv() !== "development") {
const location = useLocation();
const location = useLocation();
const match = useRouteMatch<{ lang?: LanguageEntity["code"] }>(urlLanguageRegEx);
const { language } = useSelector<StateInterface, SettingsState>((state) => state.settings);
const dispatch = useDispatch<Dispatch<SettingsState>>();

useEffect(() => {
useEffect(() => {
if (getEnv() !== "development") {
if (window.ga) {
window.ga("set", "page", location.pathname);
window.ga("send", "pageview");
window.fbq("track", "PageView");
}
}, [location]);
}
}

const urlLanguage =
allLanguages.find(({ code }) => code === match?.params.lang) || allLanguages[0];
if (urlLanguage.code !== language.code) {
dispatch({
type: "UPDATE_SETTINGS",
payload: { language: urlLanguage },
});
}
}, [location]);

return (
<Theme>
Expand All @@ -76,8 +94,13 @@ export const App: FC = () => {
<Container maxWidth="lg" style={{ paddingTop: "130px" }}>
<Suspense fallback={<Loading />}>
<Switch>
{routes.map((route, index) => (
<Route {...route} key={`route-${index}`} component={lazy(() => route.import)} />
{routes.map(({ import: im, path, ...route }, index) => (
<Route
{...route}
path={path ? `${urlLanguageRegEx}${path}` : undefined}
key={`route-${index}`}
component={lazy(() => im)}
/>
))}
</Switch>
</Suspense>
Expand Down
5 changes: 3 additions & 2 deletions web/src/apps/main/entry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import * as Sentry from "@sentry/browser";
import { Integrations } from "@sentry/tracing";
import { render } from "react-dom";
import { Provider } from "react-redux";
import { BrowserRouter as Router } from "react-router-dom";
import { Router } from "react-router-dom";
import { mainStore } from "src/apps/main/redux";
import { getEnv } from "src/common/utils";
import { history } from "src/common/utils/history";

import { App } from "./app";

Expand All @@ -23,7 +24,7 @@ if (env !== "development") {

render(
<Provider store={mainStore}>
<Router>
<Router history={history}>
<App />
</Router>
</Provider>,
Expand Down
10 changes: 7 additions & 3 deletions web/src/apps/main/pages/articles/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const ArticlesPage: FC = () => {
dispatch(fetchArticlesList());
}, []);

const { path } = useRouteMatch();
const { path: pathRegex } = useRouteMatch();
const path = "/Articles";
const loadedCurrentArticle = isLoaded(currentArticle);

return (
Expand Down Expand Up @@ -53,10 +54,13 @@ export const ArticlesPage: FC = () => {
<Grid item xs md={7}>
<Route
exact
path={`${path}`}
path={pathRegex}
render={() => <Landing onShowSidebar={() => setOpen(true)} />}
/>
<Route path={`${path}/:articleSlug`} render={() => <Content key={location.pathname} />} />
<Route
path={`${pathRegex}/:articleSlug`}
render={() => <Content key={location.pathname} />}
/>
</Grid>
</Grid>
</ErrorBoundary>
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/pages/contribute/contributions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import QuestionAnswerIcon from "@material-ui/icons/QuestionAnswer";
import Skeleton from "@material-ui/lab/Skeleton";
import { FC } from "react";
import { useDispatch, useSelector } from "react-redux";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { t } from "src/apps/main/components/t";
import { Dispatch, StateInterface } from "src/apps/main/redux";
import { fetchContributions, updateFilterValue } from "src/apps/main/redux/actions/contribute-page";
import { ContributePageState } from "src/apps/main/redux/reducers/contribute-page";
import { SettingsState } from "src/apps/main/redux/reducers/settings";
import { elapsedTime } from "src/common/utils/elapsed-time";
import { LinkV2 } from "src/components/link-v2";

const useStyles = makeStyles((theme) => ({
root: {
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/pages/landing/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import makeStyles from "@material-ui/core/styles/makeStyles";
import Typography from "@material-ui/core/Typography";
import { FC } from "react";
import image from "src/apps/main/assets/svg/dzcode.svg";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { Markdown } from "src/apps/main/components/markdown";
import { T, t } from "src/apps/main/components/t";
import { LinkV2 } from "src/components/link-v2";

export const Header: FC = () => {
const useStyles = makeStyles((theme) => ({
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/pages/landing/mobile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import androidDark from "src/apps/main/assets/png/android-dark.png";
import androidLight from "src/apps/main/assets/png/android-light.png";
import iosDark from "src/apps/main/assets/png/ios-dark.png";
import iosLight from "src/apps/main/assets/png/ios-light.png";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { T } from "src/apps/main/components/t";
import { StateInterface } from "src/apps/main/redux";
import { LinkV2 } from "src/components/link-v2";
import { fullstackConfig } from "src/config";

const useStyles = makeStyles((theme) => ({
Expand Down
8 changes: 5 additions & 3 deletions web/src/apps/main/pages/learn/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const LearnPage: FC = () => {
dispatch(fetchDocumentationList());
}, []);

const { path } = useRouteMatch();
const { path: pathRegex } = useRouteMatch();
const path = "/Learn";

const loadedCurrentDocument = isLoaded(currentDocument);

return (
Expand Down Expand Up @@ -54,11 +56,11 @@ export const LearnPage: FC = () => {
<Grid item xs md={7}>
<Route
exact
path={`${path}`}
path={pathRegex}
render={() => <Landing onShowSidebar={() => setOpen(true)} />}
/>
<Route
path={`${path}/:documentSlug`}
path={`${pathRegex}/:documentSlug`}
render={() => <Content key={location.pathname} />}
/>
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/pages/not-found/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import ArrowBackIcon from "@material-ui/icons/ArrowBack";
import ArrowForwardIcon from "@material-ui/icons/ArrowForward";
import { FC } from "react";
import { useSelector } from "react-redux";
import { LinkV2 } from "src/apps/main/components/link-v2";
import { T, t } from "src/apps/main/components/t";
import svg from "src/assets/svg/404.svg";
import { LinkV2 } from "src/components/link-v2";

import { Markdown } from "../../components/markdown";
import { StateInterface } from "../../redux";
Expand Down
12 changes: 9 additions & 3 deletions web/src/apps/main/redux/actions/articles-page/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Article } from "@dzcode.io/api/dist/app/types/legacy";
import { LanguageEntity } from "@dzcode.io/models/dist/language";
import { isLoaded } from "@dzcode.io/utils/dist/loadable";
import * as Sentry from "@sentry/browser";
import { listToTree } from "l2t";
import { matchPath } from "react-router-dom";
import { ThunkResult } from "src/apps/main/redux";
import { ArticlesState } from "src/apps/main/redux/reducers/articles";
import { ArticlesPageState } from "src/apps/main/redux/reducers/articles-page";
import { SidebarTreeItem } from "src/apps/main/types";
import { hasInCollection } from "src/common/utils";
import { fetchV2 } from "src/common/utils/fetch";
import { history } from "src/common/utils/history";
import { urlLanguageRegEx } from "src/common/utils/language";

/**
* Fetches the list of articles for the sidebar
Expand Down Expand Up @@ -164,9 +168,11 @@ export const fetchCurrentArticleAuthors =
*/
export const fetchCurrentArticle =
(): ThunkResult<ArticlesPageState | ArticlesState> => async (dispatch, getState) => {
const slug = location.pathname
.substring(location.pathname.indexOf("/", 1) + 1)
.replace(/\/$/, "");
const match = matchPath<{ lang?: LanguageEntity["code"]; slug: string }>(
history.location.pathname,
{ path: `${urlLanguageRegEx}/Articles/:slug(.*)` },
);
const slug = match?.params.slug || "";

const cashedArticle = hasInCollection<Article>(getState().articles.list, "slug", slug, [
["content"],
Expand Down
12 changes: 9 additions & 3 deletions web/src/apps/main/redux/actions/documentation-page/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Document } from "@dzcode.io/api/dist/app/types/legacy";
import { LanguageEntity } from "@dzcode.io/models/dist/language";
import { isLoaded } from "@dzcode.io/utils/dist/loadable";
import * as Sentry from "@sentry/browser";
import { listToTree } from "l2t";
import { matchPath } from "react-router-dom";
import { ThunkResult } from "src/apps/main/redux";
import { DocumentationState } from "src/apps/main/redux/reducers/documentation";
import { LearnPageState } from "src/apps/main/redux/reducers/learn-page";
import { SidebarTreeItem } from "src/apps/main/types";
import { hasInCollection } from "src/common/utils";
import { fetchV2 } from "src/common/utils/fetch";
import { history } from "src/common/utils/history";
import { urlLanguageRegEx } from "src/common/utils/language";

/**
* Fetches the list of documents for the sidebar
Expand Down Expand Up @@ -163,9 +167,11 @@ const fetchCurrentDocumentAuthors =
*/
export const fetchCurrentDocument =
(): ThunkResult<LearnPageState | DocumentationState> => async (dispatch, getState) => {
const slug = location.pathname
.substring(location.pathname.indexOf("/", 1) + 1)
.replace(/\/$/, "");
const match = matchPath<{ lang?: LanguageEntity["code"]; slug: string }>(
history.location.pathname,
{ path: `${urlLanguageRegEx}/Learn/:slug(.*)` },
);
const slug = match?.params.slug || "";
const cashedDocument = hasInCollection<Document>(getState().documentation.list, "slug", slug, [
["content"],
]);
Expand Down
Loading