Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

Fix tag selection & deleting new tags

Isaac64d20df6f1ed
diff --git a/components/InputSelect/TagSelection.tsx b/components/InputSelect/TagSelection.tsx
index 67623a49..6917abae 100644
--- a/components/InputSelect/TagSelection.tsx
+++ b/components/InputSelect/TagSelection.tsx
@@ -8,7 +8,7 @@ import { useTranslation } from "next-i18next";
type Props = {
onChange: (e: any) => void;
options?: Option[] | ArchivalTagOption[];
- isArchivalTagSelection?: boolean;
+ isArchivalSelection?: boolean;
defaultValue?: {
value?: number;
label: string;
@@ -20,41 +20,55 @@ type Props = {
export default function TagSelection({
onChange,
options,
- isArchivalTagSelection,
+ isArchivalSelection,
defaultValue,
autoFocus,
onBlur,
}: Props) {
- const { t } = useTranslation();
const { data: tags = [] } = useTags();
+ const { t } = useTranslation();
+
const [tagOptions, setTagOptions] = useState<Option[]>([]);
useEffect(() => {
- setTagOptions(
- tags.map((e: any) => ({
- value: e.id,
- label: e.name,
- }))
- );
- }, [tags]);
+ const formatedCollections = tags.map((e: any) => {
+ return { value: e.id, label: e.name };
+ });
- const selectOptions =
- isArchivalTagSelection && options ? options : tagOptions;
- const selectValue = isArchivalTagSelection ? [] : defaultValue;
+ setTagOptions(formatedCollections);
+ }, [tags]);
return (
- <CreatableSelect
- isClearable={false}
- className="react-select-container"
- classNamePrefix="react-select"
- onChange={onChange}
- options={selectOptions}
- styles={styles}
- value={selectValue}
- placeholder={t("tag_selection_placeholder")}
- isMulti
- autoFocus={autoFocus}
- onBlur={onBlur}
- />
+ <>
+ {isArchivalSelection ? (
+ <CreatableSelect
+ isClearable={false}
+ className="react-select-container"
+ classNamePrefix="react-select"
+ onChange={onChange}
+ options={options}
+ styles={styles}
+ value={[]}
+ placeholder={t("tag_selection_placeholder")}
+ isMulti
+ autoFocus={autoFocus}
+ onBlur={onBlur}
+ />
+ ) : (
+ <CreatableSelect
+ isClearable={false}
+ className="react-select-container"
+ classNamePrefix="react-select"
+ onChange={onChange}
+ options={tagOptions}
+ styles={styles}
+ defaultValue={defaultValue}
+ placeholder={t("tag_selection_placeholder")}
+ isMulti
+ autoFocus={autoFocus}
+ onBlur={onBlur}
+ />
+ )}
+ </>
);
}
diff --git a/hooks/useArchivalTags.ts b/hooks/useArchivalTags.ts
index 9f3850b5..74d467c0 100644
--- a/hooks/useArchivalTags.ts
+++ b/hooks/useArchivalTags.ts
@@ -47,16 +47,16 @@ const useArchivalTags = (initialTags: Tag[]) => {
(newTag) =>
!archivalTags.some((existing) => existing.label === newTag.label)
)
- .map(({ __isNew__, value, ...tag }) => ({
+ .map(({ value, ...tag }) => ({
...tag,
- archiveAsScreenshot: __isNew__ ? false : tag.archiveAsScreenshot,
- archiveAsMonolith: __isNew__ ? false : tag.archiveAsMonolith,
- archiveAsPDF: __isNew__ ? false : tag.archiveAsPDF,
- archiveAsReadable: __isNew__ ? false : tag.archiveAsReadable,
- archiveAsWaybackMachine: __isNew__
+ archiveAsScreenshot: tag.__isNew__ ? false : tag.archiveAsScreenshot,
+ archiveAsMonolith: tag.__isNew__ ? false : tag.archiveAsMonolith,
+ archiveAsPDF: tag.__isNew__ ? false : tag.archiveAsPDF,
+ archiveAsReadable: tag.__isNew__ ? false : tag.archiveAsReadable,
+ archiveAsWaybackMachine: tag.__isNew__
? false
: tag.archiveAsWaybackMachine,
- aiTag: __isNew__ ? false : tag.aiTag,
+ aiTag: tag.__isNew__ ? false : tag.aiTag,
}));
setArchivalTags((prev) => [...prev, ...uniqueNewTags]);
@@ -80,19 +80,7 @@ const useArchivalTags = (initialTags: Tag[]) => {
const removeTag = (tagToDelete: ArchivalTagOption) => {
setArchivalTags((prev) =>
- prev.map((t) =>
- t.label === tagToDelete.label
- ? {
- ...t,
- archiveAsScreenshot: null,
- archiveAsMonolith: null,
- archiveAsPDF: null,
- archiveAsReadable: null,
- archiveAsWaybackMachine: null,
- aiTag: null,
- }
- : t
- )
+ prev.filter((t) => t.label !== tagToDelete.label)
);
if (!tagToDelete.__isNew__) {
@@ -106,7 +94,12 @@ const useArchivalTags = (initialTags: Tag[]) => {
aiTag: false,
};
- setOptions((prev) => [...prev, resetTag]);
+ setOptions((prev) => {
+ if (!prev.some((t) => t.label === resetTag.label)) {
+ return [...prev, resetTag];
+ }
+ return prev;
+ });
}
};
diff --git a/pages/settings/preference.tsx b/pages/settings/preference.tsx
index a186d0b9..2a23919f 100644
--- a/pages/settings/preference.tsx
+++ b/pages/settings/preference.tsx
@@ -444,7 +444,7 @@ export default function Preference() {
</div>
<div className="p-3">
<TagSelection
- isArchivalTagSelection
+ isArchivalSelection
onChange={addTags}
options={options}
/>