Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 73d33df

Browse files
committed
frontend: fix compatibility session crash & simplify the redirect URI
1 parent 45e3fb0 commit 73d33df

File tree

4 files changed

+41
-128
lines changed

4 files changed

+41
-128
lines changed

frontend/src/components/CompatSession.tsx

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ import { FragmentType, graphql, useFragment } from "../gql";
2222

2323
import { Session } from "./Session";
2424

25-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
26-
const LOGIN_FRAGMENT = graphql(/* GraphQL */ `
27-
fragment CompatSession_sso_login on CompatSsoLogin {
28-
id
29-
redirectUri
30-
}
31-
`);
32-
3325
export const COMPAT_SESSION_FRAGMENT = graphql(/* GraphQL */ `
3426
fragment CompatSession_session on CompatSession {
3527
id
@@ -38,22 +30,11 @@ export const COMPAT_SESSION_FRAGMENT = graphql(/* GraphQL */ `
3830
finishedAt
3931
ssoLogin {
4032
id
41-
...CompatSession_sso_login
33+
redirectUri
4234
}
4335
}
4436
`);
4537

46-
type CompatSessionType = {
47-
id: string;
48-
deviceId: string;
49-
createdAt: string;
50-
finishedAt: string | null;
51-
ssoLogin: {
52-
id: string;
53-
redirectUri: string;
54-
};
55-
};
56-
5738
const END_SESSION_MUTATION = graphql(/* GraphQL */ `
5839
mutation EndCompatSession($id: ID!) {
5940
endCompatSession(input: { compatSessionId: $id }) {
@@ -78,14 +59,32 @@ const endCompatSessionFamily = atomFamily((id: string) => {
7859
return endCompatSessionAtom;
7960
});
8061

62+
const simplifyUrl = (url: string): string => {
63+
let parsed;
64+
try {
65+
parsed = new URL(url);
66+
} catch (e) {
67+
// Not a valid URL, return the original
68+
return url;
69+
}
70+
71+
// Clear out the search params and hash
72+
parsed.search = "";
73+
parsed.hash = "";
74+
75+
if (parsed.protocol === "https:") {
76+
return parsed.hostname;
77+
}
78+
79+
// Return the simplified URL
80+
return parsed.toString();
81+
};
82+
8183
const CompatSession: React.FC<{
8284
session: FragmentType<typeof COMPAT_SESSION_FRAGMENT>;
8385
}> = ({ session }) => {
8486
const [pending, startTransition] = useTransition();
85-
const data = useFragment(
86-
COMPAT_SESSION_FRAGMENT,
87-
session,
88-
) as CompatSessionType;
87+
const data = useFragment(COMPAT_SESSION_FRAGMENT, session);
8988
const endCompatSession = useSetAtom(endCompatSessionFamily(data.id));
9089

9190
const onSessionEnd = (): void => {
@@ -94,13 +93,17 @@ const CompatSession: React.FC<{
9493
});
9594
};
9695

96+
const clientName = data.ssoLogin?.redirectUri
97+
? simplifyUrl(data.ssoLogin.redirectUri)
98+
: undefined;
99+
97100
return (
98101
<Session
99102
id={data.id}
100103
name={data.deviceId}
101104
createdAt={data.createdAt}
102105
finishedAt={data.finishedAt || undefined}
103-
clientName={data.ssoLogin.redirectUri}
106+
clientName={clientName}
104107
>
105108
{!data.finishedAt && (
106109
<Button

frontend/src/components/__snapshots__/CompatSession.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ exports[`<CompatSession /> > renders a finished session 1`] = `
3535
<span
3636
className="_font-body-sm-semibold_1g2sj_49 _sessionMetadata_634806"
3737
>
38-
https://element.io
38+
element.io
3939
</span>
4040
</p>
4141
</div>
@@ -67,7 +67,7 @@ exports[`<CompatSession /> > renders an active session 1`] = `
6767
<span
6868
className="_font-body-sm-semibold_1g2sj_49 _sessionMetadata_634806"
6969
>
70-
https://element.io
70+
element.io
7171
</span>
7272
</p>
7373
<div

frontend/src/gql/gql.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ const documents = {
2323
types.EndBrowserSessionDocument,
2424
"\n query BrowserSessionList(\n $userId: ID!\n $state: BrowserSessionState\n $first: Int\n $after: String\n $last: Int\n $before: String\n ) {\n user(id: $userId) {\n id\n browserSessions(\n first: $first\n after: $after\n last: $last\n before: $before\n state: $state\n ) {\n totalCount\n\n edges {\n cursor\n node {\n id\n ...BrowserSession_session\n }\n }\n\n pageInfo {\n hasNextPage\n hasPreviousPage\n startCursor\n endCursor\n }\n }\n }\n }\n":
2525
types.BrowserSessionListDocument,
26-
"\n fragment CompatSession_sso_login on CompatSsoLogin {\n id\n redirectUri\n }\n":
27-
types.CompatSession_Sso_LoginFragmentDoc,
28-
"\n fragment CompatSession_session on CompatSession {\n id\n createdAt\n deviceId\n finishedAt\n ssoLogin {\n id\n ...CompatSession_sso_login\n }\n }\n":
26+
"\n fragment CompatSession_session on CompatSession {\n id\n createdAt\n deviceId\n finishedAt\n ssoLogin {\n id\n redirectUri\n }\n }\n":
2927
types.CompatSession_SessionFragmentDoc,
3028
"\n mutation EndCompatSession($id: ID!) {\n endCompatSession(input: { compatSessionId: $id }) {\n status\n compatSession {\n id\n finishedAt\n }\n }\n }\n":
3129
types.EndCompatSessionDocument,
@@ -123,14 +121,8 @@ export function graphql(
123121
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
124122
*/
125123
export function graphql(
126-
source: "\n fragment CompatSession_sso_login on CompatSsoLogin {\n id\n redirectUri\n }\n",
127-
): (typeof documents)["\n fragment CompatSession_sso_login on CompatSsoLogin {\n id\n redirectUri\n }\n"];
128-
/**
129-
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
130-
*/
131-
export function graphql(
132-
source: "\n fragment CompatSession_session on CompatSession {\n id\n createdAt\n deviceId\n finishedAt\n ssoLogin {\n id\n ...CompatSession_sso_login\n }\n }\n",
133-
): (typeof documents)["\n fragment CompatSession_session on CompatSession {\n id\n createdAt\n deviceId\n finishedAt\n ssoLogin {\n id\n ...CompatSession_sso_login\n }\n }\n"];
124+
source: "\n fragment CompatSession_session on CompatSession {\n id\n createdAt\n deviceId\n finishedAt\n ssoLogin {\n id\n redirectUri\n }\n }\n",
125+
): (typeof documents)["\n fragment CompatSession_session on CompatSession {\n id\n createdAt\n deviceId\n finishedAt\n ssoLogin {\n id\n redirectUri\n }\n }\n"];
134126
/**
135127
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
136128
*/

frontend/src/gql/graphql.ts

Lines changed: 8 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,25 +1001,17 @@ export type BrowserSessionListQuery = {
10011001
} | null;
10021002
};
10031003

1004-
export type CompatSession_Sso_LoginFragment = {
1005-
__typename?: "CompatSsoLogin";
1006-
id: string;
1007-
redirectUri: any;
1008-
} & { " $fragmentName"?: "CompatSession_Sso_LoginFragment" };
1009-
10101004
export type CompatSession_SessionFragment = {
10111005
__typename?: "CompatSession";
10121006
id: string;
10131007
createdAt: any;
10141008
deviceId: string;
10151009
finishedAt?: any | null;
1016-
ssoLogin?:
1017-
| ({ __typename?: "CompatSsoLogin"; id: string } & {
1018-
" $fragmentRefs"?: {
1019-
CompatSession_Sso_LoginFragment: CompatSession_Sso_LoginFragment;
1020-
};
1021-
})
1022-
| null;
1010+
ssoLogin?: {
1011+
__typename?: "CompatSsoLogin";
1012+
id: string;
1013+
redirectUri: any;
1014+
} | null;
10231015
} & { " $fragmentName"?: "CompatSession_SessionFragment" };
10241016

10251017
export type EndCompatSessionMutationVariables = Exact<{
@@ -1496,26 +1488,6 @@ export const BrowserSession_SessionFragmentDoc = {
14961488
},
14971489
],
14981490
} as unknown as DocumentNode<BrowserSession_SessionFragment, unknown>;
1499-
export const CompatSession_Sso_LoginFragmentDoc = {
1500-
kind: "Document",
1501-
definitions: [
1502-
{
1503-
kind: "FragmentDefinition",
1504-
name: { kind: "Name", value: "CompatSession_sso_login" },
1505-
typeCondition: {
1506-
kind: "NamedType",
1507-
name: { kind: "Name", value: "CompatSsoLogin" },
1508-
},
1509-
selectionSet: {
1510-
kind: "SelectionSet",
1511-
selections: [
1512-
{ kind: "Field", name: { kind: "Name", value: "id" } },
1513-
{ kind: "Field", name: { kind: "Name", value: "redirectUri" } },
1514-
],
1515-
},
1516-
},
1517-
],
1518-
} as unknown as DocumentNode<CompatSession_Sso_LoginFragment, unknown>;
15191491
export const CompatSession_SessionFragmentDoc = {
15201492
kind: "Document",
15211493
definitions: [
@@ -1540,31 +1512,13 @@ export const CompatSession_SessionFragmentDoc = {
15401512
kind: "SelectionSet",
15411513
selections: [
15421514
{ kind: "Field", name: { kind: "Name", value: "id" } },
1543-
{
1544-
kind: "FragmentSpread",
1545-
name: { kind: "Name", value: "CompatSession_sso_login" },
1546-
},
1515+
{ kind: "Field", name: { kind: "Name", value: "redirectUri" } },
15471516
],
15481517
},
15491518
},
15501519
],
15511520
},
15521521
},
1553-
{
1554-
kind: "FragmentDefinition",
1555-
name: { kind: "Name", value: "CompatSession_sso_login" },
1556-
typeCondition: {
1557-
kind: "NamedType",
1558-
name: { kind: "Name", value: "CompatSsoLogin" },
1559-
},
1560-
selectionSet: {
1561-
kind: "SelectionSet",
1562-
selections: [
1563-
{ kind: "Field", name: { kind: "Name", value: "id" } },
1564-
{ kind: "Field", name: { kind: "Name", value: "redirectUri" } },
1565-
],
1566-
},
1567-
},
15681522
],
15691523
} as unknown as DocumentNode<CompatSession_SessionFragment, unknown>;
15701524
export const OAuth2Session_SessionFragmentDoc = {
@@ -2526,21 +2480,6 @@ export const CompatSessionListDocument = {
25262480
],
25272481
},
25282482
},
2529-
{
2530-
kind: "FragmentDefinition",
2531-
name: { kind: "Name", value: "CompatSession_sso_login" },
2532-
typeCondition: {
2533-
kind: "NamedType",
2534-
name: { kind: "Name", value: "CompatSsoLogin" },
2535-
},
2536-
selectionSet: {
2537-
kind: "SelectionSet",
2538-
selections: [
2539-
{ kind: "Field", name: { kind: "Name", value: "id" } },
2540-
{ kind: "Field", name: { kind: "Name", value: "redirectUri" } },
2541-
],
2542-
},
2543-
},
25442483
{
25452484
kind: "FragmentDefinition",
25462485
name: { kind: "Name", value: "CompatSession_session" },
@@ -2562,10 +2501,7 @@ export const CompatSessionListDocument = {
25622501
kind: "SelectionSet",
25632502
selections: [
25642503
{ kind: "Field", name: { kind: "Name", value: "id" } },
2565-
{
2566-
kind: "FragmentSpread",
2567-
name: { kind: "Name", value: "CompatSession_sso_login" },
2568-
},
2504+
{ kind: "Field", name: { kind: "Name", value: "redirectUri" } },
25692505
],
25702506
},
25712507
},
@@ -2988,21 +2924,6 @@ export const SessionQueryDocument = {
29882924
],
29892925
},
29902926
},
2991-
{
2992-
kind: "FragmentDefinition",
2993-
name: { kind: "Name", value: "CompatSession_sso_login" },
2994-
typeCondition: {
2995-
kind: "NamedType",
2996-
name: { kind: "Name", value: "CompatSsoLogin" },
2997-
},
2998-
selectionSet: {
2999-
kind: "SelectionSet",
3000-
selections: [
3001-
{ kind: "Field", name: { kind: "Name", value: "id" } },
3002-
{ kind: "Field", name: { kind: "Name", value: "redirectUri" } },
3003-
],
3004-
},
3005-
},
30062927
{
30072928
kind: "FragmentDefinition",
30082929
name: { kind: "Name", value: "CompatSession_session" },
@@ -3024,10 +2945,7 @@ export const SessionQueryDocument = {
30242945
kind: "SelectionSet",
30252946
selections: [
30262947
{ kind: "Field", name: { kind: "Name", value: "id" } },
3027-
{
3028-
kind: "FragmentSpread",
3029-
name: { kind: "Name", value: "CompatSession_sso_login" },
3030-
},
2948+
{ kind: "Field", name: { kind: "Name", value: "redirectUri" } },
30312949
],
30322950
},
30332951
},

0 commit comments

Comments
 (0)