-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ffb3d0
commit 48e0182
Showing
8 changed files
with
226 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
126 changes: 126 additions & 0 deletions
126
frontend/apps/desktop/src/components/collaborators-panel.tsx
This file contains 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,126 @@ | ||
import { | ||
useAddCapabilities, | ||
useAllDocumentCapabilities, | ||
useMyCapability, | ||
} from '@/models/access-control' | ||
import {useSearch} from '@/models/search' | ||
import {DocumentRoute} from '@/utils/routes' | ||
import {Role, UnpackedHypermediaId, unpackHmId} from '@shm/shared' | ||
import {Button, Input, Label, SizableText, Text} from '@shm/ui' | ||
import {useState} from 'react' | ||
import {AccessoryContainer} from './accessory-sidebar' | ||
|
||
export function CollaboratorsPanel({ | ||
route, | ||
onClose, | ||
}: { | ||
route: DocumentRoute | ||
onClose: () => void | ||
}) { | ||
return ( | ||
<AccessoryContainer title="Collaborators" onClose={onClose}> | ||
<AddCollaboratorForm id={route.id} /> | ||
<Label>Collaborators</Label> | ||
<CollaboratorsList id={route.id} /> | ||
</AccessoryContainer> | ||
) | ||
} | ||
|
||
type SearchResult = { | ||
id: UnpackedHypermediaId | ||
label: string | ||
} | ||
|
||
function AddCollaboratorForm({id}: {id: UnpackedHypermediaId}) { | ||
const myCapability = useMyCapability(id, 'admin') | ||
const addCapabilities = useAddCapabilities(id) | ||
const [selectedCollaborators, setSelectedCollaborators] = useState< | ||
SearchResult[] | ||
>([]) | ||
const [search, setSearch] = useState('') | ||
const searchResults = useSearch(search, {}) | ||
if (!myCapability) return null | ||
return ( | ||
<> | ||
<Label>Add Collaborator</Label> | ||
{selectedCollaborators.map((collab) => ( | ||
<SizableText key={collab.id.id}>{collab.label}</SizableText> | ||
))} | ||
<Input | ||
value={search} | ||
onChangeText={(searchText: string) => { | ||
console.log(searchText) | ||
setSearch(searchText) | ||
}} | ||
/> | ||
{searchResults.data | ||
?.map((result) => { | ||
const id = unpackHmId(result.id) | ||
if (!id) return null | ||
return {id, label: result.title} | ||
}) | ||
.filter((result) => { | ||
if (!result) return false // probably id was not parsed correctly | ||
if (result.id.path?.length) return false // this is a directory document, not an account | ||
if (result.id.uid === id.uid) return false // this account is already the owner, cannot be added | ||
if ( | ||
selectedCollaborators.find( | ||
(collab) => collab.id.id === result.id.id, | ||
) | ||
) | ||
return false // already added | ||
return true | ||
}) | ||
.map( | ||
(result) => | ||
result && ( | ||
<SearchResultItem | ||
key={result.id.id} | ||
result={result} | ||
onSelect={() => { | ||
setSelectedCollaborators((collabs) => [...collabs, result]) | ||
}} | ||
/> | ||
), | ||
)} | ||
{/* <SelectDropdown options={RoleOptions} onSelect={() = > {}} /> // not relevant yet because we can only add writers | ||
*/} | ||
<Text>{JSON.stringify(myCapability)}</Text> | ||
<Button | ||
onPress={() => { | ||
addCapabilities.mutate({ | ||
myCapability: myCapability, | ||
collaboratorAccountIds: selectedCollaborators.map( | ||
(collab) => collab.id.uid, | ||
), | ||
role: Role.WRITER, | ||
}) | ||
}} | ||
> | ||
Add Writers | ||
</Button> | ||
</> | ||
) | ||
} | ||
|
||
function SearchResultItem({ | ||
result, | ||
onSelect, | ||
}: { | ||
result: SearchResult | ||
onSelect: () => void | ||
}) { | ||
return <Button onPress={onSelect}>{result.label}</Button> | ||
} | ||
|
||
function CollaboratorsList({id}: {id: UnpackedHypermediaId}) { | ||
const capabilities = useAllDocumentCapabilities(id) | ||
return capabilities.data?.map((capability) => { | ||
return ( | ||
<SizableText> | ||
{capability.account} - {capability.role} | ||
</SizableText> | ||
) | ||
}) | ||
return null | ||
} |
This file contains 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 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,85 @@ | ||
import {useGRPCClient, useQueryInvalidator} from '@/app-context' | ||
import {toPlainMessage} from '@bufbuild/protobuf' | ||
import {Role, UnpackedHypermediaId} from '@shm/shared' | ||
import {useMutation, useQuery} from '@tanstack/react-query' | ||
import {useMyAccountIds} from './daemon' | ||
import {getParentPaths, hmIdPathToEntityQueryPath} from './entities' | ||
import {queryKeys} from './query-keys' | ||
|
||
export function useDocumentCollaborators(id: UnpackedHypermediaId) { | ||
// | ||
getParentPaths() | ||
} | ||
|
||
export function useAddCapabilities(id: UnpackedHypermediaId) { | ||
const grpcClient = useGRPCClient() | ||
const invalidate = useQueryInvalidator() | ||
return useMutation({ | ||
mutationFn: async ({ | ||
myCapability, | ||
collaboratorAccountIds, | ||
role, | ||
}: { | ||
myCapability: HMCapability | ||
collaboratorAccountIds: string[] | ||
role: Role | ||
}) => { | ||
await Promise.all( | ||
collaboratorAccountIds.map( | ||
async (collaboratorAccountId) => | ||
await grpcClient.accessControl.createCapability({ | ||
account: collaboratorAccountId, | ||
delegate: id.uid, | ||
role, | ||
path: hmIdPathToEntityQueryPath(id.path), | ||
signingKeyName: myCapability.accountUid, | ||
// noRecursive, // ? | ||
}), | ||
), | ||
) | ||
}, | ||
onSuccess: () => { | ||
invalidate([queryKeys.CAPABILITIES, id.uid, ...(id.path || [])]) | ||
}, | ||
}) | ||
} | ||
|
||
type CapabilityType = 'admin' | 'owner' | 'writer' | ||
|
||
type HMCapability = { | ||
accountUid: string | ||
role: CapabilityType | ||
} | ||
|
||
const CapabilityInheritance: Readonly<CapabilityType[]> = | ||
// used to determine when one capability can be used in place of another. all owners are writers, for example | ||
['owner', 'admin', 'writer'] | ||
|
||
export function useMyCapability( | ||
id: UnpackedHypermediaId, | ||
capability: 'admin' | 'owner' | 'writer', | ||
): HMCapability | null { | ||
const myAccounts = useMyAccountIds() | ||
const capabilities = useAllDocumentCapabilities(id) | ||
// todo! | ||
if (myAccounts.data?.indexOf(id.uid) !== -1) { | ||
// owner. no capability needed | ||
return {accountUid: id.uid, role: 'owner'} | ||
} | ||
return null | ||
} | ||
|
||
export function useAllDocumentCapabilities(id: UnpackedHypermediaId) { | ||
const grpcClient = useGRPCClient() | ||
return useQuery({ | ||
queryKey: [queryKeys.CAPABILITIES, id.uid, ...(id.path || [])], | ||
queryFn: async () => { | ||
const result = await grpcClient.accessControl.listCapabilities({ | ||
account: id.uid, | ||
path: hmIdPathToEntityQueryPath(id.path), | ||
}) | ||
const capabilities = result.capabilities.map(toPlainMessage) | ||
return capabilities | ||
}, | ||
}) | ||
} |
This file contains 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 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 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 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