-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathmetadata-decoder.svelte
More file actions
69 lines (58 loc) · 1.78 KB
/
metadata-decoder.svelte
File metadata and controls
69 lines (58 loc) · 1.78 KB
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
<script lang="ts">
import { page } from '$app/stores';
import type { Payload } from '$lib/types';
import { decodeSingleReadablePayloadWithCodec } from '$lib/utilities/decode-payload';
import {
getCodecEndpoint,
getCodecIncludeCredentials,
getCodecPassAccessToken,
} from '$lib/utilities/get-codec';
export let value: Payload | undefined = undefined;
export let fallback: string = '';
export let prefix: string = '';
export let onDecode: (decodedValue: string) => void | undefined = undefined;
const maxLength = 120;
let decodedValue = '';
$: endpoint = getCodecEndpoint($page.data.settings);
$: passAccessToken = getCodecPassAccessToken($page.data.settings);
$: includeCredentials = getCodecIncludeCredentials($page.data.settings);
$: settings = {
...$page.data.settings,
codec: {
...$page.data.settings?.codec,
endpoint,
passAccessToken,
includeCredentials,
},
};
const setPrefix = (metadata: string) => {
if (prefix) {
metadata = `${prefix} • ${metadata}`;
if (metadata.length < maxLength) return metadata;
return metadata.slice(0, maxLength) + '...';
}
return metadata;
};
$: decodePayload = async (_value: Payload | undefined) => {
if (!_value) return fallback;
if (decodedValue) return decodedValue;
const metadata = await decodeSingleReadablePayloadWithCodec(
_value,
settings,
);
if (typeof metadata === 'string') {
decodedValue = setPrefix(metadata);
if (onDecode) {
onDecode(decodedValue);
}
return decodedValue;
}
decodedValue = fallback;
return fallback;
};
</script>
{#await decodePayload(value) then metadata}
<slot decodedValue={metadata} />
{:catch}
<slot decodedValue={fallback} />
{/await}