-
-
Notifications
You must be signed in to change notification settings - Fork 470
core&ui: assignment auto complete #1061
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 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0d5b903
core&ui: assignment auto complete
TaiRuiQu 3a5ac28
core: add permission check to group listing API
TaiRuiQu 8d2ebd6
fix: invalid group
TaiRuiQu a3bfc19
fix: group render
TaiRuiQu 08b00e8
fix: clean duplicate html code structures
TaiRuiQu 1b121b9
Merge branch 'master' into master
TaiRuiQu 78a92d6
core&ui: fix permission check, add error handling
TaiRuiQu 2fa3e05
Merge remote-tracking branch 'refs/remotes/origin/master'
TaiRuiQu 57def90
ui: add error handling
TaiRuiQu b4461b6
ui: add error handling
TaiRuiQu b070864
ui: move error handling to AutoComplete
TaiRuiQu a0e634b
fix: make linter happy
TaiRuiQu 1798fc9
Merge branch 'master' into master
TaiRuiQu 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
24 changes: 24 additions & 0 deletions
24
packages/ui-default/components/autocomplete/AssignSelectAutoComplete.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,24 @@ | ||
| import AutoComplete, { AutoCompleteOptions } from '.'; | ||
| import AssignSelectAutoCompleteFC from './components/AssignSelectAutoComplete'; | ||
|
|
||
| export default class AssignSelectAutoComplete<Multi extends boolean> extends AutoComplete { | ||
| static DOMAttachKey = 'ucwAssignSelectAutoCompleteInstance'; | ||
|
|
||
| constructor($dom, options: AutoCompleteOptions<Multi> = {}) { | ||
| super($dom, { | ||
| classes: 'assign-select', | ||
| component: AssignSelectAutoCompleteFC, | ||
| props: { | ||
| multi: true, | ||
| height: 'auto', | ||
| }, | ||
| ...options, | ||
| }); | ||
| } | ||
|
|
||
| value(): string { | ||
| return this.ref?.getSelectedItemKeys().join(',') ?? this.$dom.val(); | ||
| } | ||
| } | ||
|
|
||
| window.Hydro.components.AssignSelectAutoComplete = AssignSelectAutoComplete; |
118 changes: 118 additions & 0 deletions
118
packages/ui-default/components/autocomplete/components/AssignSelectAutoComplete.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,118 @@ | ||
| import { AutoComplete, AutoCompleteHandle, AutoCompleteProps } from '@hydrooj/components'; | ||
| import type { GDoc, Udoc } from 'hydrooj/src/interface'; | ||
| import PropTypes from 'prop-types'; | ||
| import React, { forwardRef } from 'react'; | ||
| import { api } from 'vj/utils'; | ||
|
|
||
| interface AssignItem { | ||
| type: 'user' | 'group'; | ||
| key: string; | ||
| name: string; | ||
| displayName?: string; | ||
| avatarUrl?: string; | ||
| uids?: number[]; | ||
| invalid?: boolean; | ||
| } | ||
|
|
||
| const toUserItem = (user: Udoc): AssignItem => ({ | ||
| type: 'user', | ||
| key: user._id.toString(), | ||
| name: user.uname, | ||
| displayName: user.displayName, | ||
| avatarUrl: user.avatarUrl, | ||
| }); | ||
|
|
||
| const toGroupItem = (group: GDoc): AssignItem => ({ | ||
| type: 'group', | ||
| key: group.name, | ||
| name: group.name, | ||
| uids: group.uids, | ||
| }); | ||
|
|
||
| const AssignSelectAutoComplete = forwardRef<AutoCompleteHandle<AssignItem>, AutoCompleteProps<AssignItem>>((props, ref) => ( | ||
| <AutoComplete<AssignItem> | ||
| ref={ref as any} | ||
| cacheKey="assign" | ||
| queryItems={async (query) => { | ||
| const [users, groups] = await Promise.all([ | ||
| api('users', { search: query }, ['_id', 'uname', 'displayName', 'avatarUrl']), | ||
| api('groups', { search: query }, ['name', 'uids']), | ||
| ]); | ||
| const userItems: AssignItem[] = users.map((user: Udoc) => toUserItem(user)); | ||
| const groupItems: AssignItem[] = groups.map((group: GDoc) => toGroupItem(group)); | ||
| return [...groupItems, ...userItems]; | ||
| }} | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| fetchItems={async (keys) => { | ||
| const isUserId = (k: string) => /^-?[0-9]+$/.test(k); | ||
| const userIds: string[] = keys.filter((k) => isUserId(k)); | ||
| const groupNames: string[] = keys.filter((k) => !isUserId(k)); | ||
|
|
||
| const [users, groups]: [Udoc[], GDoc[]] = await Promise.all([ | ||
| userIds.length > 0 ? api('users', { auto: userIds }, ['_id', 'uname', 'displayName']) : [], | ||
| groupNames.length > 0 ? api('groups', { names: groupNames }, ['name', 'uids']) : [], | ||
|
TaiRuiQu marked this conversation as resolved.
|
||
| ]); | ||
|
|
||
| const userItems: AssignItem[] = users.map((user: Udoc) => toUserItem(user)); | ||
| const groupItems: AssignItem[] = keys | ||
| .filter((key) => !isUserId(key)) | ||
| .map((key) => { | ||
| const group = groups.find((g) => g.name === key); | ||
| return group ? toGroupItem(group) : { type: 'group', key, name: key, invalid: true }; | ||
| }); | ||
|
|
||
| return [...groupItems, ...userItems]; | ||
| }} | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| itemText={(item) => { | ||
| if (item.type === 'group') { | ||
| if (item.invalid) return `${item.name} (invalid)`; | ||
| return `${item.name} (${item.uids?.length || 0} users)`; | ||
| } | ||
| return item.name + (item.displayName ? ` (${item.displayName})` : ''); | ||
| }} | ||
| itemKey={(item) => item.key} | ||
| renderItem={(item) => ( | ||
| <div className="media"> | ||
| {item.type === 'user' ? ( | ||
| <div className="media__left medium"> | ||
| <img className="small user-profile-avatar" alt="" src={item.avatarUrl} width="30" height="30" /> | ||
| </div> | ||
| ) : null} | ||
|
TaiRuiQu marked this conversation as resolved.
Outdated
|
||
| <div className="media__body medium"> | ||
| <div className="assign-select__name"> | ||
| {item.name}{item.type === 'user' && item.displayName && ` (${item.displayName})`} | ||
| </div> | ||
| <div className="assign-select__desc"> | ||
| {item.type === 'group' ? <>Group • {item.uids?.length || 0} users</> : <>User • UID = {item.key}</>} | ||
|
TaiRuiQu marked this conversation as resolved.
Outdated
|
||
| </div> | ||
| </div> | ||
| </div> | ||
| )} | ||
| {...{ | ||
| width: '100%', | ||
| height: 'auto', | ||
| listStyle: {}, | ||
| multi: true, | ||
| selectedKeys: [], | ||
| allowEmptyQuery: false, | ||
| freeSolo: false, | ||
| freeSoloConverter: (input) => input, | ||
| ...props, | ||
| }} | ||
| /> | ||
| )); | ||
|
|
||
| AssignSelectAutoComplete.propTypes = { | ||
| width: PropTypes.string, | ||
| height: PropTypes.string, | ||
| listStyle: PropTypes.object, | ||
| onChange: PropTypes.func.isRequired, | ||
| multi: PropTypes.bool, | ||
| selectedKeys: PropTypes.arrayOf(PropTypes.string), | ||
| allowEmptyQuery: PropTypes.bool, | ||
| freeSolo: PropTypes.bool, | ||
| freeSoloConverter: PropTypes.func, | ||
| }; | ||
|
TaiRuiQu marked this conversation as resolved.
|
||
|
|
||
| AssignSelectAutoComplete.displayName = 'AssignSelectAutoComplete'; | ||
|
|
||
| export default AssignSelectAutoComplete; | ||
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
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.