Git Product home page Git Product logo

buddhanexus-frontend-next's Introduction

This is a Next.js project bootstrapped with create-next-app.

Getting Started

First, run the development server (make sure to use yarn and not npm*!):

*note: the project doesn't use yarn classic (it's been deprecated), so install yarn if needed: https://yarnpkg.com/getting-started/install

corepack enable # if needed
yarn install

Dev mode:

yarn dev

Open http://localhost:3000 with your browser to see the result.

You can start editing the page by modifying pages/index.tsx. The page auto-updates as you edit the file.

API routes can be accessed on http://localhost:3000/api/hello. This endpoint can be edited in pages/api/hello.ts.

The pages/api directory is mapped to /api/*. Files in this directory are treated as API routes instead of React pages.

Building locally:

The project generates many, many static pages which in most cases will preclude the possibility of running a normal build locally.

It is possible to use the compile mode locally:

yarn build:compile
yarn start

API BE<>FE intergration

https://buddhanexus2.kc-tbts.uni-hamburg.de/api/docs#/ (as an intrim measure the BN api is hosted at https://dharmamitra.org/api-db/docs#/ with the api model available at https://dharmamitra.org/api-db/openapi.json)

The utils/api/endpoints directory mirrors the BE api endpoint structure

API type code generation & API Client

The project uses openapi-typescript and openapi-fetch to interface between the backend.

Types are generated from the API project's OpenAPI docs page by running:

yarn openapi-ts

openapi-fetch's api client (instantiated in utils/api/client.ts) can then be used to fetch typed data (see docs).

API type files & conventions

note: at the time of writing, there is still some inconsistency in BE naming conventions. Some FE parsing needs may be eliminated with BE consistency. Review an update accordingly.

  • utils/api/types.ts:

    • this is the source of truth for BE<>FE typing
    • it contains codegen derivate types only to create shortened, standardized API type aliases
    • all types begin with API
    • all endpoints should have corresponding API<endpoint-name>RequestQuery (for GET requests), or API<endpoint-name>RequestBody (for POST requests), and API<endpoint-name>ResponseData types
  • utils/api/endpoints/<endpoint-name>.ts:

    • co-locates endpoint fetch function and, if required, its return type.
    • inferred return types are favoured
      • in most cases this should be possible, but if eg. doing some complicated mutation, or using the fetchNextPage prop from useInfiniteQuery it might be necessary to add a Promise<DataType> return type.
    • if needed, parsed return types should
      • be created from the parser's ReturnType if possible
      • follow the Parsed<endpoint-name>Data naming convention (parsed return consituent item types should begin with Parsed, eg. ParsedSearchResult)

I18n

  • Supported locales are defined in next-i18next.config.js.
  • Namespaces are defined in types/i18next.d.ts.

tsx pages

  • Namespace files (with localized content) are created in public/locales/
  • Namespaces are imported with getI18NextStaticProps at page level.

mdx pages

  • Pages for each supported locale are created in content/pages (or a dedicated content directory for nested routes eg. news). See: MDX page i18n for more.

Note: the project uses the term locale to refer to the user's language and the term language to refer to database languages (Pali, Sanskrit, Chinese and Tibetan).

MDX page i18n (Draft)

Static pages built from mdx files are held in /content/. Each page has a dedicated directory containing localized mdx files for supported locals:

├── content
    ├── news
    │   ├── author-and-translators-identification-inittiative
    │   │   ├── de.mdx
    │   │   └── en.mdx
    …
    └── pages
        ├── contact
        │   ├── de.mdx
        │   └── en.mdx
        ├── events
        │   ├── de.mdx
        │   └── en.mdx
        …

Content files must contain frontmatter following the schema defined in the MDXFrontmatter interface.

So that external link open in new tabs it is necessary to use regular html a elements with target="_blank" rel="noopener" attributes.

Original native mdx links have been converted with:

\[((.*?\n?){0,4})\]\((\s*?http(.*?\n?){0,4})\)
<a href="$3" target="_blank" rel="noopener">$1</a>

Localized URLs

It's possible, but…

…the initial implementation had more of a maintanence burden than ideal so was shelved at bbec5441a

Static page routes are defined in /routes-i18n.ts which handles URL localization.

When adding a new page / news post, entries for the page need to be added to both of the following objects:

  • rewrites (used to render pages with localized URLs), and
  • routes (used to creates localized hrefs for the Link component).

If a path contains characters not allowed in the RFC3986 spec (in short a character that isn't an ASCII alphanumeric, /, -, ., _, or ~) it must be percent-encoded which enables fully localized page paths:

`/${encodeURIComponent("ཨོཾ་མུ་ནེ་མུ་ནེ་མ་ཧཱ་མུ་ན་ཡེ་སྭཱ་ཧཱ།")}`;

Within mdx files, external links can used the regular markdown link syntax. However internal links should use the Link component with the page directory name as a route prop value to enable i18n route handling. Eg:

<Link route="/news">Nachrichten</Link>

For bonus points see:


JSX components & imports

These can be used in mdx files, by declaring them through the optional components, imports & props frontmatter properties and importing them in utils/mdxPageImports.ts. These will then be passed to MDXRemote in the page template file, which renders compiled source from next-mdx-remote's serializer (see pages/[slug].tsx).

It might be worth sanity checking this implementation. Heed the wisdom of the readme!

Learn More about Next.js

To learn more about Next.js, take a look at the following resources:

You can check out the Next.js GitHub repository - your feedback and contributions are welcome!

Deploy on Vercel

The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.

Check out our Next.js deployment documentation for more details.

buddhanexus-frontend-next's People

Contributors

aminahbl avatar trebuhd avatar angirov avatar ayya-vimala avatar

Stargazers

Sebastian Nehrdich avatar  avatar

Watchers

 avatar Sebastian Nehrdich avatar

Forkers

dharmamitra

buddhanexus-frontend-next's Issues

Padding on match numberr

Number of matches at the top of the right viewport in text-view mode needs some padding on the left.

Image

Add pagination/search endpoint for menu

The https://buddhanexus2.kc-tbts.uni-hamburg.de/api/menus/${language} endpoint currently responds with all data at once. We should add pagination there and support it in the frontend components, or ideally - use something like Algolia or ElasticSearch.

Space between segment texts

Not sure if this is backend or frontend.
Segment texts need a space between them. F.i. two segments are ... ārāme.Tatra ... => ārāme. Tatra
Or: āmantesi:“bhikkhavo”ti.“Bhadante”ti => āmantesi: “bhikkhavo”ti. “Bhadante”ti
(I think we discussed this needs to be done on the backend ...)

Image

Not sure how this is in Tibetan / Chinese / Sanskrit. Best check what is going on here.

/db/[language] page is too large

Warning: data for page "/db/[language]" (path "/db/chn") is 444 kB which exceeds the threshold of 128 kB, this amount of data can reduce performance.

This is because of this code:

  const queryClient = await prefetchSourceTextBrowserData(
    params?.language as SourceLanguage
  );

Fill ATII page with content

The ATII page needs main page content, a page "about" and a browser for the actual data that follows the table-view layout

Add TS code generator

Hey @sebastian-nehrdich & @TrebuhD I'm really stoked to get to see the benefit of all of Sebastian's great work on the BE on the FE.

Which code generator?

Today I created test branches for two TypeScript code generation tools and both work well and I think will give the FE a nice improvement.

The first package I tested is the one Hubert suggested that kicked the whole process off: openapi-typescript-codegen (fastapi docs). The implementation test can be seen on commit 075f082. Everything under codegen/* is auto generated and the main file to pay attention to is utils/api/table.ts.

The second package I tested was openapi-fetch (with openapi-typescript as a dev dependency) under commit fa585fcf. Again, everything under codegen/* is auto generated and the main file to pay attention to is utils/api/table.ts.

While openapi-typescript-codegen is more established, has a much higher usage rate and is even referenced in the fastapi docs, I have a bit of a preference towards openapi-fetch because I like the IntelliSense, I find it a more readable, and it seems to be more precise with typing, it also claims to be much smaller and faster, but I'm not yet nerdy enough to know how to verify these comparisons. I'm wondering if you have any preference over which package to implement?

openapi-typescript-codegen
image

openapi-fetch
image

A few notes:

  1. the available endpoints/functions and body params are being given as prompts with both generators;
  2. because openapi-fetch identifies that the type is unknown and causes an error for the parser just to quickly test that everything else works I've casted data (this will be resolved by added a response model);
  3. the limits endpoint has only just come online and hasn't been developed yet hence the placeholder.

Adding a response_model prop to the BE

The unknown type mentioned above, highlights a further issue. Fabulous work has been done to improve the BE, and it would be great to get as much benefit as we can from it on the FE. At the moment, response models are not being returned by the API:

image

However, In my first play around with the BE I found they're really easy to implement by just defining the model and adding a response_model prop to the endpoints

class TableViewResponse(BaseModel):
    par_segnr: List[str]
    par_full_names: FullName
    root_full_names: FullName
    file_name: str
    root_segnr: List[str]
    par_length: int
    root_length: int
    score: int
    src_lang: str
    tgt_lang: str
    root_fulltext: List[FullTextItem]
    par_fulltext: List[FullTextItem]

…

@router.get("/table", response_model=TableViewResponse)

This gives the whole response model rather than string:

image

With this both code generators should be able to convert the models into proper types on the FE (rather than giving unknown or any). @sebastian-nehrdich would you like me to create a branch on the buddhanexus repo to add these types?

Precalculate colormap for the text-view on the backend

The text-view uses a colormap for each segment to determine whether a character needs to be highlighted or not. The value in the colormap determines the color, for example:
B 0
U 1
D 2
D 2
H 1
A 0
the colormap is determined by the number of textual matches that occur at a specific position.
From an efficiency point of view, it would be best to precalculate the colormaps on the backend. I will look into whether this is feasible or not.

Add db page descriptions for other languages

if you visit the Pali db page, you’ll see a description (copied over from the current website). We need to add those descriptions for other languages as well, would anyone here be up for that? <@U0471H1GT4H> <@U03NV402737>

Slack Message

Text link color in static pages

Text link colors in static pages have the same color as the text with an underline. It would be better the same colors as the database pages:

Image

Image

Image

fastAPI versioning

Since we are likely to make compatibility-breaking changes to the API, versioning is needed.

Create text-view

The text-view needs to provide the following functionality:

  1. 3-column layout: Main text to the left , when a match is clicked a column (we call it 'middle view' in the old codebase) opens to the right of the main text that shows the match(es); when a match in this middle column is clicked, another column to the right (called right view in the old codebase) opens that shows the text where the match is leading to
  2. main text in the left column and the other text in the right column use the same API request to fetch data from the backend
  3. The coloring of the text left and right side text is done via colormaps; when a user clicks on a match in the main text/right side text, we need to transmit the segment number of the clicked segment as well as the precise position of the character that has been clicked to the API request of the middle column
  4. When hovering over a match in the middle column, the corresponding portion of text on the left hand side needs to be highlighted in some form
  5. Routing: The old text-view URL accepts an additional parameter to specify the segment-number and will automatically scroll there when it is provided
  6. because of (5), we need endless scrolling in both directions (up and down) since we can only load a maximum number of 1000 segments at once in a reasonable time

Card header segments

API needs to return card header segments as a string instead of a list.
Card header: in case there are two segments on the card it says something like: dn1:1.1.1_0dn1:1.1.2_0 instead of dn1:1.1.1_0–1.1.2_0 (en-dash in between.)

Right now if there are more than two segments it lists them all:
mnd1:2.1_0mnd1:2.2_0mnd1:2.3_0mnd1:2.4_0mnd1:3.1_0
This can be reduced to:
mnd1:2.1_0–3.1_0

This might occur in a number of places, like f.i. also numbers-view. Basically the backend needs to return strings just as they have to be put into the frontend.

Image

Pali data reload in backend

Pali commentarial texts still have the old structure, probably due to it not having been loaded into the backend yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.