Skip to content

Commit

Permalink
Lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Feb 19, 2024
1 parent d329fd8 commit b766d85
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,3 @@ fn get_key_collections() -> ExternResult<Vec<(EntryHash, Record)>> {

Ok(records)
}

1 change: 0 additions & 1 deletion dnas/trusted/zomes/integrity/trusted/src/key_collection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use hdi::prelude::*;

use crate::prelude::GpgKeyDist;
use crate::LinkTypes;
use crate::UnitEntryTypes;

Expand Down
20 changes: 12 additions & 8 deletions dnas/trusted/zomes/integrity/trusted/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
target_address,
link_type,
),
LinkTypes::KeyCollectionToGpgKeyDist => key_collection::validate_key_collection_to_gpg_key_dist_link(
target_address,
link_type,
),
LinkTypes::KeyCollectionToGpgKeyDist => {
key_collection::validate_key_collection_to_gpg_key_dist_link(
target_address,
link_type,
)
}
},
FlatOp::RegisterDeleteLink { .. } => Ok(ValidateCallbackResult::Invalid(String::from(
"There are no link types in this integrity zome",
Expand Down Expand Up @@ -315,10 +317,12 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
target_address,
link_type,
),
LinkTypes::KeyCollectionToGpgKeyDist => key_collection::validate_key_collection_to_gpg_key_dist_link(
target_address,
link_type,
),
LinkTypes::KeyCollectionToGpgKeyDist => {
key_collection::validate_key_collection_to_gpg_key_dist_link(
target_address,
link_type,
)
}
},
// Complementary validation to the `RegisterDeleteLink` Op, in which the record itself is validated
// If you want to optimize performance, you can remove the validation for an entry type here and keep it in `RegisterDeleteLink`
Expand Down
6 changes: 4 additions & 2 deletions ui/src/component/KeyList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defineProps<{
}>();
const emit = defineEmits<{
(e: 'add-key', key: GpgKeyDist): void;
(e: "add-key", key: GpgKeyDist): void;
}>();
const myKeysStore = useMyKeysStore();
Expand Down Expand Up @@ -40,7 +40,9 @@ const isMine = (keyDist: GpgKeyDist) => {
<td v-if="!readonly">
<p v-if="isMine(k)" class="font-bold text-primary">Mine</p>
<div v-else>
<button class="btn btn-primary" @click="() => emit('add-key', k)">Add</button>
<button class="btn btn-primary" @click="() => emit('add-key', k)">
Add
</button>
</div>
</td>
</tr>
Expand Down
99 changes: 49 additions & 50 deletions ui/src/store/key-collections-store.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
import { AppAgentClient, Record } from "@holochain/client";
import { AppAgentClient } from "@holochain/client";
import { defineStore } from "pinia";
import { ComputedRef, inject, ref, watch } from "vue";
import { GpgKeyDist } from "../trusted/trusted/types";
import { registerSignalHandler } from "../signals";

export interface KeyCollectionWithKeys {
name: string;
gpg_keys: GpgKeyDist[];
name: string;
gpg_keys: GpgKeyDist[];
}

export const useKeyCollectionsStore = defineStore("key-collections", () => {
const keyCollections = ref<KeyCollectionWithKeys[]>([]);

const pushKeyCollection = (collection: KeyCollectionWithKeys) => {
keyCollections.value.push(collection);
};

const addKeyToCollection = (name: string, key: GpgKeyDist) => {
const existingCollection = keyCollections.value.find((c) => c.name === name);
if (existingCollection) {
existingCollection.gpg_keys.push(key);
}
};

const client = inject("client") as ComputedRef<AppAgentClient>;
const loadKeyCollections = async (client: AppAgentClient) => {
const collections: KeyCollectionWithKeys[] = await client.callZome({
role_name: "trusted",
zome_name: "trusted",
fn_name: "get_my_key_collections",
payload: null,
cap_secret: null,
});

console.log('loaded key collections', collections);

keyCollections.value = [
...collections,
...keyCollections.value,
];
};

watch(
client,
(client) => {
registerSignalHandler(client, {
keyCollectionsStore: { pushKeyCollection },
});

loadKeyCollections(client);
},
{ immediate: true },
);
const keyCollections = ref<KeyCollectionWithKeys[]>([]);

const pushKeyCollection = (collection: KeyCollectionWithKeys) => {
keyCollections.value.push(collection);
};

return {
keyCollections,
pushKeyCollection,
addKeyToCollection,
};
const addKeyToCollection = (name: string, key: GpgKeyDist) => {
const existingCollection = keyCollections.value.find(
(c) => c.name === name,
);
if (existingCollection) {
existingCollection.gpg_keys.push(key);
}
};

const client = inject("client") as ComputedRef<AppAgentClient>;
const loadKeyCollections = async (client: AppAgentClient) => {
const collections: KeyCollectionWithKeys[] = await client.callZome({
role_name: "trusted",
zome_name: "trusted",
fn_name: "get_my_key_collections",
payload: null,
cap_secret: null,
});

console.log("loaded key collections", collections);

keyCollections.value = [...collections, ...keyCollections.value];
};

watch(
client,
(client) => {
registerSignalHandler(client, {
keyCollectionsStore: { pushKeyCollection },
});

loadKeyCollections(client);
},
{ immediate: true },
);

return {
keyCollections,
pushKeyCollection,
addKeyToCollection,
};
});
176 changes: 101 additions & 75 deletions ui/src/trusted/trusted/AddKeyToCollection.vue
Original file line number Diff line number Diff line change
@@ -1,106 +1,132 @@
<script setup lang="ts">
import { GpgKeyDist } from './types';
import KeyList from '../../component/KeyList.vue';
import { useKeyCollectionsStore } from '../../store/key-collections-store';
import { ComputedRef, inject, ref, watch } from 'vue';
import CreateKeyCollection from './CreateKeyCollection.vue';
import { useNotificationsStore } from '../../store/notifications-store';
import { AppAgentClient } from '@holochain/client';
import { GpgKeyDist } from "./types";
import KeyList from "../../component/KeyList.vue";
import { useKeyCollectionsStore } from "../../store/key-collections-store";
import { ComputedRef, inject, ref, watch } from "vue";
import CreateKeyCollection from "./CreateKeyCollection.vue";
import { useNotificationsStore } from "../../store/notifications-store";
import { AppAgentClient } from "@holochain/client";
const props = defineProps<{
selectedKey: GpgKeyDist;
selectedKey: GpgKeyDist;
}>();
const client = inject("client") as ComputedRef<AppAgentClient>;
const keyCollectionsStore = useKeyCollectionsStore();
const notificationsStore = useNotificationsStore();
const selectedCollection = ref<string>('');
const selectedCollection = ref<string>("");
const expectCreated = ref<string | null>(null);
const linking = ref(false);
watch(keyCollectionsStore.keyCollections, (newVal) => {
watch(
keyCollectionsStore.keyCollections,
(newVal) => {
if (newVal.length > 0) {
if (expectCreated.value && newVal.map(c => c.name).indexOf(expectCreated.value) !== -1) {
selectedCollection.value = expectCreated.value;
expectCreated.value = null;
} else {
selectedCollection.value = newVal[0].name;
}
if (
expectCreated.value &&
newVal.map((c) => c.name).indexOf(expectCreated.value) !== -1
) {
selectedCollection.value = expectCreated.value;
expectCreated.value = null;
} else {
selectedCollection.value = newVal[0].name;
}
}
}, { immediate: true });
},
{ immediate: true },
);
const onKeyCollectionCreated = (name: string) => {
expectCreated.value = name;
expectCreated.value = name;
// To reset the UI if something goes wrong, 5s might be a bit aggressive but if the signal shows up then the select will show the new collection anyway.
setTimeout(() => {
// If we haven't started creating a new collection, then we should clear this field
if (expectCreated.value === name) {
expectCreated.value = null;
notificationsStore.pushNotification({
message: `New key collection '${name}' may not have been created`,
type: 'warning',
});
}
}, 5000);
// To reset the UI if something goes wrong, 5s might be a bit aggressive but if the signal shows up then the select will show the new collection anyway.
setTimeout(() => {
// If we haven't started creating a new collection, then we should clear this field
if (expectCreated.value === name) {
expectCreated.value = null;
notificationsStore.pushNotification({
message: `New key collection '${name}' may not have been created`,
type: "warning",
});
}
}, 5000);
};
const addKeyToCollection = () => {
if (!selectedCollection.value || !client.value || linking.value) return;
if (!selectedCollection.value || !client.value || linking.value) return;
linking.value = true;
linking.value = true;
try {
client.value.callZome({
cap_secret: null,
role_name: 'trusted',
zome_name: 'trusted',
fn_name: 'link_gpg_key_to_key_collection',
payload: {
gpg_key_fingerprint: props.selectedKey.fingerprint,
key_collection_name: selectedCollection.value,
},
});
try {
client.value.callZome({
cap_secret: null,
role_name: "trusted",
zome_name: "trusted",
fn_name: "link_gpg_key_to_key_collection",
payload: {
gpg_key_fingerprint: props.selectedKey.fingerprint,
key_collection_name: selectedCollection.value,
},
});
keyCollectionsStore.addKeyToCollection(selectedCollection.value, props.selectedKey);
keyCollectionsStore.addKeyToCollection(
selectedCollection.value,
props.selectedKey,
);
notificationsStore.pushNotification({
message: `Key added to collection '${selectedCollection.value}'`,
type: 'info',
});
} catch (e: any) {
notificationsStore.pushNotification({
message: `Error adding key to collection - ${e}`,
type: 'error',
timeout: 5000,
});
} {
linking.value = false;
}
}
notificationsStore.pushNotification({
message: `Key added to collection '${selectedCollection.value}'`,
type: "info",
});
} catch (e: any) {
notificationsStore.pushNotification({
message: `Error adding key to collection - ${e}`,
type: "error",
timeout: 5000,
});
}
{
linking.value = false;
}
};
</script>

<template>
<KeyList :keys="[selectedKey]" :readonly="true"></KeyList>
<KeyList :keys="[selectedKey]" :readonly="true"></KeyList>

<p class="mt-5">Pick a key collection</p>
<div class="join w-full">
<select v-model="selectedCollection" class="select select-bordered w-full join-item">
<option disabled value="">Select a key collection</option>
<option v-for="c in keyCollectionsStore.keyCollections" v-bind:key="c.name" :value="c.name">
{{ c.name }}
</option>
</select>
<button class="btn btn-primary join-item" :disabled="!selectedCollection" @click="addKeyToCollection">Add to collection</button>
</div>
<p class="mt-5">Pick a key collection</p>
<div class="join w-full">
<select
v-model="selectedCollection"
class="select select-bordered w-full join-item"
>
<option disabled value="">Select a key collection</option>
<option
v-for="c in keyCollectionsStore.keyCollections"
v-bind:key="c.name"
:value="c.name"
>
{{ c.name }}
</option>
</select>
<button
class="btn btn-primary join-item"
:disabled="!selectedCollection"
@click="addKeyToCollection"
>
Add to collection
</button>
</div>

<p class="mt-5">Or create a new one</p>
<div v-if="expectCreated" class="flex grow justify-center">
<span class="loading loading-spinner"></span>
</div>
<div v-else>
<CreateKeyCollection @created="onKeyCollectionCreated"></CreateKeyCollection>
</div>
<p class="mt-5">Or create a new one</p>
<div v-if="expectCreated" class="flex grow justify-center">
<span class="loading loading-spinner"></span>
</div>
<div v-else>
<CreateKeyCollection
@created="onKeyCollectionCreated"
></CreateKeyCollection>
</div>
</template>
Loading

0 comments on commit b766d85

Please sign in to comment.