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

feat: cf #3872

Closed
wants to merge 22 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ webapp.xml

# IDE
.idea/
.env*.local
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"pnpm": {
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
},
"overrides": {
"eslint": "^7.32.0"
}
}
}
4 changes: 4 additions & 0 deletions packages/shared/src/components/CalendarHeatmap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const binsAttributes: React.SVGProps<SVGRectElement>[] = [
];

function getRange(count: number): number[] {
if (count <= 0) {
return [];
}

return Array.from(new Array(count), (_, i) => i);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/components/GrowthBookProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { apiUrl } from '../lib/config';
import { useRequestProtocol } from '../hooks/useRequestProtocol';
import { feature as localFeature, Feature } from '../lib/featureManagement';
import { useViewSize, ViewSize } from '../hooks/useViewSize';
import { withCredentials } from '../lib/withCredentials';

const ServerError = dynamic(
() => import(/* webpackChunkName: "serverError" */ './errors/ServerError'),
Expand Down Expand Up @@ -81,7 +82,7 @@ export const GrowthBookProvider = ({
experiment_id: data.experimentId,
variation_id: data.variationId,
}),
credentials: 'include',
credentials: withCredentials('include'),
headers: {
'content-type': 'application/json',
},
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/components/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import PromotionalBanner from './PromotionalBanner';
import useSidebarRendered from '../hooks/useSidebarRendered';
import LogContext from '../contexts/LogContext';
import SettingsContext from '../contexts/SettingsContext';
import Toast from './notifications/Toast';
import { useAuthErrors } from '../hooks/useAuthErrors';
import { useAuthVerificationRecovery } from '../hooks/useAuthVerificationRecovery';
import MainLayoutHeader, {
Expand Down Expand Up @@ -52,6 +51,10 @@ const Sidebar = dynamic(() =>
),
);

const Toast = dynamic(
() => import(/* webpackChunkName: "toast" */ './notifications/Toast'),
);

export interface MainLayoutProps
extends Omit<MainLayoutHeaderProps, 'onMobileSidebarToggle'>,
HTMLAttributes<HTMLDivElement> {
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/components/RenderMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { ReactElement, useEffect, useState } from 'react';
import classNames from 'classnames';
import { LightAsync as SyntaxHighlighterAsync } from 'react-syntax-highlighter';
import dynamic from 'next/dynamic';
import { ReactMarkdownOptions } from 'react-markdown/lib/react-markdown';
import type { ReactMarkdownOptions } from 'react-markdown/lib/react-markdown';
import styles from './markdown.module.css';
import {
Button,
Expand All @@ -21,6 +21,9 @@ import {

const ReactMarkdown = dynamic(
() => import(/* webpackChunkName: "reactMarkdown" */ 'react-markdown'),
{
ssr: false,
},
);

const SyntaxHighlighter = dynamic(() =>
Expand Down
3 changes: 1 addition & 2 deletions packages/shared/src/components/cards/common/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { HTMLAttributes, ReactNode } from 'react';
import React, { HTMLAttributes, ReactNode, ReactElement } from 'react';
import classNames from 'classnames';
import { ReactElement } from 'react-markdown/lib/react-markdown';
import styles from './Card.module.css';
import classed from '../../../lib/classed';
import { Post } from '../../../graphql/posts';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { HTMLAttributes, ReactNode } from 'react';
import React, { HTMLAttributes, ReactElement, ReactNode } from 'react';
import classNames from 'classnames';
import { ReactElement } from 'react-markdown/lib/react-markdown';
import classed from '../../../../lib/classed';
import { Image } from '../../../image/Image';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import classNames from 'classnames';
import React from 'react';
import { ReactElement } from 'react-markdown/lib/react-markdown';
import React, { ReactElement } from 'react';
import { Button } from '../buttons/Button';
import { ButtonVariant, ButtonIconPosition } from '../buttons/common';
import { ArrowIcon } from '../icons';
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/components/modals/NewSourceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ReputationAlert } from './ReputationAlert';
import { RequestKey } from '../../lib/query';
import { ProfileImageSize } from '../ProfilePicture';
import { gqlClient } from '../../graphql/common';
import { withCredentials } from '../../lib/withCredentials';

interface RSS {
url: string;
Expand Down Expand Up @@ -136,7 +137,7 @@ export default function NewSourceModal(props: ModalProps): ReactElement {
`${apiUrl}/scrape/source?url=${url}`,
20000,
{
credentials: 'same-origin',
credentials: withCredentials('same-origin'),
},
);
return res.json();
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/contexts/DndContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const DEFAULT_VALUE = {
onDndSettings: null,
};
const DndContext = React.createContext<DndContextData>(DEFAULT_VALUE);
const now = new Date();

interface DndContextProviderProps {
children: ReactNode;
Expand All @@ -35,6 +34,7 @@ interface DndContextProviderProps {
export const DndContextProvider = ({
children,
}: DndContextProviderProps): ReactElement => {
const now = new Date();
const [showDnd, setShowDnd] = useState(false);
const [dndSettings, setDndSettings] =
usePersistentContext<DndSettings>('dnd');
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/graphql/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QueryKey, UseInfiniteQueryOptions } from '@tanstack/react-query';
import { GraphQLError } from 'graphql-request/dist/types';
import type { PublicProfile, UserShortProfile } from '../lib/user';
import { graphqlUrl } from '../lib/config';
import { withCredentials } from '../lib/withCredentials';
// GraphQL Relay pagination types

export type ConnectionCursor = string;
Expand Down Expand Up @@ -140,7 +141,7 @@ export interface ResponseError {
}

export const gqlClient = new GraphQLClient(graphqlUrl, {
credentials: 'include',
credentials: withCredentials('include'),
fetch: globalThis.fetch,
});

Expand Down
5 changes: 3 additions & 2 deletions packages/shared/src/hooks/log/useLogQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MutableRefObject, useMemo, useRef } from 'react';
import { apiUrl } from '../../lib/config';
import useDebounceFn from '../useDebounceFn';
import { ExtensionMessageType } from '../../lib/extension';
import { withCredentials } from '../../lib/withCredentials';

export interface LogEvent extends Record<string, unknown> {
visit_id?: string;
Expand Down Expand Up @@ -37,7 +38,7 @@ export default function useLogQueue({
const res = await fetchMethod(LOG_ENDPOINT, {
method: 'POST',
body: JSON.stringify({ events }),
credentials: 'include',
credentials: withCredentials('include'),
headers: {
'content-type': 'application/json',
},
Expand Down Expand Up @@ -84,7 +85,7 @@ export default function useLogQueue({
type: ExtensionMessageType.FetchRequest,
args: {
body: JSON.stringify({ events }),
credentials: 'include',
credentials: withCredentials('include'),
method: 'POST',
headers: {
'content-type': 'application/json',
Expand Down
21 changes: 13 additions & 8 deletions packages/shared/src/hooks/profile/useActivityTimeFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ import { useViewSize, ViewSize } from '../useViewSize';
import SettingsContext from '../../contexts/SettingsContext';

const BASE_YEAR = 2018;
const currentYear = new Date().getFullYear();
const dropdownOptions = [
'Last year',
...Array.from(new Array(currentYear - BASE_YEAR + 1), (_, i) =>
(currentYear - i).toString(),
),
];

type UseActivityTimeFilterRet = {
selectedHistoryYear: number;
Expand All @@ -29,6 +22,18 @@ type UseActivityTimeFilterRet = {
};

export function useActivityTimeFilter(): UseActivityTimeFilterRet {
const dropdownOptions = useMemo(() => {
const currentYear = new Date().getFullYear();
const optionsLength = Math.max(currentYear - BASE_YEAR + 1, 0);

return [
'Last year',
...Array.from(new Array(optionsLength), (_, i) =>
(currentYear - i).toString(),
),
];
}, []);

const laptop = useViewSize(ViewSize.Laptop);
const laptopL = useViewSize(ViewSize.LaptopL);
const { sidebarExpanded } = useContext(SettingsContext);
Expand All @@ -47,7 +52,7 @@ export function useActivityTimeFilter(): UseActivityTimeFilterRet {
const startYear = new Date(selected, 0, 1);

return [addDays(endOfYear(startYear), 1), startYear];
}, [fullHistory, selectedHistoryYear]);
}, [fullHistory, selectedHistoryYear, dropdownOptions]);

return {
selectedHistoryYear,
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/hooks/useAutomation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useMutation,
} from '@tanstack/react-query';
import { HttpError } from '../lib/errors';
import { withCredentials } from '../lib/withCredentials';

export enum Automation {
Roaster = 'roaster',
Expand All @@ -28,7 +29,7 @@ export function useAutomation<
const url = `${process.env.NEXT_PUBLIC_API_URL}/auto/${name}`;
const res = await fetch(url, {
method: 'POST',
credentials: 'include',
credentials: withCredentials('include'),
headers: { 'content-type': 'application/json' },
body: JSON.stringify(vars),
keepalive: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/lib/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Squad } from '../graphql/sources';
import { decrypt } from '../components/crypto';
import { MarketingCta } from '../components/marketingCta/common';
import { Feed } from '../graphql/feed';
import { withCredentials } from './withCredentials';

interface NotificationsBootData {
unreadNotificationsCount: number;
Expand Down Expand Up @@ -94,7 +95,7 @@ export async function getBootData(app: string, url?: string): Promise<Boot> {

const res = await fetch(`${apiUrl}/boot${appRoute}?${params}`, {
method: 'GET',
credentials: 'include',
credentials: withCredentials('include'),
headers: { app, 'Content-Type': 'application/json' },
});
const result = await res.json();
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/lib/dynamicParent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React, {
ComponentClass,
FunctionComponent,
PropsWithChildren,
ReactElement,
ReactHTML,
ReactNode,
} from 'react';
import { ReactElement } from 'react-markdown/lib/react-markdown';

type LoaderResult<P> = ComponentClass<P> | FunctionComponent<P>;

Expand Down
17 changes: 9 additions & 8 deletions packages/shared/src/lib/kratos.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withCredentials } from './withCredentials';
import { authUrl, heimdallUrl } from './constants';

export type EmptyObjectLiteral = Record<string, never | string>;
Expand Down Expand Up @@ -221,7 +222,7 @@ export const initializeKratosFlow = async (
): Promise<InitializationData> => {
const search = new URLSearchParams(params);
const res = await fetch(`${authUrl}/self-service${flow}/browser?${search}`, {
credentials: 'include',
credentials: withCredentials('include'),
headers: { Accept: 'application/json' },
});

Expand All @@ -233,7 +234,7 @@ export const getKratosSettingsFlow = async (
id: string,
): Promise<InitializationData> => {
const res = await fetch(`${authUrl}/self-service${flow}/flows?id=${id}`, {
credentials: 'include',
credentials: withCredentials('include'),
headers: { Accept: 'application/json' },
});
return res.json();
Expand All @@ -244,15 +245,15 @@ export const getKratosFlow = async <T = InitializationData>(
id: string,
): Promise<T> => {
const res = await fetch(`${authUrl}/self-service${flow}?flow=${id}`, {
credentials: 'include',
credentials: withCredentials('include'),
headers: { Accept: 'application/json' },
});
return res.json();
};

export const getKratosError = async (id: string): Promise<ErrorData> => {
const res = await fetch(`${authUrl}/self-service/errors?id=${id}`, {
credentials: 'include',
credentials: withCredentials('include'),
headers: { Accept: 'application/json' },
});
return res.json();
Expand Down Expand Up @@ -296,7 +297,7 @@ export const getKratosProviders = async (
): Promise<KratosProviderData> => {
const search = flow ? new URLSearchParams({ flow }) : '';
const res = await fetch(`${heimdallUrl}/api/list_providers?${search}`, {
credentials: 'include',
credentials: withCredentials('include'),
method: flow ? 'GET' : 'POST',
});
return res.json();
Expand All @@ -313,7 +314,7 @@ export const submitKratosFlow = async <
}: KratosFormParams<T>): Promise<RequestResponse<R, E>> => {
const res = await fetch(action, {
method,
credentials: 'include',
credentials: withCredentials('include'),
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': params.csrf_token,
Expand Down Expand Up @@ -350,7 +351,7 @@ interface KratosSession {

export const getKratosSession = async (): Promise<KratosSession> => {
const res = await fetch(`${heimdallUrl}/api/whoami`, {
credentials: 'include',
credentials: withCredentials('include'),
});

if (res.status === 401) {
Expand All @@ -363,7 +364,7 @@ export const getKratosSession = async (): Promise<KratosSession> => {
export const getVerificationSession =
async (): Promise<VerificationSession> => {
const res = await fetch(`${heimdallUrl}/api/get_verification_flow`, {
credentials: 'include',
credentials: withCredentials('include'),
});

if (!res.ok) {
Expand Down
9 changes: 5 additions & 4 deletions packages/shared/src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import type { Company } from './userCompany';
import type { ContentPreference } from '../graphql/contentPreference';
import type { TopReader } from '../components/badges/TopReaderBadge';
import { withCredentials } from './withCredentials';

export enum Roles {
Moderator = 'moderator',
Expand Down Expand Up @@ -152,14 +153,14 @@ export async function logout(reason: string): Promise<void> {
const urlParams = reason ? `?${new URLSearchParams({ reason })}` : '';
await fetch(`${apiUrl}/v1/users/logout${urlParams}`, {
method: 'POST',
credentials: 'include',
credentials: withCredentials('include'),
});
}

export async function deleteAccount(): Promise<void> {
await fetch(`${apiUrl}/v1/users/me`, {
method: 'DELETE',
credentials: 'include',
credentials: withCredentials('include'),
});
}

Expand All @@ -175,7 +176,7 @@ const getProfileRequest = async (method = fetch, id: string) => {
id,
},
}),
credentials: 'include',
credentials: withCredentials('include'),
});
if (userRes.status === 404) {
throw new Error('not found');
Expand All @@ -200,7 +201,7 @@ const getProfileV2ExtraRequest = async (
id,
},
}),
credentials: 'include',
credentials: withCredentials('include'),
});
if (userRes.status === 404) {
throw new Error('not found');
Expand Down
Loading