Skip to content

Commit b02b1db

Browse files
authored
chore: enable rule of hooks (#7080)
1 parent ea73b9a commit b02b1db

File tree

7 files changed

+135
-112
lines changed

7 files changed

+135
-112
lines changed

.eslintrc.cjs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ module.exports = {
179179
'react/jsx-no-useless-fragment': 'off',
180180
'@typescript-eslint/no-explicit-any': 'off',
181181
'@typescript-eslint/no-empty-function': 'off',
182-
'react-hooks/rules-of-hooks': 'off',
182+
'react-hooks/rules-of-hooks': 'error',
183183
'react-hooks/exhaustive-deps': 'off',
184184
'unicorn/filename-case': 'off',
185185
'import/no-default-export': 'off',
@@ -206,13 +206,12 @@ module.exports = {
206206
},
207207
},
208208
},
209-
// {
210-
// files: ['packages/web/app/**'],
211-
// excludedFiles: ['packages/web/app/src/pages/**'],
212-
// rules: {
213-
// 'import/no-unused-modules': ['error', { unusedExports: true }],
214-
// },
215-
// },
209+
{
210+
files: ['packages/web/app/**/*.stories.tsx', 'packages/web/docs/**'],
211+
rules: {
212+
'react-hooks/rules-of-hooks': 'off',
213+
},
214+
},
216215
{
217216
files: ['packages/web/docs/**'],
218217
settings: {

packages/web/app/src/components/admin/AdminStats.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ function CollectedOperationsOverTime(props: {
8585
const dataRef = useRef<[string, number][]>();
8686
dataRef.current ||= operations.map(node => [node.date, node.count]);
8787
const data = dataRef.current;
88+
const chartStyles = useChartStyles();
8889

8990
return (
9091
<AutoSizer disableHeight>
@@ -93,7 +94,7 @@ function CollectedOperationsOverTime(props: {
9394
style={{ width: size.width, height: 200 }}
9495
theme={theme.theme}
9596
option={{
96-
...useChartStyles(),
97+
...chartStyles,
9798
grid: {
9899
left: 50,
99100
top: 50,

packages/web/app/src/components/target/history/errors-and-changes.tsx

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,31 @@ export function ChangesBlock(
130130
}
131131
),
132132
): ReactElement | null {
133-
const changes = props.changesWithUsage ?? props.changes;
134-
135133
return (
136134
<div>
137135
<h2 className="mb-3 font-bold text-gray-900 dark:text-white">{props.title}</h2>
138136
<div className="list-inside list-disc space-y-2 text-sm leading-relaxed">
139-
{changes.map((change, key) => (
137+
{props.changesWithUsage?.map((change, key) => (
138+
<ChangeItem
139+
organizationSlug={props.organizationSlug}
140+
projectSlug={props.projectSlug}
141+
targetSlug={props.targetSlug}
142+
schemaCheckId={props.schemaCheckId}
143+
key={key}
144+
change={null}
145+
changeWithUsage={change}
146+
conditionBreakingChangeMetadata={props.conditionBreakingChangeMetadata ?? null}
147+
/>
148+
))}
149+
{props.changes?.map((change, key) => (
140150
<ChangeItem
141151
organizationSlug={props.organizationSlug}
142152
projectSlug={props.projectSlug}
143153
targetSlug={props.targetSlug}
144154
schemaCheckId={props.schemaCheckId}
145155
key={key}
146156
change={change}
157+
changeWithUsage={null}
147158
conditionBreakingChangeMetadata={props.conditionBreakingChangeMetadata ?? null}
148159
/>
149160
))}
@@ -152,32 +163,34 @@ export function ChangesBlock(
152163
);
153164
}
154165

155-
// Obviously I'm not proud of this...
156-
// But I didn't want to spend too much time on this
157-
function isChangesBlock_SchemaChangeWithUsageFragment(
158-
fragment: any,
159-
): fragment is FragmentType<typeof ChangesBlock_SchemaChangeWithUsageFragment> {
160-
return (
161-
!!fragment[' $fragmentRefs'] &&
162-
'ChangesBlock_SchemaChangeWithUsageFragment' in fragment[' $fragmentRefs']
166+
function ChangeItem(
167+
props: {
168+
conditionBreakingChangeMetadata: FragmentType<
169+
typeof ChangesBlock_SchemaCheckConditionalBreakingChangeMetadataFragment
170+
> | null;
171+
organizationSlug: string;
172+
projectSlug: string;
173+
targetSlug: string;
174+
schemaCheckId: string;
175+
} & (
176+
| {
177+
change: FragmentType<typeof ChangesBlock_SchemaChangeFragment>;
178+
changeWithUsage: null;
179+
}
180+
| {
181+
change: null;
182+
changeWithUsage: FragmentType<typeof ChangesBlock_SchemaChangeWithUsageFragment>;
183+
}
184+
),
185+
) {
186+
const cchange = useFragment(ChangesBlock_SchemaChangeFragment, props.change);
187+
const cchangeWithUsage = useFragment(
188+
ChangesBlock_SchemaChangeWithUsageFragment,
189+
props.changeWithUsage,
163190
);
164-
}
165191

166-
function ChangeItem(props: {
167-
change:
168-
| FragmentType<typeof ChangesBlock_SchemaChangeWithUsageFragment>
169-
| FragmentType<typeof ChangesBlock_SchemaChangeFragment>;
170-
conditionBreakingChangeMetadata: FragmentType<
171-
typeof ChangesBlock_SchemaCheckConditionalBreakingChangeMetadataFragment
172-
> | null;
173-
organizationSlug: string;
174-
projectSlug: string;
175-
targetSlug: string;
176-
schemaCheckId: string;
177-
}) {
178-
const change = isChangesBlock_SchemaChangeWithUsageFragment(props.change)
179-
? useFragment(ChangesBlock_SchemaChangeWithUsageFragment, props.change)
180-
: useFragment(ChangesBlock_SchemaChangeFragment, props.change);
192+
// at least one prop must be provided :)
193+
const change = (cchange ?? cchangeWithUsage)!;
181194

182195
const metadata = useFragment(
183196
ChangesBlock_SchemaCheckConditionalBreakingChangeMetadataFragment,

packages/web/app/src/components/target/insights/Filters.tsx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ function OperationsFilter({
4949
operationStatsConnection,
5050
);
5151

52-
const clientFilteredOperations = clientOperationStatsConnection
53-
? useFragment(
54-
OperationsFilter_OperationStatsValuesConnectionFragment,
55-
clientOperationStatsConnection,
56-
)
57-
: null;
52+
const clientFilteredOperations = useFragment(
53+
OperationsFilter_OperationStatsValuesConnectionFragment,
54+
clientOperationStatsConnection,
55+
);
5856

5957
function getOperationHashes() {
6058
const items: string[] = [];
@@ -324,11 +322,12 @@ function OperationRow({
324322
}): ReactElement {
325323
const operation = useFragment(OperationRow_OperationStatsValuesFragment, operationStats);
326324
const requests = useFormattedNumber(operation.count);
327-
const clientsOperation = clientOperationStats
328-
? useFragment(OperationRow_OperationStatsValuesFragment, clientOperationStats)
329-
: undefined;
325+
const clientsOperation = useFragment(
326+
OperationRow_OperationStatsValuesFragment,
327+
clientOperationStats || null,
328+
);
330329
const hasClientOperation = clientOperationStats !== false;
331-
const clientsRequests = clientsOperation ? useFormattedNumber(clientsOperation.count) : null;
330+
const clientsRequests = useFormattedNumber(clientsOperation?.count);
332331
const hash = operation.operationHash || '';
333332
const change = useCallback(() => {
334333
if (hash) {
@@ -340,7 +339,7 @@ function OperationRow({
340339
if (hasClientOperation) {
341340
return (
342341
<div className="flex shrink-0 text-right text-gray-500">
343-
<span>{clientsRequests ?? 0}</span>
342+
<span>{clientsRequests === '-' ? 0 : clientsRequests}</span>
344343
<span className="ml-1 truncate text-gray-600">/ {requests}</span>
345344
</div>
346345
);
@@ -425,10 +424,10 @@ function ClientRow({
425424
style: any;
426425
}): ReactElement {
427426
const client = useFragment(ClientRow_ClientStatsValuesFragment, props.client);
428-
const clientOperation =
429-
props.clientOperationStats === false
430-
? false
431-
: useFragment(ClientRow_ClientStatsValuesFragment, props.clientOperationStats);
427+
const clientOperation = useFragment(
428+
ClientRow_ClientStatsValuesFragment,
429+
props.clientOperationStats || null,
430+
);
432431
const requests = useFormattedNumber(client.count);
433432
const hash = client.name;
434433
const change = useCallback(() => {
@@ -438,7 +437,7 @@ function ClientRow({
438437
}, [onSelect, hash, selected]);
439438

440439
const Totals = () => {
441-
if (clientOperation !== false) {
440+
if (props.clientOperationStats !== false) {
442441
return (
443442
<div className="flex shrink-0 text-right text-gray-500">
444443
<span>{clientOperation?.count ?? 0}</span>
@@ -544,9 +543,10 @@ function ClientsFilter({
544543
setSelectedItems([]);
545544
}, [setSelectedItems]);
546545

547-
const operationConnection = operationStatsConnection
548-
? useFragment(ClientsFilter_ClientStatsValuesConnectionFragment, operationStatsConnection)
549-
: null;
546+
const operationConnection = useFragment(
547+
ClientsFilter_ClientStatsValuesConnectionFragment,
548+
operationStatsConnection ?? null,
549+
);
550550

551551
const renderRow = useCallback<ComponentType<ListChildComponentProps>>(
552552
({ index, style }) => {

packages/web/app/src/components/target/settings/cdn-access-tokens.tsx

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,9 @@ import { AlertTriangleIcon, TrashIcon } from '@/components/ui/icon';
1111
import { SubPageLayout, SubPageLayoutHeader } from '@/components/ui/page-content-layout';
1212
import { Input, Modal, Table, Tag, TBody, Td, TimeAgo, Tr } from '@/components/v2';
1313
import { InlineCode } from '@/components/v2/inline-code';
14-
import { graphql, useFragment } from '@/gql';
14+
import { FragmentType, graphql, useFragment } from '@/gql';
1515
import { Link, useRouter } from '@tanstack/react-router';
1616

17-
const CDNAccessTokeRowFragment = graphql(`
18-
fragment CDNAccessTokens_CdnAccessTokenRowFragment on CdnAccessToken {
19-
id
20-
firstCharacters
21-
lastCharacters
22-
alias
23-
createdAt
24-
}
25-
`);
26-
2717
const CDNAccessTokenCreateMutation = graphql(`
2818
mutation CDNAccessTokens_CDNAccessTokenCreateMutation($input: CreateCdnAccessTokenInput!) {
2919
createCdnAccessToken(input: $input) {
@@ -407,38 +397,9 @@ export function CDNAccessTokens(props: {
407397
</div>
408398
<Table>
409399
<TBody>
410-
{target?.data?.target?.cdnAccessTokens.edges?.map(edge => {
411-
const node = useFragment(CDNAccessTokeRowFragment, edge.node);
412-
413-
return (
414-
<Tr key={node.id}>
415-
<Td>
416-
{node.firstCharacters + new Array(10).fill('•').join('') + node.lastCharacters}
417-
</Td>
418-
<Td>{node.alias}</Td>
419-
<Td align="right">
420-
created <TimeAgo date={node.createdAt} />
421-
</Td>
422-
<Td align="right">
423-
<Button
424-
className="hover:text-red-500"
425-
variant="ghost"
426-
onClick={() => {
427-
void router.navigate({
428-
search: {
429-
page: 'cdn',
430-
cdn: 'delete',
431-
id: node.id,
432-
},
433-
});
434-
}}
435-
>
436-
<TrashIcon />
437-
</Button>
438-
</Td>
439-
</Tr>
440-
);
441-
})}
400+
{target?.data?.target?.cdnAccessTokens.edges?.map(edge => (
401+
<CDNAccessTokenRow cdnAccessToken={edge.node} key={edge.node.id} />
402+
))}
442403
</TBody>
443404
</Table>
444405

@@ -503,3 +464,49 @@ export function CDNAccessTokens(props: {
503464
</SubPageLayout>
504465
);
505466
}
467+
468+
const CDNAccessTokenRowFragment = graphql(`
469+
fragment CDNAccessTokens_CdnAccessTokenRowFragment on CdnAccessToken {
470+
id
471+
firstCharacters
472+
lastCharacters
473+
alias
474+
createdAt
475+
}
476+
`);
477+
478+
type CDNAccessTokenRowProps = {
479+
cdnAccessToken: FragmentType<typeof CDNAccessTokenRowFragment>;
480+
};
481+
482+
function CDNAccessTokenRow(props: CDNAccessTokenRowProps): React.ReactNode {
483+
const node = useFragment(CDNAccessTokenRowFragment, props.cdnAccessToken);
484+
const router = useRouter();
485+
486+
return (
487+
<Tr key={node.id}>
488+
<Td>{node.firstCharacters + new Array(10).fill('•').join('') + node.lastCharacters}</Td>
489+
<Td>{node.alias}</Td>
490+
<Td align="right">
491+
created <TimeAgo date={node.createdAt} />
492+
</Td>
493+
<Td align="right">
494+
<Button
495+
className="hover:text-red-500"
496+
variant="ghost"
497+
onClick={() => {
498+
void router.navigate({
499+
search: {
500+
page: 'cdn',
501+
cdn: 'delete',
502+
id: node.id,
503+
},
504+
});
505+
}}
506+
>
507+
<TrashIcon />
508+
</Button>
509+
</Td>
510+
</Tr>
511+
);
512+
}

packages/web/app/src/pages/target-checks.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,22 +271,13 @@ function ChecksPageContent(props: {
271271
},
272272
});
273273

274-
if (query.error) {
275-
return (
276-
<QueryError
277-
organizationSlug={props.organizationSlug}
278-
error={query.error}
279-
showLogoutButton={false}
280-
/>
281-
);
282-
}
283-
284274
const isLoading = query.fetching || query.stale;
285275
const renderLoading = useDebouncedLoader(isLoading);
286276

287277
const [hasSchemaChecks, setHasSchemaChecks] = useState(
288278
!!query.data?.target?.schemaChecks?.edges?.length,
289279
);
280+
290281
useEffect(() => {
291282
if (!isLoading) {
292283
setHasSchemaChecks(!!query.data?.target?.schemaChecks?.edges?.length);
@@ -298,8 +289,19 @@ function ChecksPageContent(props: {
298289
const [paginationVariables, setPaginationVariables] = useState<Array<string | null>>(() => [
299290
null,
300291
]);
292+
301293
const onLoadMore = (cursor: string) => setPaginationVariables(cursors => [...cursors, cursor]);
302294

295+
if (query.error) {
296+
return (
297+
<QueryError
298+
organizationSlug={props.organizationSlug}
299+
error={query.error}
300+
showLogoutButton={false}
301+
/>
302+
);
303+
}
304+
303305
return (
304306
<>
305307
<div className={cn(!hasSchemaChecks && 'w-full')}>

0 commit comments

Comments
 (0)