Skip to content

Commit 60d612d

Browse files
authored
Extract NewCommitsToggle and WhitelistedTeams components (#785)
1 parent bdc7818 commit 60d612d

File tree

4 files changed

+152
-128
lines changed

4 files changed

+152
-128
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import styled from "@emotion/styled";
2+
import { observer } from "mobx-react-lite";
3+
import React from "react";
4+
5+
export interface NewCommitsToggleProps {
6+
toggled: boolean;
7+
onToggle(): void;
8+
}
9+
10+
export const NewCommitsToggle = observer((props: NewCommitsToggleProps) => {
11+
return (
12+
<Container>
13+
<NewCommitsCheckbox
14+
type="checkbox"
15+
checked={props.toggled}
16+
onChange={props.onToggle}
17+
/>
18+
Include new commits
19+
</Container>
20+
);
21+
});
22+
23+
const Container = styled.label`
24+
padding: 8px;
25+
margin: 0;
26+
display: flex;
27+
flex-direction: row;
28+
align-items: center;
29+
`;
30+
31+
const NewCommitsCheckbox = styled.input`
32+
margin-right: 8px;
33+
`;

src/components/Popup.tsx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import { Link } from "./design/Link";
1212
import { Row } from "./design/Row";
1313
import { IgnoredRepositories } from "./IgnoredRepositories";
1414
import { Loader } from "./Loader";
15+
import { NewCommitsToggle } from "./NewCommitsToggle";
1516
import { PullRequestList } from "./PullRequestList";
1617
import { Settings } from "./Settings";
1718
import { Status } from "./Status";
19+
import { WhitelistedTeams } from "./WhitelistedTeams";
1820

1921
export interface PopupProps {
2022
core: Core;
@@ -159,6 +161,31 @@ export const Popup = observer((props: PopupProps) => {
159161
/>
160162
</Tabs>
161163
<PullRequestList
164+
header={
165+
state.currentFilter === Filter.INCOMING && (
166+
<>
167+
<WhitelistedTeams
168+
onlyDirectRequestsToggled={
169+
!!props.core.muteConfiguration.onlyDirectRequests
170+
}
171+
whitelistedTeams={
172+
props.core.muteConfiguration.whitelistedTeams || []
173+
}
174+
userLogin={
175+
props.core.loadedState
176+
? props.core.loadedState.userLogin
177+
: undefined
178+
}
179+
onToggleOnlyDirectRequests={onToggleOnlyDirectRequests}
180+
onChangeWhitelistedTeams={onChangeWhitelistedTeams}
181+
/>
182+
<NewCommitsToggle
183+
toggled={!!props.core.muteConfiguration.notifyNewCommits}
184+
onToggle={onToggleNewCommitsNotification}
185+
/>
186+
</>
187+
)
188+
}
162189
pullRequests={
163190
props.core.filteredPullRequests
164191
? props.core.filteredPullRequests[state.currentFilter]
@@ -176,28 +203,6 @@ export const Popup = observer((props: PopupProps) => {
176203
? "allow-unmuting"
177204
: "none"
178205
}
179-
newCommitsNotificationToggled={
180-
state.currentFilter === Filter.INCOMING
181-
? !!props.core.muteConfiguration.notifyNewCommits
182-
: null
183-
}
184-
onlyDirectRequestsToggled={
185-
state.currentFilter === Filter.INCOMING
186-
? !!props.core.muteConfiguration.onlyDirectRequests
187-
: null
188-
}
189-
whitelistedTeams={
190-
state.currentFilter === Filter.INCOMING
191-
? props.core.muteConfiguration.whitelistedTeams || []
192-
: []
193-
}
194-
userLogin={
195-
(props.core.loadedState && props.core.loadedState.userLogin) ||
196-
undefined
197-
}
198-
onToggleNewCommitsNotification={onToggleNewCommitsNotification}
199-
onToggleOnlyDirectRequests={onToggleOnlyDirectRequests}
200-
onChangeWhitelistedTeams={onChangeWhitelistedTeams}
201206
onOpenAll={onOpenAll}
202207
onOpen={onOpen}
203208
onMute={onMute}

src/components/PullRequestList.tsx

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import styled from "@emotion/styled";
22
import { observer } from "mobx-react-lite";
3-
import React, { useRef, useState } from "react";
3+
import React from "react";
44
import { EnrichedPullRequest } from "../filtering/enriched-pull-request";
55
import { PullRequest } from "../storage/loaded-state";
66
import { MuteType } from "../storage/mute-configuration";
7-
import { LargeButton } from "./design/Button";
87
import { Link } from "./design/Link";
98
import { Paragraph } from "./design/Paragraph";
109
import { Loader } from "./Loader";
@@ -17,40 +16,6 @@ const List = styled.div`
1716
margin-bottom: 16px;
1817
`;
1918

20-
const NewCommitsToggle = styled.label`
21-
padding: 8px;
22-
margin: 0;
23-
display: flex;
24-
flex-direction: row;
25-
align-items: center;
26-
`;
27-
28-
const NewCommitsCheckbox = styled.input`
29-
margin-right: 8px;
30-
`;
31-
32-
const OnlyDirectRequestsCheckbox = styled.input`
33-
margin-right: 8px;
34-
`;
35-
36-
const OnlyDirectRequestsToggle = styled.label`
37-
padding: 8px;
38-
margin: 0;
39-
display: flex;
40-
flex-direction: row;
41-
align-items: center;
42-
`;
43-
44-
const WhitelistedTeamsInput = styled.input`
45-
flex-grow: 1;
46-
padding: 4px 8px;
47-
margin-right: 8px;
48-
49-
&:focus {
50-
outline-color: #2ee59d;
51-
}
52-
`;
53-
5419
const OpenAllParagraph = styled(Paragraph)`
5520
text-align: center;
5621
color: #777;
@@ -60,85 +25,17 @@ export interface PullRequestListProps {
6025
pullRequests: EnrichedPullRequest[] | null;
6126
emptyMessage: string;
6227
mutingConfiguration: "allow-muting" | "allow-unmuting" | "none";
63-
newCommitsNotificationToggled: boolean | null;
64-
onlyDirectRequestsToggled: boolean | null;
65-
whitelistedTeams: string[];
66-
userLogin?: string;
67-
onToggleNewCommitsNotification?(): void;
68-
onToggleOnlyDirectRequests?(): void;
69-
onChangeWhitelistedTeams?: (text: string) => void;
28+
header: React.ReactNode;
7029
onOpenAll(): void;
7130
onOpen(pullRequestUrl: string): void;
7231
onMute(pullRequest: PullRequest, muteType: MuteType): void;
7332
onUnmute(pullRequest: PullRequest): void;
7433
}
7534

7635
export const PullRequestList = observer((props: PullRequestListProps) => {
77-
const defaultWhitelistedTeams = props.whitelistedTeams.join(", ");
78-
const [whitelistedTeams, setWhitelistedTeams] = useState(
79-
defaultWhitelistedTeams
80-
);
81-
const inputRef = useRef<HTMLInputElement>(null);
82-
const handleWhitelistedTeamsChange = (e: React.FormEvent) => {
83-
e.preventDefault();
84-
if (!inputRef.current) {
85-
return;
86-
}
87-
88-
setWhitelistedTeams(inputRef.current.value);
89-
};
90-
const handleApplyWhitelistedTeamsChange = (e: React.FormEvent) => {
91-
e.preventDefault();
92-
props.onChangeWhitelistedTeams &&
93-
props.onChangeWhitelistedTeams(whitelistedTeams);
94-
};
9536
return (
9637
<List>
97-
{props.onlyDirectRequestsToggled !== null && (
98-
<OnlyDirectRequestsToggle>
99-
<OnlyDirectRequestsCheckbox
100-
type="checkbox"
101-
checked={props.onlyDirectRequestsToggled}
102-
onChange={props.onToggleOnlyDirectRequests}
103-
/>
104-
<div>
105-
Only directly requested
106-
{props.userLogin && (
107-
<span>
108-
{" "}
109-
(<b>@{props.userLogin}</b>){" "}
110-
</span>
111-
)}
112-
and whitelisted teams
113-
{props.onlyDirectRequestsToggled && props.onChangeWhitelistedTeams && (
114-
<div>
115-
<WhitelistedTeamsInput
116-
ref={inputRef}
117-
placeholder="team1, team2"
118-
value={whitelistedTeams}
119-
onInput={handleWhitelistedTeamsChange}
120-
></WhitelistedTeamsInput>
121-
<LargeButton
122-
disabled={whitelistedTeams === defaultWhitelistedTeams}
123-
onClick={handleApplyWhitelistedTeamsChange}
124-
>
125-
Apply
126-
</LargeButton>
127-
</div>
128-
)}
129-
</div>
130-
</OnlyDirectRequestsToggle>
131-
)}
132-
{props.newCommitsNotificationToggled !== null && (
133-
<NewCommitsToggle>
134-
<NewCommitsCheckbox
135-
type="checkbox"
136-
checked={props.newCommitsNotificationToggled}
137-
onChange={props.onToggleNewCommitsNotification}
138-
/>
139-
Include new commits
140-
</NewCommitsToggle>
141-
)}
38+
{props.header}
14239
{props.pullRequests === null ? (
14340
<Loader />
14441
) : props.pullRequests.length === 0 ? (
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import styled from "@emotion/styled";
2+
import { observer } from "mobx-react-lite";
3+
import React, { useRef, useState } from "react";
4+
import { LargeButton } from "./design/Button";
5+
6+
export interface WhitelistedTeamsProps {
7+
onlyDirectRequestsToggled: boolean;
8+
whitelistedTeams: string[];
9+
userLogin?: string;
10+
onToggleOnlyDirectRequests(): void;
11+
onChangeWhitelistedTeams(text: string): void;
12+
}
13+
14+
export const WhitelistedTeams = observer((props: WhitelistedTeamsProps) => {
15+
const defaultWhitelistedTeams = props.whitelistedTeams.join(", ");
16+
const [whitelistedTeams, setWhitelistedTeams] = useState(
17+
defaultWhitelistedTeams
18+
);
19+
const inputRef = useRef<HTMLInputElement>(null);
20+
const handleWhitelistedTeamsChange = (e: React.FormEvent) => {
21+
e.preventDefault();
22+
if (!inputRef.current) {
23+
return;
24+
}
25+
26+
setWhitelistedTeams(inputRef.current.value);
27+
};
28+
const handleApplyWhitelistedTeamsChange = (e: React.FormEvent) => {
29+
e.preventDefault();
30+
props.onChangeWhitelistedTeams(whitelistedTeams);
31+
};
32+
return (
33+
<OnlyDirectRequestsToggle>
34+
<OnlyDirectRequestsCheckbox
35+
type="checkbox"
36+
checked={props.onlyDirectRequestsToggled}
37+
onChange={props.onToggleOnlyDirectRequests}
38+
/>
39+
<div>
40+
Only directly requested
41+
{props.userLogin && (
42+
<span>
43+
{" "}
44+
(<b>@{props.userLogin}</b>){" "}
45+
</span>
46+
)}{" "}
47+
and whitelisted teams
48+
{props.onlyDirectRequestsToggled && (
49+
<div>
50+
<WhitelistedTeamsInput
51+
ref={inputRef}
52+
placeholder="team1, team2"
53+
value={whitelistedTeams}
54+
onInput={handleWhitelistedTeamsChange}
55+
></WhitelistedTeamsInput>
56+
<LargeButton
57+
disabled={whitelistedTeams === defaultWhitelistedTeams}
58+
onClick={handleApplyWhitelistedTeamsChange}
59+
>
60+
Apply
61+
</LargeButton>
62+
</div>
63+
)}
64+
</div>
65+
</OnlyDirectRequestsToggle>
66+
);
67+
});
68+
69+
const OnlyDirectRequestsCheckbox = styled.input`
70+
margin-right: 8px;
71+
`;
72+
73+
const OnlyDirectRequestsToggle = styled.label`
74+
padding: 8px;
75+
margin: 0;
76+
display: flex;
77+
flex-direction: row;
78+
align-items: center;
79+
`;
80+
81+
const WhitelistedTeamsInput = styled.input`
82+
flex-grow: 1;
83+
padding: 4px 8px;
84+
margin-right: 8px;
85+
86+
&:focus {
87+
outline-color: #2ee59d;
88+
}
89+
`;

0 commit comments

Comments
 (0)