-
-
Notifications
You must be signed in to change notification settings - Fork 6
[PB Extension] Create FW project #2394
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
Draft
imnasnainaec
wants to merge
20
commits into
develop
Choose a base branch
from
pb-extension/create-project-api
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4d6335f
Add createProject to FwLiteApi, fix empty-body fetch handling
imnasnainaec 2b91c54
Add Create new lexicon option to SelectLexicon WebView
imnasnainaec 54748c2
Address review: fix CRDT type routing, error bodies, tag validation, …
imnasnainaec bbaacfd
Address review: deriveCode parity with backend, resilient onCreated
imnasnainaec 180824a
Fix getBrowseUrl to use correct path segment for CRDT projects
imnasnainaec ad032f1
Add en placeholder to analysis WS input
imnasnainaec d9996e8
Surface lexicon creation errors in the form
imnasnainaec 570b4c1
Surface lexicon-creation errors and fix vernacularLanguage WebView ty…
imnasnainaec b377039
Repopulate project-type cache on miss so restarts don't misroute lexi…
imnasnainaec 5971d28
Fail lexicon selection loudly when the command reports no success
imnasnainaec 085201b
Harden lexicon-code validation and pre-check duplicate codes
imnasnainaec 057b7fb
Fix language filtering by reading wsId instead of array indices
imnasnainaec 4436f18
Show validation message for invalid lexicon codes
imnasnainaec 5f9f128
Align lexicon code validation with Lexbox's create-project rules
imnasnainaec 75121ff
Add lang-tag validation and recover from a deleted lexicon
imnasnainaec 09df8f0
Tweak comments
imnasnainaec f212c51
Tighten lexicon create validation comments
imnasnainaec 6a407ba
Don't treat a transient backend failure as a confirmed lexicon deletion
imnasnainaec 258add7
Unwrap JSON error bodies and stop one bad project from killing langua…
imnasnainaec 91c442b
Surface lexicon-selection save failures in the combo box
imnasnainaec 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 hidden or 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 hidden or 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
195 changes: 195 additions & 0 deletions
195
platform.bible-extension/src/components/create-lexicon.tsx
This file contains hidden or 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,195 @@ | ||
| import { logger } from '@papi/frontend'; | ||
| import { useLocalizedStrings } from '@papi/frontend/react'; | ||
| import { Button, Input, Label } from 'platform-bible-react'; | ||
| import { type ReactElement, useEffect, useState } from 'react'; | ||
| import { LOCALIZED_STRING_KEYS } from '../types/localized-string-keys'; | ||
|
|
||
| const CODE_PATTERN = /^[a-z\d][a-z\d-]*$/; | ||
| // Matches Lexbox UI validation: frontend/src/routes/(authenticated)/project/create/+page.svelte | ||
| const MIN_CODE_LENGTH = 4; | ||
| // Loosely approximates FW Lite backend validation via SIL.WritingSystems.IetfLanguageTag.IsValid | ||
| const BASIC_IETF_LANGUAGE_TAG_PATTERN = /^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$/; | ||
|
|
||
| function normalizeLangTag(tag: string): string { | ||
| return tag.trim().replace(/_/g, '-'); | ||
| } | ||
|
|
||
| function isValidLangTag(tag: string): boolean { | ||
| const normalizedTag = normalizeLangTag(tag); | ||
| if (!normalizedTag) return false; | ||
|
|
||
| try { | ||
| return Intl.getCanonicalLocales(normalizedTag).length === 1; | ||
| } catch { | ||
| // Fallback to syntax validation so we don't block backend-accepted tags that Intl rejects. | ||
| return BASIC_IETF_LANGUAGE_TAG_PATTERN.test(normalizedTag); | ||
| } | ||
| } | ||
|
|
||
| function deriveCode(name: string): string { | ||
| return name | ||
| .toLowerCase() | ||
| .replace(/\s+/g, '-') | ||
| .replace(/[^a-z0-9-]/g, '-') | ||
| .replace(/-+/g, '-') | ||
| .replace(/^[^a-z0-9]+/, '') | ||
| .replace(/[^a-z0-9]+$/, ''); | ||
| } | ||
|
|
||
| interface CreateLexiconProps { | ||
| createLexicon: ( | ||
| name: string, | ||
| code: string, | ||
| vernacularWs: string, | ||
| analysisWs?: string, | ||
| ) => Promise<void>; | ||
| defaultVernacularWs?: string; | ||
| existingCodes?: string[]; | ||
| onCancel: () => void; | ||
| onCreated: (code: string) => Promise<void>; | ||
| } | ||
|
|
||
| /** A form for creating a new FW Lite CRDT project from the blank template. */ | ||
| export default function CreateLexicon({ | ||
| createLexicon, | ||
| defaultVernacularWs, | ||
| existingCodes, | ||
| onCancel, | ||
| onCreated, | ||
| }: CreateLexiconProps): ReactElement { | ||
| const [localizedStrings] = useLocalizedStrings(LOCALIZED_STRING_KEYS); | ||
|
|
||
| const [name, setName] = useState(''); | ||
| const [code, setCode] = useState(''); | ||
| const [codeEdited, setCodeEdited] = useState(false); | ||
| const [vernacularWs, setVernacularWs] = useState(defaultVernacularWs ?? ''); | ||
| const [analysisWs, setAnalysisWs] = useState(''); | ||
| const [creating, setCreating] = useState(false); | ||
| const [error, setError] = useState(''); | ||
|
|
||
| useEffect(() => { | ||
| if (!codeEdited) setCode(deriveCode(name)); | ||
| }, [codeEdited, name]); | ||
|
|
||
| // The code input lowercases as typed, but existingCodes may come from elsewhere with different | ||
| // casing, so compare case-insensitively to catch a well-formed collision before it 400s. | ||
| const codeExists = | ||
| !!code && (existingCodes ?? []).some((c) => c.toLowerCase() === code.toLowerCase()); | ||
|
|
||
| const isValid = !!( | ||
| name.trim() && | ||
| code.length >= MIN_CODE_LENGTH && | ||
| CODE_PATTERN.test(code) && | ||
| !codeExists && | ||
| isValidLangTag(vernacularWs) && | ||
| (!analysisWs.trim() || isValidLangTag(analysisWs)) | ||
| ); | ||
|
|
||
| const handleSubmit = async () => { | ||
| if (!isValid) return; | ||
| setCreating(true); | ||
| setError(''); | ||
| try { | ||
| await createLexicon( | ||
| name.trim(), | ||
| code, | ||
| normalizeLangTag(vernacularWs), | ||
| analysisWs.trim() ? normalizeLangTag(analysisWs) : undefined, | ||
| ); | ||
| await onCreated(code); | ||
| } catch (e) { | ||
| logger.error(localizedStrings['%lexicon_createLexicon_error%'], JSON.stringify(e)); | ||
| setError(e instanceof Error ? e.message : String(e)); | ||
| } finally { | ||
| setCreating(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="tw:flex tw:flex-col tw:gap-1 tw:p-4"> | ||
| <h3 className="tw:font-semibold tw:mb-2"> | ||
| {localizedStrings['%lexicon_createLexicon_title%']} | ||
| </h3> | ||
|
|
||
| <div> | ||
| <Label htmlFor="createLexiconName"> | ||
| {localizedStrings['%lexicon_createLexicon_name%']} | ||
| </Label> | ||
| <Input id="createLexiconName" onChange={(e) => setName(e.target.value)} value={name} /> | ||
| </div> | ||
|
|
||
| <div> | ||
| <Label htmlFor="createLexiconCode"> | ||
| {localizedStrings['%lexicon_createLexicon_code%']} | ||
| </Label> | ||
| <Input | ||
| id="createLexiconCode" | ||
| onChange={(e) => { | ||
| setCode(e.target.value.toLowerCase()); | ||
| setCodeEdited(true); | ||
| }} | ||
| value={code} | ||
| /> | ||
| {codeExists && ( | ||
| <p className="tw:text-sm tw:text-destructive tw:mt-1"> | ||
| {localizedStrings['%lexicon_createLexicon_codeExists%']} | ||
| </p> | ||
| )} | ||
| {!codeExists && !!code && code.length < MIN_CODE_LENGTH && ( | ||
| <p className="tw:text-sm tw:text-destructive tw:mt-1"> | ||
| {localizedStrings['%lexicon_createLexicon_codeTooShort%']} | ||
| </p> | ||
| )} | ||
| {!codeExists && code.length >= MIN_CODE_LENGTH && !CODE_PATTERN.test(code) && ( | ||
| <p className="tw:text-sm tw:text-destructive tw:mt-1"> | ||
| {localizedStrings['%lexicon_createLexicon_codeInvalid%']} | ||
| </p> | ||
| )} | ||
| </div> | ||
|
|
||
| <div> | ||
| <Label htmlFor="createLexiconVernWs"> | ||
| {localizedStrings['%lexicon_createLexicon_vernacularWs%']} | ||
| </Label> | ||
| <Input | ||
| id="createLexiconVernWs" | ||
| onChange={(e) => setVernacularWs(e.target.value)} | ||
| value={vernacularWs} | ||
| /> | ||
|
imnasnainaec marked this conversation as resolved.
|
||
| {!!vernacularWs.trim() && !isValidLangTag(vernacularWs) && ( | ||
| <p className="tw:text-sm tw:text-destructive tw:mt-1"> | ||
| {localizedStrings['%lexicon_createLexicon_langTagInvalid%']} | ||
| </p> | ||
| )} | ||
| </div> | ||
|
|
||
| <div> | ||
| <Label htmlFor="createLexiconAnalysisWs"> | ||
| {localizedStrings['%lexicon_createLexicon_analysisWs%']} | ||
| </Label> | ||
| <Input | ||
|
imnasnainaec marked this conversation as resolved.
|
||
| id="createLexiconAnalysisWs" | ||
| onChange={(e) => setAnalysisWs(e.target.value)} | ||
| placeholder="en" | ||
| value={analysisWs} | ||
| /> | ||
| {!!analysisWs.trim() && !isValidLangTag(analysisWs) && ( | ||
| <p className="tw:text-sm tw:text-destructive tw:mt-1"> | ||
| {localizedStrings['%lexicon_createLexicon_langTagInvalid%']} | ||
| </p> | ||
| )} | ||
| </div> | ||
|
|
||
| {error && <p className="tw:text-sm tw:text-destructive tw:mt-1">{error}</p>} | ||
|
|
||
| <div className="tw:flex tw:gap-1 tw:mt-2"> | ||
| <Button disabled={!isValid || creating} onClick={() => handleSubmit()}> | ||
| {localizedStrings['%lexicon_createLexicon_submit%']} | ||
| </Button> | ||
| <Button onClick={onCancel} variant="secondary"> | ||
| {localizedStrings['%lexicon_button_cancel%']} | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.