feat: add project languages (#6765)

* feat: add project languages

* fix: update select languages

* fix: select language

* Update UI for project languages

---------

Co-authored-by: Kamran Ahmed <kamranahmed.se@gmail.com>
pull/6812/head
Arik Chakma 5 months ago committed by GitHub
parent 4b7022948a
commit 2b6c326819
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      src/components/Projects/EmptySolutions.tsx
  2. 11
      src/components/Projects/LeavingRoadmapWarningModal.tsx
  3. 100
      src/components/Projects/ListProjectSolutions.tsx
  4. 88
      src/components/Projects/SelectLanguages.tsx
  5. 11
      src/components/Projects/SubmitProjectModal.tsx
  6. 17
      src/pages/projects/[projectId]/solutions.astro

@ -8,7 +8,7 @@ export function EmptySolutions(props: EmptySolutionsProps) {
const { projectId } = props;
return (
<div className="flex min-h-[250px] flex-col items-center justify-center rounded-xl px-5 py-3 sm:px-0 sm:py-20">
<div className="flex min-h-[250px] flex-col items-center justify-center rounded-xl px-5 py-3 sm:px-0 sm:py-20 bg-white border mb-5">
<Blocks className="mb-4 opacity-10 h-14 w-14" />
<h2 className="mb-1 text-lg font-semibold sm:text-xl">
No solutions submitted yet

@ -4,13 +4,13 @@ import { SubmissionRequirement } from './SubmissionRequirement.tsx';
type LeavingRoadmapWarningModalProps = {
onClose: () => void;
onContinue: () => void;
repositoryUrl: string;
};
export function LeavingRoadmapWarningModal(
props: LeavingRoadmapWarningModalProps,
) {
const { onClose, onContinue } = props;
const { onClose, repositoryUrl } = props;
return (
<Modal onClose={onClose} bodyClassName="h-auto p-4">
@ -45,13 +45,14 @@ export function LeavingRoadmapWarningModal(
</p>
</div>
<button
<a
className="inline-flex w-full items-center gap-2 rounded-lg bg-black px-3 py-2.5 text-sm text-white"
onClick={onContinue}
href={repositoryUrl}
target="_blank"
>
<ArrowUpRight className="h-5 w-5" />
Continue to Solution
</button>
</a>
<button
className="absolute right-2.5 top-2.5 text-gray-600 hover:text-black"

@ -13,7 +13,8 @@ import { isLoggedIn } from '../../lib/jwt';
import { showLoginPopup } from '../../lib/popup';
import { VoteButton } from './VoteButton.tsx';
import { GitHubIcon } from '../ReactIcons/GitHubIcon.tsx';
import { cn } from '../../lib/classname.ts';
import { SelectLanguages } from './SelectLanguages.tsx';
import type { ProjectFrontmatter } from '../../lib/project.ts';
export interface ProjectStatusDocument {
_id?: string;
@ -24,6 +25,7 @@ export interface ProjectStatusDocument {
startedAt?: Date;
submittedAt?: Date;
repositoryUrl?: string;
languages?: string[];
upvotes: number;
downvotes: number;
@ -53,15 +55,16 @@ type ListProjectSolutionsResponse = {
type QueryParams = {
p?: string;
l?: string;
};
type PageState = {
currentPage: number;
language: string;
};
const VISITED_SOLUTIONS_KEY = 'visited-project-solutions';
type ListProjectSolutionsProps = {
project: ProjectFrontmatter;
projectId: string;
};
@ -90,27 +93,26 @@ const submittedAlternatives = [
];
export function ListProjectSolutions(props: ListProjectSolutionsProps) {
const { projectId } = props;
const { projectId, project: projectData } = props;
const toast = useToast();
const [pageState, setPageState] = useState<PageState>({
currentPage: 0,
language: '',
});
const [isLoading, setIsLoading] = useState(true);
const [solutions, setSolutions] = useState<ListProjectSolutionsResponse>();
const [alreadyVisitedSolutions, setAlreadyVisitedSolutions] = useState<
Record<string, boolean>
>({});
const [showLeavingRoadmapModal, setShowLeavingRoadmapModal] = useState<
ListProjectSolutionsResponse['data'][number] | null
>(null);
const loadSolutions = async (page = 1) => {
const loadSolutions = async (page = 1, language: string = '') => {
const { response, error } = await httpGet<ListProjectSolutionsResponse>(
`${import.meta.env.PUBLIC_API_URL}/v1-list-project-solutions/${projectId}`,
{
currPage: page,
...(language ? { languages: language } : {}),
},
);
@ -132,7 +134,7 @@ export function ListProjectSolutions(props: ListProjectSolutionsProps) {
return;
}
pageProgressMessage.set('Submitting vote...');
pageProgressMessage.set('Submitting vote');
const { response, error } = await httpPost(
`${import.meta.env.PUBLIC_API_URL}/v1-vote-project/${solutionId}`,
{
@ -172,13 +174,9 @@ export function ListProjectSolutions(props: ListProjectSolutionsProps) {
useEffect(() => {
const queryParams = getUrlParams() as QueryParams;
const alreadyVisitedSolutions = JSON.parse(
localStorage.getItem(VISITED_SOLUTIONS_KEY) || '{}',
);
setAlreadyVisitedSolutions(alreadyVisitedSolutions);
setPageState({
currentPage: +(queryParams.p || '1'),
language: queryParams.l || '',
});
}, []);
@ -188,23 +186,21 @@ export function ListProjectSolutions(props: ListProjectSolutionsProps) {
return;
}
if (pageState.currentPage !== 1) {
if (pageState.currentPage !== 1 || pageState.language !== '') {
setUrlParams({
p: String(pageState.currentPage),
l: pageState.language,
});
} else {
deleteUrlParam('p');
deleteUrlParam('l');
}
loadSolutions(pageState.currentPage).finally(() => {
loadSolutions(pageState.currentPage, pageState.language).finally(() => {
setIsLoading(false);
});
}, [pageState]);
if (isLoading) {
return <LoadingSolutions />;
}
const isEmpty = solutions?.data.length === 0;
if (isEmpty) {
return <EmptySolutions projectId={projectId} />;
@ -213,37 +209,49 @@ export function ListProjectSolutions(props: ListProjectSolutionsProps) {
const leavingRoadmapModal = showLeavingRoadmapModal ? (
<LeavingRoadmapWarningModal
onClose={() => setShowLeavingRoadmapModal(null)}
onContinue={() => {
const visitedSolutions = {
...alreadyVisitedSolutions,
[showLeavingRoadmapModal._id!]: true,
};
localStorage.setItem(
VISITED_SOLUTIONS_KEY,
JSON.stringify(visitedSolutions),
);
window.open(showLeavingRoadmapModal.repositoryUrl, '_blank');
}}
repositoryUrl={showLeavingRoadmapModal?.repositoryUrl!}
/>
) : null;
const selectedLanguage = pageState.language;
return (
<section>
<div className="mb-4 overflow-hidden rounded-lg border bg-white p-3 sm:p-5">
{leavingRoadmapModal}
<div className="relative mb-5 hidden items-center justify-between sm:flex">
<div>
<h1 className="mb-1 text-xl font-semibold">
{projectData.title} Solutions
</h1>
<p className="text-sm text-gray-500">{projectData.description}</p>
</div>
{!isLoading && (
<SelectLanguages
projectId={projectId}
selectedLanguage={selectedLanguage}
onSelectLanguage={(language) => {
setPageState((prev) => ({
...prev,
language: prev.language === language ? '' : language,
}));
}}
/>
)}
</div>
{isLoading ? (
<LoadingSolutions />
) : (
<>
<div className="flex min-h-[500px] flex-col divide-y divide-gray-100">
{solutions?.data.map((solution, counter) => {
const isVisited = alreadyVisitedSolutions[solution._id!];
const avatar = solution.user.avatar || '';
return (
<div
key={solution._id}
className={
'flex flex-col justify-between gap-2 py-2 text-sm text-gray-500 sm:flex-row sm:items-center sm:gap-0'
}
className="flex flex-col gap-2 py-2 text-sm text-gray-500"
>
<div className="flex flex-col justify-between gap-2 text-sm text-gray-500 sm:flex-row sm:items-center sm:gap-0">
<div className="flex items-center gap-1.5">
<img
src={
@ -268,7 +276,7 @@ export function ListProjectSolutions(props: ListProjectSolutionsProps) {
</div>
<div className="flex items-center justify-end gap-1">
<span className="flex overflow-hidden rounded-full border">
<span className="flex shrink-0 overflow-hidden rounded-full border">
<VoteButton
icon={ThumbsUp}
isActive={solution?.voteType === 'upvote'}
@ -289,18 +297,16 @@ export function ListProjectSolutions(props: ListProjectSolutionsProps) {
/>
</span>
<a
<button
className="ml-1 flex items-center gap-1 rounded-full border px-2 py-1 text-xs text-black transition-colors hover:border-black hover:bg-black hover:text-white"
onClick={(e) => {
e.preventDefault();
onClick={() => {
setShowLeavingRoadmapModal(solution);
}}
target="_blank"
href={solution.repositoryUrl}
>
<GitHubIcon className="h-4 w-4 text-current" />
Visit Solution
</a>
</button>
</div>
</div>
</div>
);
@ -323,6 +329,8 @@ export function ListProjectSolutions(props: ListProjectSolutionsProps) {
/>
</div>
)}
</section>
</>
)}
</div>
);
}

@ -0,0 +1,88 @@
import { useEffect, useRef, useState } from 'react';
import { useOutsideClick } from '../../hooks/use-outside-click';
import { httpGet } from '../../lib/http';
import { useToast } from '../../hooks/use-toast';
import { ChevronDown, X } from 'lucide-react';
type SelectLanguagesProps = {
projectId: string;
selectedLanguage: string;
onSelectLanguage: (language: string) => void;
};
export function SelectLanguages(props: SelectLanguagesProps) {
const { projectId, onSelectLanguage, selectedLanguage } = props;
const dropdownRef = useRef<HTMLDivElement>(null);
const toast = useToast();
const [distinctLanguages, setDistinctLanguages] = useState<string[]>([]);
const [isOpen, setIsOpen] = useState(false);
const loadDistinctLanguages = async () => {
const { response, error } = await httpGet<string[]>(
`${import.meta.env.PUBLIC_API_URL}/v1-list-project-languages/${projectId}`,
);
if (error || !response) {
toast.error(error?.message || 'Failed to load project languages');
return;
}
setDistinctLanguages(response);
};
useOutsideClick(dropdownRef, () => {
setIsOpen(false);
});
useEffect(() => {
loadDistinctLanguages().finally(() => {});
}, []);
return (
<div className="relative flex">
<button
className="flex items-center gap-1 rounded-md border border-gray-300 py-1.5 pl-3 pr-2 text-xs font-medium text-gray-900"
onClick={() => setIsOpen(!isOpen)}
>
{selectedLanguage || 'Select Language'}
<ChevronDown className="ml-1 h-4 w-4" />
</button>
{selectedLanguage && (
<button
className="ml-1 text-red-500 text-xs border border-red-500 rounded-md px-2 py-1"
onClick={() => onSelectLanguage('')}
>
Clear
</button>
)}
{isOpen && (
<div
className="absolute right-0 top-full z-10 w-full min-w-[200px] max-w-[200px] translate-y-1.5 overflow-hidden rounded-md border border-gray-300 bg-white p-1 shadow-lg"
ref={dropdownRef}
>
{distinctLanguages.map((language) => {
const isSelected = selectedLanguage === language;
return (
<button
key={language}
className="flex w-full items-center rounded-md px-4 py-1.5 text-left text-sm text-gray-700 hover:bg-gray-100 aria-selected:bg-gray-100"
onClick={() => {
onSelectLanguage(language);
setIsOpen(false);
}}
aria-selected={isSelected}
>
{language}
</button>
);
})}
</div>
)}
</div>
);
}

@ -170,10 +170,19 @@ export function SubmitProjectModal(props: SubmitProjectModalProps) {
projectUrlExists: 'success',
});
const languagesUrl = `${mainApiUrl}/languages`;
const languagesResponse = await fetch(languagesUrl);
let languages: string[] = [];
if (languagesResponse.ok) {
const languagesData = await languagesResponse.json();
languages = Object.keys(languagesData || {})?.slice(0, 4);
}
const submitProjectUrl = `${import.meta.env.PUBLIC_API_URL}/v1-submit-project/${projectId}`;
const { response: submitResponse, error } =
await httpPost<SubmitProjectResponse>(submitProjectUrl, {
repositoryUrl: repoUrl,
languages,
});
if (error || !submitResponse) {
@ -272,7 +281,7 @@ export function SubmitProjectModal(props: SubmitProjectModalProps) {
<button
type="submit"
className="mt-2 w-full rounded-lg bg-black p-2 font-medium text-white disabled:opacity-50 text-sm"
className="mt-2 w-full rounded-lg bg-black p-2 text-sm font-medium text-white disabled:opacity-50"
disabled={isLoading}
>
{isLoading ? 'Verifying...' : 'Verify and Submit'}

@ -49,18 +49,11 @@ const githubUrl = `https://github.com/kamranahmedse/developer-roadmap/tree/maste
<div class='container'>
<ProjectTabs projectId={projectId} activeTab='solutions' />
<div class='mb-4 overflow-hidden rounded-lg border bg-white p-3 sm:p-5'>
<div class='relative mb-5 hidden sm:block'>
<h1 class='mb-1 text-xl font-semibold'>
{projectData.title} Solutions
</h1>
<p class='text-sm text-gray-500'>
{projectData.description}
</p>
</div>
<ListProjectSolutions projectId={projectId} client:load />
</div>
<ListProjectSolutions
project={projectData}
projectId={projectId}
client:load
/>
</div>
</div>
</BaseLayout>

Loading…
Cancel
Save