-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.tsx
More file actions
105 lines (91 loc) · 3.13 KB
/
Copy pathindex.tsx
File metadata and controls
105 lines (91 loc) · 3.13 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
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
import { createWebpackLoader, getTopic, type Locale, type Technique, type Framework } from '@appt.org/samples';
import { useEffect, useState } from 'react';
import MDXCode from '@theme/MDXComponents/Code';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import clsx from 'clsx';
import React from 'react';
import styles from './styles.module.css';
const context = require.context('@appt.org/samples/samples', true, /\.md$/, 'lazy');
const loader = createWebpackLoader(context);
const frameworks: Framework[] = [
'android',
'jetpack-compose',
'ios',
'swiftui',
'flutter',
'react-native',
'net-maui',
'xamarin',
];
export type CodeSampleProps = {
id: Technique;
framework?: Framework;
locale: Locale;
};
export function CodeSample({ id, framework }: CodeSampleProps) {
const [codeBlocks, setCodeBlocks] = useState(null);
useEffect(() => {
const getBlocks = async () => {
try {
const topic = await getTopic(loader, {
locale: ['en'],
technique: id,
frameworks: framework ? [framework] : undefined,
});
return topic.samples.map(sample => {
// Inject `url` in metastring, used in `CodeBlockString` function
const components = {
code: props => {
const urlMeta = `url="${sample.url}"`;
const metastring = props.metastring ? `${props.metastring} ${urlMeta}` : urlMeta;
return (
<MDXCode {...props} metastring={metastring}>
{props.children}
</MDXCode>
);
},
};
return {
framework: sample.framework.id,
label: sample.framework.label,
locale: sample.locale,
url: sample.url,
content: <sample.content.default components={components} />,
};
});
} catch (error) {
console.error('Failed to import MDX content:', error);
return [];
}
};
const setBlocks = async () => {
const codeBlocks = (await getBlocks()).filter(block => block);
// Sort the code blocks according to the order in frameworks array
const sortedCodeBlocks = [...codeBlocks].sort((a, b) => {
const indexA = frameworks.indexOf(a.framework);
const indexB = frameworks.indexOf(b.framework);
return (indexA === -1 ? Infinity : indexA) - (indexB === -1 ? Infinity : indexB);
});
setCodeBlocks(sortedCodeBlocks);
};
setBlocks();
}, []);
if (codeBlocks) {
const firstCodeBlock = codeBlocks[0];
return (
<div className={clsx(styles.codeSampleContainer, 'mt-10 mb-12 last:mb-0 md:mb-20')}>
{codeBlocks.length > 1 && (
<Tabs className={styles.codeSampleTabs} groupId="framework" queryString>
{codeBlocks.map((codeBlock, index) => (
<TabItem key={index} value={codeBlock.framework} label={codeBlock.label}>
<div lang={codeBlock.locale}>{codeBlock.content}</div>
</TabItem>
))}
</Tabs>
)}
{codeBlocks.length === 1 && firstCodeBlock.content}
</div>
);
}
}