-
-
Notifications
You must be signed in to change notification settings - Fork 7
[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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9421522
Add createProject to FwLiteApi, fix empty-body fetch handling
imnasnainaec 4937c0f
Add Create new lexicon option to SelectLexicon WebView
imnasnainaec fc49101
Address review: fix CRDT type routing, error bodies, tag validation, …
imnasnainaec ebe0619
Address review: deriveCode parity with backend, resilient onCreated
imnasnainaec fa37e75
Fix getBrowseUrl to use correct path segment for CRDT projects
imnasnainaec ed5889a
Add en placeholder to analysis WS input
imnasnainaec 909316b
Surface lexicon creation errors in the form
imnasnainaec ad153f5
Surface lexicon-creation errors and fix vernacularLanguage WebView ty…
imnasnainaec 53b4124
Repopulate project-type cache on miss so restarts don't misroute lexi…
imnasnainaec 8edfde3
Fail lexicon selection loudly when the command reports no success
imnasnainaec cf65ddd
Harden lexicon-code validation and pre-check duplicate codes
imnasnainaec a77da24
Fix language filtering by reading wsId instead of array indices
imnasnainaec 1b571e7
Show validation message for invalid lexicon codes
imnasnainaec e337f6a
Align lexicon code validation with Lexbox's create-project rules
imnasnainaec 1973826
Add lang-tag validation and recover from a deleted lexicon
imnasnainaec 85633c5
Tweak comments
imnasnainaec 8db3304
Tighten lexicon create validation comments
imnasnainaec 99d4dfc
Don't treat a transient backend failure as a confirmed lexicon deletion
imnasnainaec f71d97d
Unwrap JSON error bodies and stop one bad project from killing langua…
imnasnainaec 3e81efa
Surface lexicon-selection save failures in the combo box
imnasnainaec 5afa856
Align create-lexicon labels with Paratext terminology
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.