Skip to content

Commit

Permalink
fix: implement fuzzy search with locale switcher and settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
AmruthPillai committed Nov 24, 2023
1 parent 016e9ac commit e9d888b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 75 deletions.
71 changes: 71 additions & 0 deletions apps/client/src/components/locale-combobox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { t } from "@lingui/macro";
import { Check } from "@phosphor-icons/react";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
ScrollArea,
} from "@reactive-resume/ui";
import { cn } from "@reactive-resume/utils";
import fuzzy from "fuzzy";
import { useMemo, useState } from "react";

import { useLanguages } from "../services/resume/translation";

type Props = {
value: string;
onValueChange: (locale: string) => void;
};

export const LocaleCombobox = ({ value, onValueChange }: Props) => {
const { languages } = useLanguages();
const [search, setSearch] = useState("");

const options = useMemo(() => {
return fuzzy.filter(search, languages, {
extract: (lang) => `${lang.name} ${lang.locale}`,
});
}, [search, languages]);

return (
<Command shouldFilter={false}>
<CommandInput
value={search}
onValueChange={setSearch}
placeholder={t`Search for a language`}
/>
<CommandEmpty>{t`No results found`}</CommandEmpty>
<CommandGroup>
<ScrollArea orientation="vertical">
<div className="max-h-60">
{options.map(({ original }) => (
<CommandItem
key={original.locale}
value={original.locale.trim().toLowerCase()}
onSelect={async (selectedValue) => {
const result = options.find(
({ original }) => original.locale.trim().toLowerCase() === selectedValue,
);

if (!result) return null;

onValueChange(result.original.locale);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4 opacity-0",
value === original.locale && "opacity-100",
)}
/>
{original.name} <span className="ml-1 text-xs opacity-50">({original.locale})</span>
</CommandItem>
))}
</div>
</ScrollArea>
</CommandGroup>
</Command>
);
};
67 changes: 10 additions & 57 deletions apps/client/src/components/locale-switch.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
import { t } from "@lingui/macro";
import { useLingui } from "@lingui/react";
import { Check, Translate } from "@phosphor-icons/react";
import {
Button,
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
Popover,
PopoverContent,
PopoverTrigger,
ScrollArea,
} from "@reactive-resume/ui";
import { cn } from "@reactive-resume/utils";
import { Translate } from "@phosphor-icons/react";
import { Button, Popover, PopoverContent, PopoverTrigger } from "@reactive-resume/ui";
import { useState } from "react";

import { changeLanguage } from "../providers/locale";
import { useLanguages } from "../services/resume/translation";
import { LocaleCombobox } from "./locale-combobox";

export const LocaleSwitch = () => {
const { i18n } = useLingui();
const { languages } = useLanguages();

const [open, setOpen] = useState(false);

const options = languages.map((language) => ({
label: language.name,
value: language.locale,
}));

return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
Expand All @@ -38,40 +18,13 @@ export const LocaleSwitch = () => {
</Button>
</PopoverTrigger>
<PopoverContent align="end" className="p-0">
<Command>
<CommandInput placeholder={t`Search for a language`} />
<CommandEmpty>{t`No results found`}</CommandEmpty>
<CommandGroup>
<ScrollArea orientation="vertical">
<div className="max-h-60">
{options.map((option) => (
<CommandItem
key={option.value}
value={option.value.toLowerCase().trim()}
onSelect={async (selectedValue) => {
const option = options.find(
(option) => option.value.toLowerCase().trim() === selectedValue,
);

if (!option) return null;

await changeLanguage(option.value);
setOpen(false);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4 opacity-0",
i18n.locale === option.value && "opacity-100",
)}
/>
{option.label} <span className="ml-1 text-xs opacity-50">({option.value})</span>
</CommandItem>
))}
</div>
</ScrollArea>
</CommandGroup>
</Command>
<LocaleCombobox
value={i18n.locale}
onValueChange={async (locale) => {
await changeLanguage(locale);
setOpen(false);
}}
/>
</PopoverContent>
</Popover>
);
Expand Down
17 changes: 2 additions & 15 deletions apps/client/src/pages/dashboard/settings/_sections/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";

import { useLanguages } from "@/client/services/resume/translation";
import { LocaleCombobox } from "@/client/components/locale-combobox";
import { useUpdateUser, useUser } from "@/client/services/user";

const formSchema = z.object({
Expand All @@ -21,7 +21,6 @@ type FormValues = z.infer<typeof formSchema>;

export const ProfileSettings = () => {
const { user } = useUser();
const { languages } = useLanguages();
const { theme, setTheme } = useTheme();
const { updateUser, loading } = useUpdateUser();

Expand Down Expand Up @@ -95,19 +94,7 @@ export const ProfileSettings = () => {
<FormItem>
<FormLabel>{t`Language`}</FormLabel>
<div className="w-full">
<Combobox
{...field}
value={field.value}
onValueChange={field.onChange}
options={languages.map(({ locale, name }) => ({
label: (
<>
{name} <span className="ml-1 text-xs opacity-50">({locale})</span>
</>
),
value: locale,
}))}
/>
<LocaleCombobox value={field.value} onValueChange={field.onChange} />
</div>
<FormDescription>
<span>
Expand Down
4 changes: 2 additions & 2 deletions apps/client/src/pages/home/sections/faq/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ const Question4 = () => {
className="no-underline"
href={`https://crowdin.com/translate/reactive-resume/all/en-${language.editorCode}`}
>
<div className="relative bg-secondary-accent font-medium">
<div className="relative bg-secondary-accent font-medium transition-colors hover:bg-primary hover:text-background">
<span className="px-2 py-1">{language.name}</span>

{language.progress && (
{language.progress !== undefined && (
<span
className={cn(
"inset-0 bg-warning px-1.5 py-1 text-xs text-white",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@reactive-resume/source",
"description": "A free and open-source resume builder that simplifies the process of creating, updating, and sharing your resume.",
"version": "4.0.0",
"version": "4.0.1",
"license": "MIT",
"private": true,
"author": {
Expand Down Expand Up @@ -187,6 +187,7 @@
"deepmerge": "^4.3.1",
"file-saver": "^2.0.5",
"framer-motion": "^10.16.5",
"fuzzy": "^0.1.3",
"helmet": "^7.1.0",
"immer": "^10.0.3",
"ioredis": "^5.3.2",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit e9d888b

Please sign in to comment.