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
33 changes: 32 additions & 1 deletion packages/webapp/__tests__/SquadFeedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
} from '@dailydotdev/shared/src/graphql/squads';
import {
type SourceMember,
SourceType,
type Squad,
SourceMemberRole,
SourcePermissions,
Expand All @@ -57,8 +58,9 @@ import {
CONTENT_PREFERENCE_STATUS_QUERY,
ContentPreferenceType,
} from '@dailydotdev/shared/src/graphql/contentPreference';
import { gqlClient } from '@dailydotdev/shared/src/graphql/common';
import { useSquad } from '@dailydotdev/shared/src/hooks/squads/useSquad';
import SquadPage from '../pages/squads/[handle]';
import SquadPage, { getServerSideProps } from '../pages/squads/[handle]';

const defaultSquad: Squad = {
...generateTestSquad(),
Expand Down Expand Up @@ -259,6 +261,35 @@ const renderComponent = (
};

describe('squad page', () => {
it('should redirect machine sources to the canonical sources route', async () => {
jest.spyOn(gqlClient, 'request').mockResolvedValueOnce({
source: {
id: 'daily',
name: 'daily.dev',
image: 'https://daily.dev/daily.png',
handle: 'daily',
permalink: 'https://app.daily.dev/sources/daily',
type: SourceType.Machine,
public: true,
},
} as Awaited<ReturnType<typeof gqlClient.request>>);

const setHeader = jest.fn();
const result = await getServerSideProps({
params: { handle: 'daily' },
query: {},
res: { setHeader },
} as never);

expect(setHeader).toHaveBeenCalled();
expect(result).toEqual({
redirect: {
destination: '/sources/daily',
permanent: false,
},
});
});

it('should request source feed', async () => {
renderComponent();
await waitForNock();
Expand Down
35 changes: 34 additions & 1 deletion packages/webapp/pages/squads/[handle]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ import {
} from '@dailydotdev/shared/src/graphql/squads';
import type {
BasicSourceMember,
SourceData,
Squad,
} from '@dailydotdev/shared/src/graphql/sources';
import {
isSourceUserSource,
SOURCE_QUERY,
SourceType,
} from '@dailydotdev/shared/src/graphql/sources';
import Unauthorized from '@dailydotdev/shared/src/components/errors/Unauthorized';
import { useQuery } from '@tanstack/react-query';
import {
Expand Down Expand Up @@ -496,6 +502,32 @@ export async function getServerSideProps({
};

try {
const sourceResult = await gqlClient.request<SourceData>(SOURCE_QUERY, {
id: handle,
});

if (isSourceUserSource(sourceResult.source)) {
setCacheHeader();

return {
redirect: {
destination: `/${sourceResult.source.id}`,
permanent: false,
},
};
}

if (sourceResult.source?.type === SourceType.Machine) {
setCacheHeader();

return {
redirect: {
destination: `/sources/${handle}`,
permanent: false,
},
};
}

const referringUserPromise =
userId && campaign
? gqlClient
Expand Down Expand Up @@ -553,8 +585,9 @@ export async function getServerSideProps({
} catch (err) {
const clientError = err as ClientError;
const errors = Object.values(ApiError);
const errorCode = clientError?.response?.errors?.[0]?.extensions?.code;

if (errors.includes(clientError?.response?.errors?.[0]?.extensions?.code)) {
if (errors.includes(errorCode)) {
setCacheHeader();

return {
Expand Down
Loading