From ff47a781d107e451d8f676f19dd0a6cbf7c885d1 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 2 Oct 2023 14:44:53 +0200 Subject: [PATCH] Use SelectNext for import and export user selectors --- package.json | 2 +- .../ShareDialog/ExportAnnotations.tsx | 65 ++++++++++++------- .../ShareDialog/ImportAnnotations.tsx | 61 ++++++++--------- .../ShareDialog/UserAnnotationsListItem.tsx | 21 ++++++ .../test/ExportAnnotations-test.js | 62 +++++++++++++----- .../test/ImportAnnotations-test.js | 55 ++++++++++------ yarn.lock | 10 +-- 7 files changed, 183 insertions(+), 93 deletions(-) create mode 100644 src/sidebar/components/ShareDialog/UserAnnotationsListItem.tsx diff --git a/package.json b/package.json index 6e7d9e3e668..dd8529aeb87 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "@babel/preset-react": "^7.0.0", "@babel/preset-typescript": "^7.16.7", "@hypothesis/frontend-build": "^2.0.0", - "@hypothesis/frontend-shared": "^6.5.0", + "@hypothesis/frontend-shared": "^6.8.1", "@hypothesis/frontend-testing": "^1.0.1", "@npmcli/arborist": "^7.0.0", "@octokit/rest": "^20.0.1", diff --git a/src/sidebar/components/ShareDialog/ExportAnnotations.tsx b/src/sidebar/components/ShareDialog/ExportAnnotations.tsx index 681d239b86c..ce1059b5694 100644 --- a/src/sidebar/components/ShareDialog/ExportAnnotations.tsx +++ b/src/sidebar/components/ShareDialog/ExportAnnotations.tsx @@ -3,13 +3,14 @@ import { CardActions, Link, Input, - Select, + SelectNext, } from '@hypothesis/frontend-shared'; -import { useCallback, useMemo, useState } from 'preact/hooks'; +import { useCallback, useId, useMemo, useState } from 'preact/hooks'; import { downloadJSONFile } from '../../../shared/download-json-file'; import type { APIAnnotationData } from '../../../types/api'; import { annotationDisplayName } from '../../helpers/annotation-user'; +import type { UserAnnotations } from '../../helpers/annotations-by-user'; import { annotationsByUser } from '../../helpers/annotations-by-user'; import { suggestedFilename } from '../../helpers/export-annotations'; import { withServices } from '../../service-context'; @@ -17,6 +18,7 @@ import type { AnnotationsExporter } from '../../services/annotations-exporter'; import type { ToastMessengerService } from '../../services/toast-messenger'; import { useSidebarStore } from '../../store'; import LoadingSpinner from './LoadingSpinner'; +import { UserAnnotationsListItem } from './UserAnnotationsListItem'; export type ExportAnnotationsProps = { // injected @@ -51,9 +53,23 @@ function ExportAnnotations({ [exportableAnnotations, getDisplayName], ); - // User whose annotations are going to be exported. Preselect current user + // User whose annotations are going to be exported. const currentUser = store.profile().userid; - const [selectedUser, setSelectedUser] = useState(currentUser); + const allAnnotationsOption: Omit = useMemo( + () => ({ + annotations: exportableAnnotations, + displayName: 'All annotations', + }), + [exportableAnnotations], + ); + const [selectedUser, setSelectedUser] = useState( + // Try to preselect current user + userList.find(userInfo => userInfo.userid === currentUser) ?? + allAnnotationsOption, + ); + + const fileInputId = useId(); + const userSelectId = useId(); const draftCount = store.countDrafts(); @@ -76,8 +92,7 @@ function ExportAnnotations({ try { const annotationsToExport = - userList.find(item => item.userid === selectedUser)?.annotations ?? - exportableAnnotations; + selectedUser?.annotations ?? exportableAnnotations; const filename = `${customFilename ?? defaultFilename}.json`; const exportData = annotationsExporter.buildExportContent(annotationsToExport); @@ -113,14 +128,14 @@ function ExportAnnotations({

@@ -129,28 +144,30 @@ function ExportAnnotations({ required maxLength={250} /> -