lbry-desktop/ui/component/publishName/name-help-text.jsx
infinite-persistence 3f805a6189 doCheckPublishNameAvailability: case-insensitive version
## Issue
??

## Behavioral Changes
- Use `claim_search` instead of `claim_list` to retrieve all all own claims with the same name (case-insensitive).
  - Caveat: annonymous posts will be excluded.
- When a clash occurs, there is a possibility that we have multiple existing entries (e.g. "xxX", "xXx"). Since we don't know which one is best to fall back, I removed the "edit" button for this and replaced with a simpler text

## Code Note
- If not mistaken, the rest of the code still needs `selectMyClaimForUri` to be case-sensitive, so augment the selector to support both through an optional parameter.
2022-04-18 00:44:11 -04:00

84 lines
2.6 KiB
JavaScript

// @flow
import * as React from 'react';
import Button from 'component/button';
import { buildURI } from 'util/lbryURI';
import I18nMessage from 'component/i18nMessage';
function isUriPendingUpload(uri: ?string, currentUploadNames: Array<string>) {
const protocol = 'lbry://';
const uriName = uri && uri.startsWith(protocol) ? uri.substring(protocol.length) : uri;
return currentUploadNames.includes(uriName);
}
type Props = {
uri: ?string,
myClaimForUri: ?StreamClaim,
myClaimForUriCaseInsensitive: ?StreamClaim,
currentUploads: { [key: string]: FileUploadItem },
isStillEditing: boolean,
onEditMyClaim: (any, string) => void,
};
function NameHelpText(props: Props) {
const { uri, myClaimForUri, myClaimForUriCaseInsensitive, currentUploads, onEditMyClaim, isStillEditing } = props;
const currentUploadNames: Array<string> = React.useMemo(() => {
// $FlowFixMe - unable to resolve mixed
return Object.values(currentUploads).map((x) => (x.params ? x.params.name : ''));
}, [currentUploads]);
let nameHelpText;
if (isStillEditing) {
nameHelpText = __('You are currently editing this claim.');
} else if (isUriPendingUpload(uri, currentUploadNames)) {
nameHelpText = (
<div className="error__text">
{/* prettier-ignore */}
<I18nMessage tokens={{ existing_uri: (<u><em>{uri}</em></u>) }}>
You already have a pending upload at %existing_uri%.
</I18nMessage>
</div>
);
} else if (uri && myClaimForUri) {
const editUri = buildURI({
streamName: myClaimForUri.name,
streamClaimId: myClaimForUri.claim_id,
});
nameHelpText = (
<React.Fragment>
<div className="error__text">
<I18nMessage
tokens={{
existing_uri: (
<u>
<em>{uri}</em>
</u>
),
}}
>
You already have a claim at %existing_uri%. Publishing will update (overwrite) your existing claim.
</I18nMessage>
</div>
<Button
button="link"
label={__('Edit existing claim instead')}
onClick={() => onEditMyClaim(myClaimForUri, editUri)}
/>
</React.Fragment>
);
} else if (uri && myClaimForUriCaseInsensitive) {
nameHelpText = <div className="error__text">{__('You already have an upload with that name.')}</div>;
}
return (
<React.Fragment>
{nameHelpText || (
<span>{__('Create a URL for this content. Simpler names are easier to find and remember.')}</span>
)}
</React.Fragment>
);
}
export default NameHelpText;