Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | 3x 3x 36x 36x 36x 36x 36x 36x 36x 9x 1x 8x 8x 8x 16x 16x 24x 24x 24x 16x 8x 8x 8x 8x 8x 8x 36x 6x 6x 5x 4x 4x 4x 2x 36x 12x 2x 36x 1x 35x 7x 140x 13x 3x | import {
Button,
ButtonVariant,
Text as TextComponent,
TextVariant,
Box,
FontWeight,
ButtonSize,
TextColor,
} from '@metamask/design-system-react';
import { stringify } from '@metamask/kernel-utils';
import type { Json } from '@metamask/utils';
import { useState, useMemo } from 'react';
import { usePanelContext } from '../context/PanelContext.tsx';
import { useRegistry } from '../hooks/useRegistry.ts';
const inputStyle =
'flex items-center h-9 px-3 rounded border border-border-default text-sm bg-background-default text-text-default transition-colors hover:bg-background-hover focus:outline-none focus:ring-2 focus:ring-primary-default';
/**
* Renders a form for users to queue a message to a vat.
*
* @returns JSX element for queue message form
*/
export const SendMessageForm: React.FC = () => {
const { callKernelMethod, logMessage, objectRegistry } = usePanelContext();
const { fetchObjectRegistry } = useRegistry();
const [target, setTarget] = useState('');
const [method, setMethod] = useState('__getMethodNames__');
const [paramsText, setParamsText] = useState('[]');
const [result, setResult] = useState<Json | null>(null);
// Build list of object KRef targets with their owner vat names
const targets = useMemo(() => {
if (!objectRegistry) {
return [];
}
const seen = new Set<string>();
const list: { label: string; value: string }[] = [];
for (const [vatId, vat] of Object.entries(objectRegistry.vats)) {
const ownerName = vat.overview.name ?? vatId;
// Owned objects
for (const obj of vat.ownedObjects) {
Eif (!seen.has(obj.kref)) {
seen.add(obj.kref);
list.push({ label: `${obj.kref} (${ownerName})`, value: obj.kref });
}
}
// Imported objects
for (const obj of vat.importedObjects) {
const originVat = obj.fromVat ?? vatId;
const originName =
objectRegistry.vats[originVat]?.overview.name ?? originVat;
Eif (!seen.has(obj.kref)) {
seen.add(obj.kref);
list.push({ label: `${obj.kref} (${originName})`, value: obj.kref });
}
}
}
return list;
}, [objectRegistry]);
const handleSend = (): void => {
Promise.resolve()
.then(() => JSON.parse(paramsText) as Json[])
.then(async (args) =>
callKernelMethod({
method: 'queueMessage',
params: [target, method, args],
}),
)
.then((response) => {
setResult(response);
logMessage(stringify(response), 'received');
return fetchObjectRegistry();
})
.catch((error) => logMessage(String(error), 'error'));
};
const handleKeyDown = (
event: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>,
): void => {
if (event.key === 'Enter') {
handleSend();
}
};
if (!objectRegistry) {
return <></>;
}
return (
<Box className="bg-section p-4 rounded mb-4">
<TextComponent
variant={TextVariant.BodySm}
fontWeight={FontWeight.Medium}
className="mb-4"
>
Send Message
</TextComponent>
<Box className="flex flex-col sm:flex-row gap-3">
<Box className="flex flex-col flex-1 lg:flex-none lg:w-[200px]">
<label
htmlFor="message-target"
className="mb-1 text-sm text-text-default"
>
Target:
</label>
<select
id="message-target"
value={target}
onChange={(event) => setTarget(event.target.value)}
data-testid="message-target"
className={`${inputStyle} cursor-pointer`}
>
<option value="" disabled>
Select target
</option>
{targets.map(({ label, value }) => (
<option key={value} value={value}>
{label}
</option>
))}
</select>
</Box>
<Box className="flex flex-col flex-1">
<label
htmlFor="message-method"
className="mb-1 text-sm text-text-default"
>
Method:
</label>
<input
id="message-method"
type="text"
value={method}
onChange={(event) => setMethod(event.target.value)}
placeholder="methodName"
onKeyDown={handleKeyDown}
data-testid="message-method"
className={inputStyle}
/>
</Box>
<Box className="flex flex-col flex-1">
<label
htmlFor="message-params"
className="mb-1 text-sm text-text-default"
>
Params (JSON):
</label>
<input
id="message-params"
value={paramsText}
onChange={(event) => setParamsText(event.target.value)}
placeholder="[arg1, arg2]"
onKeyDown={handleKeyDown}
data-testid="message-params"
className={inputStyle}
/>
</Box>
<Box className="flex flex-none lg:w-[80px] items-end">
<Button
variant={ButtonVariant.Primary}
size={ButtonSize.Sm}
onClick={handleSend}
isDisabled={!(target.trim() && method.trim())}
className="h-9 rounded-md"
data-testid="message-send-button"
>
<TextComponent
variant={TextVariant.BodyMd}
color={TextColor.PrimaryInverse}
className="select-none"
>
Send
</TextComponent>
</Button>
</Box>
</Box>
{result && (
<Box className="mt-4 font-mono text-sm" data-testid="message-response">
<TextComponent
variant={TextVariant.BodySm}
fontWeight={FontWeight.Medium}
className="mb-2"
>
Response:
</TextComponent>
<pre className="p-3 rounded overflow-auto bg-background-default text-text-default">
{stringify(result, 0)}
</pre>
</Box>
)}
</Box>
);
};
|