import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { Loading, IconAlt } from '@veupathdb/wdk-client/lib/Components'; import { makeClassNameHelper } from '@veupathdb/wdk-client/lib/Utils/ComponentUtils'; import { combineClassNames } from 'ebrc-client/components/homepage/Utils'; import { useCommunitySiteRootUrl, useCommunitySiteContentProjectUrl } from 'ebrc-client/hooks/staticData'; import { ContentError } from './ContentError'; import './WorkshopExercises.scss'; const cx = makeClassNameHelper('vpdb-WorkshopExercises'); const cardListCx = makeClassNameHelper('vpdb-CardList'); const bgDarkCx = makeClassNameHelper('vpdb-BgDark'); const bgWashCx = makeClassNameHelper('vpdb-BgWash'); // FIXME This prefix should be added on the "Jekyll side" const WORKSHOP_EXERCISES_PREFIX = 'https://workshop.eupathdb.org'; const WORKSHOP_EXERCISES_URL_SEGMENT = 'workshop_exercises.json'; const FILL_ME_IN = 'FILL ME IN'; type WorkshopExercisesResponseData = { cards: CardResponseData[] }; type CardResponseData = { card: string, description: string | null, links: LinkResponseData[] }; type LinkResponseData = { name: string, path: string, description: string, }; type CardMetadata = { cardOrder: string[], cardEntries: Record }; type CardEntry = { title: string, description: string, exercises: ExerciseEntry[] }; type ExerciseEntry = { title: string, url: string, description: string }; type Result = | { status: 'ok', value: T } | { status: 'error', message: string } function useCardMetadata(): Result | undefined { const communitySiteUrl = useCommunitySiteContentProjectUrl(); const [ workshopExercisesResponseData, setWorkshopExercisesResponseData ] = useState(undefined); const [ workshopExercisesResponseError, setWorkshopExercisesResponseError ] = useState(); useEffect(() => { if (communitySiteUrl != null) { (async () => { try { const response = await fetch(`https://${communitySiteUrl}/${WORKSHOP_EXERCISES_URL_SEGMENT}`, { mode: 'cors' }); if (response.ok) { // FIXME Validate this JSON using a Decoder const responseData = await response.json() as WorkshopExercisesResponseData; setWorkshopExercisesResponseData(responseData); } else { setWorkshopExercisesResponseError(response.statusText); } } catch (error) { setWorkshopExercisesResponseError(error.message); } })(); } }, [ communitySiteUrl ]); const cardMetadata = useMemo( () => workshopExercisesResponseData && { cardOrder: workshopExercisesResponseData.cards.map(({ card }) => card), cardEntries: workshopExercisesResponseData.cards.reduce( (memo, { card, description, links }) => ({ ...memo, [card]: { title: card, description: description == null ? FILL_ME_IN : description, exercises: links.map(({ name, path, description }) => ({ title: name, url: path, description })) } }), { } as Record ) }, [ workshopExercisesResponseData ] ); return workshopExercisesResponseError != null ? { status: 'error', message: workshopExercisesResponseError } : cardMetadata != null ? { status: 'ok', value: cardMetadata } : undefined; } export const WorkshopExercises = () => { const cardMetadata = useCardMetadata(); const [ isExpanded, setIsExpanded ] = useState(false); const toggleExpansion = useCallback((e: React.MouseEvent) => { e.preventDefault(); setIsExpanded(!isExpanded); }, [ isExpanded ]) return (
{ !cardMetadata ? : cardMetadata.status === 'error' ? : }
); }; type CardListProps = { cardMetadata: CardMetadata; isExpanded: boolean; }; const CardList = ({ cardMetadata: { cardOrder, cardEntries }, isExpanded }: CardListProps) =>
{cardOrder.map( cardKey => )}
; type CardProps = { entry: CardEntry; }; export function Card({ entry }: CardProps) { const communitySiteUrl = useCommunitySiteRootUrl(); return (

{entry.title}

{entry.description}

    { entry.exercises.map( // FIXME: Dynamically render the exercise content by "taking cue" from exercise.description // FIXME: each link in each card should include another property: url prefix (workshop or jekyll or no link) exercise =>
  • { exercise.url != "" ? <> : } { exercise.url.includes("/") ? <> {exercise.title} : <> {exercise.title} }
  • ) }
); }