-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist language when crossing plans
- Loading branch information
Showing
6 changed files
with
105 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { stripTrailingSlash } from 'common/utils'; | ||
|
||
describe('stripTrailingSlash', () => { | ||
it('strips trailing slashes', () => { | ||
expect(stripTrailingSlash('/')).toBe(''); | ||
expect(stripTrailingSlash('foobar/')).toBe('foobar'); | ||
expect(stripTrailingSlash('foobar/lorem')).toBe('foobar/lorem'); | ||
expect(stripTrailingSlash('https://www.foobar.com/foobar/lorem/')).toBe( | ||
'https://www.foobar.com/foobar/lorem' | ||
); | ||
}); | ||
}); |
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,19 @@ | ||
import { useTranslation } from 'common/i18n'; | ||
import { stripTrailingSlash } from 'common/utils'; | ||
import { usePlan } from 'context/plan'; | ||
|
||
/** | ||
* Applies the locale to the end of the link, this can't | ||
* yet be used in cases where the locale should be injected | ||
* to the middle of the path e.g. /<plan>/<locale>/actions/ | ||
*/ | ||
export const useLocalizedLink = (link: string) => { | ||
const { i18n } = useTranslation(); | ||
const plan = usePlan(); | ||
|
||
if (plan.primaryLanguage === i18n.language) { | ||
return link; | ||
} | ||
|
||
return `${stripTrailingSlash(link)}/${i18n.language}`; | ||
}; |
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,59 @@ | ||
import styled from 'styled-components'; | ||
|
||
import { PlanContextType } from 'context/plan'; | ||
import { useTheme } from 'common/theme'; | ||
import PlanChip from './PlanChip'; | ||
import { useLocalizedLink } from 'common/hooks/localize-link'; | ||
|
||
const PlanDropdownItem = styled.a` | ||
display: block; | ||
padding: 0.25rem 0; | ||
margin: 0 0.5rem 0.5rem; | ||
border: 1px solid ${(props) => props.theme.themeColors.light}; | ||
border-radius: 0.5rem; | ||
text-decoration: none !important; | ||
&:last-child { | ||
margin-bottom: 0; | ||
} | ||
&:hover { | ||
background: ${(props) => props.theme.themeColors.light}; | ||
border-color: ${(props) => props.theme.themeColors.light}; | ||
text-decoration: none; | ||
} | ||
`; | ||
|
||
interface Props { | ||
plan: PlanContextType | NonNullable<PlanContextType['allRelatedPlans'][0]>; | ||
} | ||
|
||
const PlanLink = ({ plan }: Props) => { | ||
const theme = useTheme(); | ||
const localizedPlanUrl = useLocalizedLink(plan.viewUrl ?? ''); | ||
|
||
if (!plan.viewUrl) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<PlanDropdownItem | ||
key={plan.identifier} | ||
href={localizedPlanUrl} | ||
type="button" | ||
tabIndex={0} | ||
role="menuitem" | ||
> | ||
<PlanChip | ||
planImage={plan.image?.rendition?.src} | ||
planShortName={plan.shortName} | ||
organization={ | ||
theme.settings?.multiplan?.hideLongPlanNames ? undefined : plan.name | ||
} | ||
size="md" | ||
/> | ||
</PlanDropdownItem> | ||
); | ||
}; | ||
|
||
export default PlanLink; |
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