-
Notifications
You must be signed in to change notification settings - Fork 38
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
❇️ Language filter on moderation queue #25
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d4f6679
:sparkles: Use dids in all links instead of handles
foysalit 6fcfdf2
:bug: Maintain search params when redirecting
foysalit fc77ab2
:bug: Wait for auth before attempting to resolve handle
foysalit 8fee64a
:sparkles: Add language filter to moderation queue
foysalit 7b229a9
:sparkles: Move lang filter to use tags
foysalit 461ed4a
:bug: Unique key for option
foysalit 4256dcc
:sparkles: Add tag display and emit form
foysalit 0f888ad
Merge branch 'main' of github.com:bluesky-social/ozone-ui into langua…
foysalit 74aa1c7
:sparkles: Allow multiple language inclusion and exclusion from the l…
foysalit 540c44c
:lipstick: Fix style of the lang picker button
foysalit 4cc7ced
:arrow_up: Upgrade atproto api version
foysalit f479358
:lipstick: Add inline comments and refactor
foysalit 5dc2b91
:lipstick: Improve the language picker styling and add help text
foysalit 8100f85
:bug: Use tag filters in the query key to refresh after lang selection
foysalit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,216 @@ | ||
import { LANGUAGES_MAP_CODE2 } from '@/lib/locale/languages' | ||
import { Popover, Transition } from '@headlessui/react' | ||
import { CheckIcon, ChevronDownIcon } from '@heroicons/react/20/solid' | ||
import { useSearchParams, useRouter, usePathname } from 'next/navigation' | ||
import { ActionButton } from './buttons' | ||
|
||
// Please make sure that any item added here exists in LANGUAGES_MAP_CODE2 or add it there first | ||
const availableLanguageCodes = [ | ||
'en', | ||
'es', | ||
'fr', | ||
'de', | ||
'it', | ||
'ja', | ||
'ko', | ||
'pt', | ||
'ru', | ||
] | ||
|
||
const SelectionTitle = ({ | ||
includedLanguages, | ||
excludedLanguages, | ||
}: { | ||
includedLanguages: string[] | ||
excludedLanguages: string[] | ||
}) => { | ||
if (includedLanguages.length === 0 && excludedLanguages.length === 0) { | ||
return ( | ||
<span className="text-gray-700 dark:text-gray-100">All Languages</span> | ||
) | ||
} | ||
|
||
const includedNames = includedLanguages.map( | ||
(lang) => LANGUAGES_MAP_CODE2[lang].name, | ||
) | ||
const excludedNames = excludedLanguages.map( | ||
(lang) => LANGUAGES_MAP_CODE2[lang].name, | ||
) | ||
|
||
return ( | ||
<> | ||
<span className="text-gray-700 dark:text-gray-100"> | ||
{includedNames.join(', ')} | ||
</span> | ||
{includedNames.length > 0 && excludedNames.length > 0 && ( | ||
<span className="text-gray-700 dark:text-gray-100 mx-1">|</span> | ||
)} | ||
<span className="text-gray-700 dark:text-gray-100"> | ||
{excludedNames.map((name, i) => ( | ||
<s key={name}> | ||
{name} | ||
{i < excludedNames.length - 1 && ', '} | ||
</s> | ||
))} | ||
</span> | ||
</> | ||
) | ||
} | ||
|
||
// Tags can be any arbitrary string, and lang tags are prefixed with lang:[code2] so we use this to get the lang code from tag string | ||
const getLangFromTag = (tag: string) => tag.split(':')[1] | ||
|
||
export const LanguagePicker: React.FC = () => { | ||
const searchParams = useSearchParams() | ||
const router = useRouter() | ||
const pathname = usePathname() | ||
|
||
const tagsParam = searchParams.get('tags') | ||
const excludeTagsParam = searchParams.get('excludeTags') | ||
const tags = tagsParam?.split(',') || [] | ||
const excludedTags = excludeTagsParam?.split(',') || [] | ||
const includedLanguages = tags | ||
.filter((tag) => tag.startsWith('lang:')) | ||
.map(getLangFromTag) | ||
const excludedLanguages = excludedTags | ||
.filter((tag) => tag.startsWith('lang:')) | ||
.map(getLangFromTag) | ||
|
||
const toggleLanguage = (section: 'include' | 'exclude', newLang: string) => { | ||
const nextParams = new URLSearchParams(searchParams) | ||
const urlQueryKey = section === 'include' ? 'tags' : 'excludeTags' | ||
const selectedLanguages = | ||
section === 'include' ? includedLanguages : excludedLanguages | ||
const selectedLanguageTags = section === 'include' ? tags : excludedTags | ||
|
||
if (selectedLanguages.includes(newLang)) { | ||
const newTags = selectedLanguageTags.filter( | ||
(tag) => `lang:${newLang}` !== tag, | ||
) | ||
if (newTags.length) { | ||
nextParams.set(urlQueryKey, newTags.join(',')) | ||
} else { | ||
nextParams.delete(urlQueryKey) | ||
} | ||
} else { | ||
nextParams.set( | ||
urlQueryKey, | ||
[...selectedLanguageTags, `lang:${newLang}`].join(','), | ||
) | ||
} | ||
|
||
router.push((pathname ?? '') + '?' + nextParams.toString()) | ||
} | ||
const clearLanguages = () => { | ||
const nextParams = new URLSearchParams(searchParams) | ||
|
||
nextParams.delete('tags') | ||
nextParams.delete('excludeTags') | ||
router.push((pathname ?? '') + '?' + nextParams.toString()) | ||
} | ||
|
||
return ( | ||
<Popover> | ||
{({ open, close }) => ( | ||
<> | ||
<Popover.Button className="text-sm flex flex-row items-center"> | ||
<SelectionTitle {...{ includedLanguages, excludedLanguages }} /> | ||
<ChevronDownIcon className="dark:text-gray-50 w-4 h-4" /> | ||
</Popover.Button> | ||
|
||
{/* Use the `Transition` component. */} | ||
<Transition | ||
show={open} | ||
enter="transition duration-100 ease-out" | ||
enterFrom="transform scale-95 opacity-0" | ||
enterTo="transform scale-100 opacity-100" | ||
leave="transition duration-75 ease-out" | ||
leaveFrom="transform scale-100 opacity-100" | ||
leaveTo="transform scale-95 opacity-0" | ||
> | ||
<Popover.Panel className="absolute left-0 z-10 mt-1 flex w-screen max-w-max -translate-x-1/5 px-4"> | ||
<div className="w-fit-content flex-auto rounded bg-white dark:bg-slate-800 p-4 text-sm leading-6 shadow-lg dark:shadow-slate-900 ring-1 ring-gray-900/5"> | ||
<div className="flex flex-row gap-4 text-gray-700 dark:text-gray-100"> | ||
<LanguageList | ||
disabled={excludedLanguages} | ||
selected={includedLanguages} | ||
header="Include Languages" | ||
onSelect={(lang) => toggleLanguage('include', lang)} | ||
/> | ||
<LanguageList | ||
disabled={includedLanguages} | ||
selected={excludedLanguages} | ||
header="Exclude Languages" | ||
onSelect={(lang) => toggleLanguage('exclude', lang)} | ||
/> | ||
</div> | ||
|
||
<p className="py-2 block max-w-xs text-gray-500 dark:text-gray-300 text-xs"> | ||
Note: <i>When multiple languages are selected, only subjects that are | ||
tagged with <b>all</b> of those languages will be | ||
included/excluded.</i> | ||
</p> | ||
{(includedLanguages.length > 0 || | ||
excludedLanguages.length > 0) && ( | ||
<div className="flex flex-row mt-2"> | ||
<ActionButton | ||
size="xs" | ||
appearance="outlined" | ||
onClick={() => { | ||
clearLanguages() | ||
close() | ||
}} | ||
> | ||
<span className="text-xs">Clear All</span> | ||
</ActionButton> | ||
</div> | ||
)} | ||
</div> | ||
</Popover.Panel> | ||
</Transition> | ||
</> | ||
)} | ||
</Popover> | ||
) | ||
} | ||
|
||
const LanguageList = ({ | ||
header, | ||
onSelect, | ||
selected = [], | ||
disabled = [], | ||
}: { | ||
selected: string[] | ||
disabled: string[] | ||
header: string | ||
onSelect: (lang: string) => void | ||
}) => { | ||
return ( | ||
<div> | ||
<h4 className="text-gray-900 dark:text-gray-200 border-b border-gray-300 mb-2 pb-1"> | ||
{header} | ||
</h4> | ||
<div className="flex flex-col items-start"> | ||
{availableLanguageCodes.map((code2) => { | ||
const isDisabled = disabled.includes(code2) | ||
return ( | ||
<button | ||
className={`w-full flex flex-row items-center justify-between ${ | ||
isDisabled | ||
? 'text-gray-400' | ||
: 'text-gray-700 dark:text-gray-100' | ||
}`} | ||
onClick={() => !isDisabled && onSelect(code2)} | ||
key={code2} | ||
> | ||
{LANGUAGES_MAP_CODE2[code2].name} | ||
{selected.includes(code2) && ( | ||
<CheckIcon className="h-4 w-4 text-green-700" /> | ||
)} | ||
</button> | ||
) | ||
})} | ||
</div> | ||
</div> | ||
) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there anything that guarantees that
LANGUAGES_MAP_CODE2[code2]
will exist? Maybe we could just document nearavailableLanguageCodes
that it must be a subset ofLANGUAGES_MAP_CODE2
. Alternately we could mark certain items inLANGUAGES_MAP_CODE2
as "available".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah documenting
availableLanguageCodes
should do the job because the languages I've added are the ones that bryan suggested and overtime we may add more but even then, theLANGUAGES_MAP_CODE2
is quite comprehensive so I don't see a lang code ever missing there.