-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
142 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import clsx from "clsx"; | ||
|
||
type SnackbarProps = { | ||
isVisible: boolean; | ||
children?: React.ReactNode; | ||
}; | ||
|
||
const Snackbar = ({ isVisible = false, children }: SnackbarProps) => { | ||
return ( | ||
<div | ||
data-testid="snackbar" | ||
className={clsx( | ||
"text-left position-fixed padding-2 text-white font-sans-2xs radius-md bg-base-darkest usa-modal-wrapper top-0 right-0", | ||
{ | ||
"is-visible": isVisible, | ||
"is-hidden": !isVisible, | ||
}, | ||
)} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Snackbar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,68 @@ | ||
"use client"; | ||
|
||
import { environment } from "src/constants/environments"; | ||
import { useCopyToClipboard } from "src/hooks/useCopyToClipboard"; | ||
import { useSnackbar } from "src/hooks/useSnackbar"; | ||
|
||
import { usePathname, useSearchParams } from "next/navigation"; | ||
import { Button, Tooltip } from "@trussworks/react-uswds"; | ||
|
||
import { USWDSIcon } from "src/components/USWDSIcon"; | ||
|
||
const SNACKBAR_VISIBLE_TIME = 4000; | ||
|
||
type SearchSavedQueryProps = { | ||
copyText: string; | ||
copyingText: string; | ||
copiedText: string; | ||
helpText: string; | ||
url: string; | ||
snackbarMessage: React.ReactNode | string; | ||
}; | ||
const SearchSavedQuery2 = ({ copyText, helpText }: SearchSavedQueryProps) => { | ||
|
||
const SearchSavedQuery = ({ | ||
copyText, | ||
copyingText, | ||
copiedText, | ||
helpText, | ||
url, | ||
snackbarMessage, | ||
}: SearchSavedQueryProps) => { | ||
const { copied, loading, copyToClipboard } = useCopyToClipboard(); | ||
const path = usePathname(); | ||
const searchParams = useSearchParams(); | ||
const url = `${environment.NEXT_PUBLIC_BASE_URL}${path}?${searchParams.toString()}`; | ||
const { snackbarIsVisible, showSnackbar, Snackbar } = useSnackbar(); | ||
|
||
return ( | ||
<div className="text-underline border-base-lighter border-1px padding-2 text-primary-darker display-flex"> | ||
<Button | ||
type="button" | ||
unstyled | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
onClick={() => copyToClipboard(url)} | ||
> | ||
<USWDSIcon name="content_copy" /> | ||
{loading ? "Copying..." : <>{copied ? "Copied!" : copyText}</>} | ||
</Button> | ||
<Tooltip | ||
className="text-secondary-darker usa-button--unstyled" | ||
label={helpText} | ||
position="top" | ||
> | ||
<USWDSIcon className="margin-left-1" name="info_outline" /> | ||
</Tooltip> | ||
<div> | ||
<div className="text-underline border-base-lighter border-1px padding-2 text-primary-darker display-flex"> | ||
<Button | ||
type="button" | ||
unstyled | ||
onClick={() => { | ||
copyToClipboard(url, SNACKBAR_VISIBLE_TIME) | ||
.then(() => { | ||
showSnackbar(SNACKBAR_VISIBLE_TIME); | ||
}) | ||
.catch((error) => { | ||
console.error("Error copying to clipboard:", error); | ||
}); | ||
}} | ||
> | ||
<USWDSIcon name="content_copy" /> | ||
{loading ? ( | ||
<>{copyingText}</> | ||
) : ( | ||
<>{copied ? { copiedText } : copyText}</> | ||
)} | ||
</Button> | ||
<Tooltip | ||
className="text-secondary-darker usa-button--unstyled" | ||
label={helpText} | ||
position="top" | ||
> | ||
<USWDSIcon className="margin-left-1" name="info_outline" /> | ||
</Tooltip> | ||
</div> | ||
<Snackbar isVisible={snackbarIsVisible}>{snackbarMessage}</Snackbar> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchSavedQuery2; | ||
export default SearchSavedQuery; |
14 changes: 14 additions & 0 deletions
14
frontend/src/components/user/SearchQuerySaveUserControl.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
|
||
import Snackbar from "src/components/Snackbar"; | ||
|
||
export const useSnackbar = () => { | ||
const [snackbarIsVisible, setSnackbarIsVisible] = useState<boolean>(false); | ||
|
||
const showSnackbar = (visibleTime: number) => { | ||
setSnackbarIsVisible(true); | ||
setTimeout(() => { | ||
setSnackbarIsVisible(false); | ||
}, visibleTime); | ||
}; | ||
|
||
return { snackbarIsVisible, showSnackbar, Snackbar }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
frontend/stories/components/search/SearchSavedQuery.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Meta } from "@storybook/react"; | ||
|
||
import SearchSavedQuery from "src/components/search/SearchSavedQuery"; | ||
|
||
const meta: Meta<typeof SearchSavedQuery> = { | ||
title: "Components/Search/SearchSavedQuery", | ||
component: SearchSavedQuery, | ||
args: { | ||
copyText: "Copy this link to your clipboard", | ||
helpText: "This is a tooltip with some help text", | ||
url: "https://www.example.com?query=example", | ||
snackbarMessage: "This is the success message that will let you know", | ||
}, | ||
}; | ||
export default meta; | ||
|
||
export const Default = { | ||
args: { | ||
snackbarCopyText: | ||
"This is the success message that will let you know. really. really.d", | ||
snackbarMessage: "This is the success message that will let you knowddddd", | ||
}, | ||
}; |