Skip to content

Commit 5a54c27

Browse files
authored
feat/feature-flag-link-checker (#1811)
* feat/feature-flag-link-checker * fix(linkChecker): dont trip alarms
1 parent 1620e1c commit 5a54c27

File tree

6 files changed

+97
-47
lines changed

6 files changed

+97
-47
lines changed

src/constants/featureFlags.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const FEATURE_FLAGS = {
66
RTE_ENABLED_BLOCKS: "rte_enabled_blocks",
77
TIPTAP_EDITOR: "is-tiptap-enabled",
88
IS_SHOW_STAGING_BUILD_STATUS_ENABLED: "is_show_staging_build_status_enabled",
9+
IS_BROKEN_LINKS_REPORT_ENABLED: "is_broken_links_report_enabled",
910
} as const
1011

1112
export type FeatureFlagsType = typeof FEATURE_FLAGS[keyof typeof FEATURE_FLAGS]

src/hooks/siteDashboardHooks/useGetLinkChecker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import * as LinkCheckerService from "services/LinkCheckerService"
77
import { RepoErrorDto } from "types/linkReport"
88

99
export const useGetBrokenLinks = (
10-
siteName: string
10+
siteName: string,
11+
isBrokenLinksReporterEnabled: boolean
1112
): UseQueryResult<RepoErrorDto> => {
1213
return useQuery<RepoErrorDto>(
1314
[SITE_LINK_CHECKER_STATUS_KEY, siteName],
1415
() => {
15-
return LinkCheckerService.getLinkCheckerStatus({ siteName })
16+
return LinkCheckerService.getLinkCheckerStatus({
17+
siteName,
18+
isBrokenLinksReporterEnabled,
19+
})
1620
},
1721
{
1822
retry: false,

src/layouts/LinkReport/LinksReport.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Tr,
1414
VStack,
1515
} from "@chakra-ui/react"
16+
import { useFeatureIsOn } from "@growthbook/growthbook-react"
1617
import { Badge, Breadcrumb, Button, Link } from "@opengovsg/design-system-react"
1718
import { Redirect, useParams } from "react-router-dom"
1819

@@ -59,8 +60,14 @@ export const LinksReportBanner = () => {
5960
const onClick = () => {
6061
refreshLinkChecker(siteName)
6162
}
63+
const isBrokenLinksReporterEnabled = useFeatureIsOn(
64+
"is_broken_links_report_enabled"
65+
)
6266

63-
const { data: brokenLinks } = useGetBrokenLinks(siteName)
67+
const { data: brokenLinks } = useGetBrokenLinks(
68+
siteName,
69+
isBrokenLinksReporterEnabled
70+
)
6471

6572
const isBrokenLinksLoading = brokenLinks?.status === "loading"
6673
return (
@@ -89,6 +96,17 @@ export const LinksReportBanner = () => {
8996
)
9097
}
9198

99+
const normaliseUrl = (url: string): string => {
100+
let normalisedUrl = url
101+
if (url.endsWith("/")) {
102+
normalisedUrl = url.slice(0, -1)
103+
}
104+
if (url.startsWith("/")) {
105+
normalisedUrl = url.slice(1)
106+
}
107+
return normalisedUrl
108+
}
109+
92110
const SiteReportCard = ({
93111
breadcrumb,
94112
links,
@@ -103,7 +121,9 @@ const SiteReportCard = ({
103121
siteName
104122
)
105123

106-
const viewableLinkInStaging = stagingUrl + viewablePageInStaging.slice(1) // rm the leading `/`
124+
const normalisedStagingUrl = normaliseUrl(stagingUrl || "")
125+
const normalisedViewablePageInStaging = normaliseUrl(viewablePageInStaging)
126+
const viewableLinkInStaging = `${normalisedStagingUrl}/${normalisedViewablePageInStaging}`
107127

108128
return (
109129
<VStack
@@ -301,12 +321,20 @@ const ErrorLoading = () => {
301321
}
302322

303323
const LinkBody = () => {
324+
const isBrokenLinksReporterEnabled = useFeatureIsOn(
325+
"is_broken_links_report_enabled"
326+
)
304327
const { siteName } = useParams<{ siteName: string }>()
305328
const { data: brokenLinks, isError: isBrokenLinksError } = useGetBrokenLinks(
306-
siteName
329+
siteName,
330+
isBrokenLinksReporterEnabled
307331
)
308332

309-
if (isBrokenLinksError || brokenLinks?.status === "error") {
333+
if (
334+
!isBrokenLinksReporterEnabled ||
335+
isBrokenLinksError ||
336+
brokenLinks?.status === "error"
337+
) {
310338
return <ErrorLoading />
311339
}
312340

src/layouts/SiteDashboard/SiteDashboard.tsx

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
useDisclosure,
1212
VStack,
1313
} from "@chakra-ui/react"
14+
import { useFeatureIsOn, useGrowthBook } from "@growthbook/growthbook-react"
1415
import { Button, Link } from "@opengovsg/design-system-react"
1516
import _ from "lodash"
1617
import { useEffect } from "react"
@@ -95,11 +96,15 @@ export const SiteDashboard = (): JSX.Element => {
9596
mutateAsync: updateViewedReviewRequests,
9697
} = useUpdateViewedReviewRequests()
9798

99+
const isBrokenLinksReporterEnabled = useFeatureIsOn(
100+
"is_broken_links_report_enabled"
101+
)
102+
98103
const {
99104
data: brokenLinks,
100105
isError: isBrokenLinksError,
101106
isLoading: isBrokenLinksLoading,
102-
} = useGetBrokenLinks(siteName)
107+
} = useGetBrokenLinks(siteName, isBrokenLinksReporterEnabled)
103108

104109
const savedAt = getDateTimeFromUnixTime(siteInfo?.savedAt || 0)
105110
const publishedAt = getDateTimeFromUnixTime(siteInfo?.publishedAt || 0)
@@ -205,48 +210,52 @@ export const SiteDashboard = (): JSX.Element => {
205210
))
206211
)}
207212
</DisplayCardContent>
208-
209-
{isBrokenLinksLoading || brokenLinks?.status === "loading" ? (
210-
<Skeleton w="100%" height="4rem" />
211-
) : (
213+
{isBrokenLinksReporterEnabled && (
212214
<>
213-
<DisplayCardHeader mt="0.75rem">
214-
<DisplayCardTitle>Your site health</DisplayCardTitle>
215-
<DisplayCardCaption>
216-
{`Understand your site's broken references`}
217-
</DisplayCardCaption>
218-
</DisplayCardHeader>
219-
<DisplayCardContent>
220-
{isBrokenLinksError || brokenLinks?.status === "error" ? (
221-
<Text textStyle="body-1">
222-
Unable to retrieve broken links report
223-
</Text>
224-
) : (
225-
<DisplayCard
226-
variant="full"
227-
bgColor="background.action.defaultInverse"
228-
>
229-
<HStack w="full" justifyContent="space-between">
230-
<HStack>
231-
<Text textStyle="h4">
232-
{brokenLinks?.status === "success" &&
233-
brokenLinks?.errors.length}
234-
</Text>
235-
<Text textStyle="body-1">
236-
broken references found
237-
</Text>
238-
</HStack>
239-
240-
<Link
241-
href={`/sites/${siteName}/linkCheckerReport`}
242-
textStyle="body-1"
215+
{isBrokenLinksLoading || brokenLinks?.status === "loading" ? (
216+
<Skeleton w="100%" height="4rem" />
217+
) : (
218+
<>
219+
<DisplayCardHeader mt="0.75rem">
220+
<DisplayCardTitle>Your site health</DisplayCardTitle>
221+
<DisplayCardCaption>
222+
{`Understand your site's broken references`}
223+
</DisplayCardCaption>
224+
</DisplayCardHeader>
225+
<DisplayCardContent>
226+
{isBrokenLinksError ||
227+
brokenLinks?.status === "error" ? (
228+
<Text textStyle="body-1">
229+
Unable to retrieve broken links report
230+
</Text>
231+
) : (
232+
<DisplayCard
233+
variant="full"
234+
bgColor="background.action.defaultInverse"
243235
>
244-
View report
245-
</Link>
246-
</HStack>
247-
</DisplayCard>
248-
)}
249-
</DisplayCardContent>
236+
<HStack w="full" justifyContent="space-between">
237+
<HStack>
238+
<Text textStyle="h4">
239+
{brokenLinks?.status === "success" &&
240+
brokenLinks?.errors.length}
241+
</Text>
242+
<Text textStyle="body-1">
243+
broken references found
244+
</Text>
245+
</HStack>
246+
247+
<Link
248+
href={`/sites/${siteName}/linkCheckerReport`}
249+
textStyle="body-1"
250+
>
251+
View report
252+
</Link>
253+
</HStack>
254+
</DisplayCard>
255+
)}
256+
</DisplayCardContent>
257+
</>
258+
)}
250259
</>
251260
)}
252261
</DisplayCard>

src/services/LinkCheckerService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ import { apiService } from "./ApiService"
44

55
export const getLinkCheckerStatus = async ({
66
siteName,
7+
isBrokenLinksReporterEnabled,
78
}: {
89
siteName: string
10+
isBrokenLinksReporterEnabled: boolean
911
}): Promise<RepoErrorDto> => {
12+
if (!isBrokenLinksReporterEnabled) {
13+
return {
14+
status: "error",
15+
}
16+
}
1017
const endpoint = `/sites/${siteName}/getLinkCheckerStatus`
1118
return (await apiService.get(endpoint)).data
1219
}

src/types/featureFlags.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface FeatureFlags {
1313
[FEATURE_FLAGS.RTE_ENABLED_BLOCKS]: { blocks: RTEBlockValues[] }
1414
[FEATURE_FLAGS.TIPTAP_EDITOR]: boolean
1515
[FEATURE_FLAGS.IS_SHOW_STAGING_BUILD_STATUS_ENABLED]: boolean
16+
[FEATURE_FLAGS.IS_BROKEN_LINKS_REPORT_ENABLED]: boolean
1617
}
1718

1819
export type GBAttributes = {

0 commit comments

Comments
 (0)