Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Website: User location based flag #986

Merged
merged 12 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion ui/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { clsx, type ClassValue } from 'clsx';
import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

/**
* We use the files from GitHub instead of the package so that donations from new countries are automatically supported.
*/
export const getFlagImageURL = (country: string) =>
`https://raw.githubusercontent.com/lipis/flag-icons/a87d8b256743c9b0df05f20de2c76a7975119045/flags/1x1/${country.toLowerCase()}.svg`;
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@tanstack/react-query": "^5.59.0",
"@types/js-cookie": "^3.0.6",
"@vercel/analytics": "^1.3.1",
"@vercel/functions": "^1.5.2",
"@vimeo/player": "^2.24.0",
"accept-language-parser": "^1.5.0",
"classnames": "^2.5.1",
Expand Down
19 changes: 19 additions & 0 deletions website/src/app/[lang]/[region]/(website)/me/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DONATION_CERTIFICATE_FIRESTORE_PATH } from '@socialincome/shared/src/ty
import { Employer, EMPLOYERS_FIRESTORE_PATH } from '@socialincome/shared/src/types/employers';
import { USER_FIRESTORE_PATH } from '@socialincome/shared/src/types/user';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Geo } from '@vercel/functions';
import { addDoc, collection, deleteDoc, doc, getDocs, query, Timestamp, updateDoc, where } from 'firebase/firestore';
import { useContext } from 'react';
import { useFirestore } from 'reactfire';
Expand Down Expand Up @@ -170,3 +171,21 @@ export const useAddEmployer = () => {
await queryClient.invalidateQueries({ queryKey: ['me', 'employers'] });
};
};

export const useGeolocation = () => {
dnhn marked this conversation as resolved.
Show resolved Hide resolved
const api = useApi();
const {
data: geolocation,
isLoading,
error,
} = useQuery({
queryKey: ['geolocation'],
queryFn: async () => {
const response = await api.get('/api/geolocation');
return (await response.json()) as Geo;
},
staleTime: Infinity,
});

return { geolocation, isLoading, error };
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
'use client';

import { Button, Card, CardContent, Typography } from '@socialincome/ui';
import { getFlagImageURL } from '@socialincome/ui/src/lib/utils';
import { Children, PropsWithChildren, useState } from 'react';

/**
* We use the files from GitHub instead of the package so that donations from new countries are automatically supported.
*/
const getFlagImageURL = (country: string) => {
return `https://raw.githubusercontent.com/lipis/flag-icons/a87d8b256743c9b0df05f20de2c76a7975119045/flags/1x1/${country.toLowerCase()}.svg`;
};

type CountryCardProps = {
country: string;
translations: {
Expand Down
1 change: 1 addition & 0 deletions website/src/app/[lang]/[region]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const CURRENCY_COOKIE = 'si_currency';

export interface DefaultParams {
lang: WebsiteLanguage;
country?: string;
region: WebsiteRegion;
}

Expand Down
10 changes: 10 additions & 0 deletions website/src/app/api/geolocation/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { handleApiError } from '@/app/api/auth';
import { geolocation } from '@vercel/functions';

export async function GET(request: Request) {
try {
return Response.json(geolocation(request));
} catch (error: any) {
return handleApiError(error);
}
dnhn marked this conversation as resolved.
Show resolved Hide resolved
}
dnhn marked this conversation as resolved.
Show resolved Hide resolved
57 changes: 48 additions & 9 deletions website/src/components/navbar/navbar-client.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { DefaultParams } from '@/app/[lang]/[region]';
import { getFlagComponentByCurrency } from '@/components/country-flags';
import { useGeolocation } from '@/app/[lang]/[region]/(website)/me/hooks';
import { DonateIcon } from '@/components/logos/donate-icon';
import { SIAnimatedLogo } from '@/components/logos/si-animated-logo';
import { SIIcon } from '@/components/logos/si-icon';
Expand All @@ -11,8 +11,10 @@ import { useGlobalStateProvider } from '@/components/providers/global-state-prov
import { WebsiteCurrency, WebsiteLanguage, WebsiteRegion } from '@/i18n';
import { Bars3Icon, CheckIcon, ChevronLeftIcon, XMarkIcon } from '@heroicons/react/24/outline';
import { Typography } from '@socialincome/ui';
import { getFlagImageURL } from '@socialincome/ui/src/lib/utils';
import classNames from 'classnames';
import _ from 'lodash';
import Image from 'next/image';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import { twMerge } from 'tailwind-merge';
Expand Down Expand Up @@ -55,7 +57,16 @@ type NavbarProps = {
sections?: NavigationSection[];
} & DefaultParams;

const MobileNavigation = ({ lang, region, languages, regions, currencies, navigation, translations }: NavbarProps) => {
const MobileNavigation = ({
lang,
country,
region,
languages,
regions,
currencies,
navigation,
translations,
}: NavbarProps) => {
const [visibleSection, setVisibleSection] = useState<
'main' | 'our-work' | 'about-us' | 'transparency' | 'i18n' | null
>(null);
Expand Down Expand Up @@ -188,7 +199,6 @@ const MobileNavigation = ({ lang, region, languages, regions, currencies, naviga
break;
case 'main':
default:
const Flag = getFlagComponentByCurrency(currency);
const ourWork = navigation![0];
const aboutUs = navigation![1];
const transparency = navigation![2];
Expand All @@ -211,7 +221,17 @@ const MobileNavigation = ({ lang, region, languages, regions, currencies, naviga
{translations.myProfile}
</NavbarLink>
<div className="flex-inline flex items-center">
{Flag && <Flag className="mx-3 h-6 w-6 rounded-full" />}
{country && (
<Image
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also do it like this in website/src/components/ui/currency-selector.tsx? Then we could uninstall the package. Wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I’ll update it.

Copy link
Member Author

@dnhn dnhn Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The library we are using https://raw.githubusercontent.com/lipis/flag-icons/a87d8b256743c9b0df05f20de2c76a7975119045/flags/… only accepts country code, do you have any library suggestions for currency flags?

I would work on this in another PR since this involves with other components.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean currency icons? Otherwise, we would have to create some mapping to map currencies to countries (similiar to the countryToCurrency mapping in currency.ts).

FYI – I created an MR (#992) to use a middleware to set the country code. Let me know what you think :)

src={getFlagImageURL(country)}
width={24}
height={24}
alt=""
priority
unoptimized
className="mx-3 rounded-full"
/>
)}
dnhn marked this conversation as resolved.
Show resolved Hide resolved
<Typography as="button" className="text-2xl font-medium" onClick={() => setVisibleSection('i18n')}>
{currency} / {languages.find((l) => l.code === language)?.translation}
</Typography>
Expand Down Expand Up @@ -250,9 +270,17 @@ const MobileNavigation = ({ lang, region, languages, regions, currencies, naviga
);
};

const DesktopNavigation = ({ lang, region, languages, regions, currencies, navigation, translations }: NavbarProps) => {
const DesktopNavigation = ({
lang,
country,
region,
languages,
regions,
currencies,
navigation,
translations,
}: NavbarProps) => {
let { currency, setCurrency, setLanguage, setRegion } = useI18n();
const Flag = getFlagComponentByCurrency(currency);
const NavbarLink = ({ href, children, className }: { href: string; children: string; className?: string }) => (
<Link href={href} className={twMerge('hover:text-accent text-lg', className)}>
{children}
Expand Down Expand Up @@ -321,7 +349,17 @@ const DesktopNavigation = ({ lang, region, languages, regions, currencies, navig
</div>
<div className="group/i18n flex h-full flex-1 shrink-0 basis-1/4 flex-col">
<div className="flex flex-row items-baseline justify-end">
{Flag && <Flag className="m-auto mx-2 h-5 w-5 rounded-full" />}
{country && (
<Image
src={getFlagImageURL(country)}
width={20}
height={20}
alt=""
priority
unoptimized
className="m-auto mx-2 rounded-full"
/>
)}
<Typography size="lg">{languages.find((l) => l.code === lang)?.translation}</Typography>
</div>
<div className="ml-auto mt-6 hidden h-full grid-cols-1 justify-items-start gap-2 text-left opacity-0 group-hover/navbar:grid group-hover/i18n:opacity-100 lg:grid-cols-[repeat(3,auto)] lg:justify-items-end lg:gap-8">
Expand Down Expand Up @@ -380,11 +418,12 @@ const DesktopNavigation = ({ lang, region, languages, regions, currencies, navig

export function NavbarClient(props: NavbarProps) {
const { backgroundColor } = useGlobalStateProvider();
const { geolocation } = useGeolocation();

return (
<nav className={twMerge('theme-blue group/navbar fixed inset-x-0 top-0 z-20 flex flex-col', backgroundColor)}>
<DesktopNavigation {...props} />
<MobileNavigation {...props} />
<DesktopNavigation {...props} country={geolocation?.country} />
<MobileNavigation {...props} country={geolocation?.country} />
</nav>
);
}
Loading