Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/components/admin/LicenceInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ function offerOf(rights: string, defaultLicence: string) {
return found ? { id: found, name: nameOf(found) } : { id: defaultLicence, name: licenceShortName(defaultLicence) ?? "the default" };
}

/**
* The licence a record's rights name, when its own says otherwise: rights edited to "All rights reserved" after it started
* with CC BY 4.0. A licence of the record's own goes against rights that name one of Zenodo's, or that keep every right
* while it is open; a closed one of its own under "All rights reserved" agrees with them.
*/
function contradicted(licence: RecordLicence | null, rights: string) {
const found = licenceFromRights(rights);
if (!licence || !found) return null;
if (licence.kind === "zenodo") return found === licence.id ? null : { found, own: nameOf(licence.id) };
return licenceComplete(licence) && (found !== "other-closed" || licence.open) ? { found, own: licence.title } : null;
}

/**
* Where the licence came from, said under the field: its rights, the community's default, or a person. "Its rights"
* never says "above": the import review has no Rights field to point at.
Expand All @@ -52,6 +64,12 @@ function originNote(licence: RecordLicence | null, rights: string, defaultLicenc
return "Chosen for this record.";
}

/** The note under the field: the licence its rights name when it has another, or else where its licence came from. */
function noteOf(licence: RecordLicence | null, rights: string, defaultLicence: string) {
const other = contradicted(licence, rights);
return other ? `Its rights name ${nameOf(other.found)}, not ${other.own}.` : originNote(licence, rights, defaultLicence);
}

/**
* A record's licence: one of Zenodo's, or another by its name and link. It is what Zenodo and Crossref get for the
* record's DOI, so a record with none can't get one. Shaped like the Study type field: the search, then where the
Expand Down Expand Up @@ -130,10 +148,10 @@ export function LicenceInput({
</div>
<div className="flex items-start gap-3 px-3.5 pb-3.5 pt-1" aria-live="polite">
<p className={`min-w-0 flex-1 text-xs leading-relaxed ${missing ? "text-danger" : "text-black/50 dark:text-white/50"}`}>
{originNote(licence, rights, defaultLicence)}
{noteOf(licence, rights, defaultLicence)}
{missing ? " A DOI needs one." : null}
</p>
{!licenceComplete(licence) && !disabled ? (
{(!licenceComplete(licence) || contradicted(licence, rights)) && !disabled ? (
// A licence without a short name, such as CC BY-NC-ND 3.0 IGO, goes by its full title, which the note above names
// whole: the button keeps to half the row and cuts it.
<Button size="xs" variant="muted" className="min-w-0 max-w-[50%]" onPress={() => onChange({ kind: "zenodo", id: offer.id })}>
Expand Down
20 changes: 16 additions & 4 deletions tests/unit/licence-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ afterEach(() => {
container.remove();
});

async function pressUse(rights: string) {
/** The licence the field's "Use" button gives, or undefined when it has none. */
async function pressUse(rights: string, value: RecordLicence | null = null) {
const onChange = vi.fn<(licence: RecordLicence) => void>();
await act(async () => root.render(<NextUIProvider disableAnimation><LicenceInput value={null} rights={rights} defaultLicence="cc-by-4.0" needed={false} onChange={onChange} /></NextUIProvider>));
const use = [...container.querySelectorAll("button")].find((button) => button.textContent?.startsWith("Use "))!;
await act(async () => use.click());
await act(async () => root.render(<NextUIProvider disableAnimation><LicenceInput value={value} rights={rights} defaultLicence="cc-by-4.0" needed={false} onChange={onChange} /></NextUIProvider>));
const use = [...container.querySelectorAll("button")].find((button) => button.textContent?.startsWith("Use "));
await act(async () => use?.click());
return onChange.mock.calls[0]?.[0];
}

Expand All @@ -36,3 +37,14 @@ it("offers the licence a record's rights name before the community's default", a
expect(await pressUse("© 2024 Ministry of Education. All rights reserved.")).toEqual({ kind: "zenodo", id: "other-closed" });
expect(await pressUse("© 2024 Ministry of Education.")).toEqual({ kind: "zenodo", id: "cc-by-4.0" });
});

// A hand-made record kept the default CC BY 4.0 after its rights were changed to "All rights reserved", with nothing to say so.
it("offers the licence a record's rights name when it has another", async () => {
const ccBy = { kind: "zenodo", id: "cc-by-4.0" } as const;
expect(await pressUse("© 2024 Ministry of Education. All rights reserved.", ccBy)).toEqual({ kind: "zenodo", id: "other-closed" });
expect(await pressUse("This work is licensed under CC BY 4.0.", ccBy)).toBeUndefined();
expect(await pressUse("© 2024 Ministry of Education.", ccBy)).toBeUndefined();
const own = { kind: "custom", title: "Ministry licence", url: "https://example.org/terms", open: true } as const;
expect(await pressUse("© 2024 Ministry of Education. All rights reserved.", own)).toEqual({ kind: "zenodo", id: "other-closed" });
expect(await pressUse("© 2024 Ministry of Education. All rights reserved.", { ...own, open: false })).toBeUndefined();
});
Loading