|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Code, |
| 4 | + css, |
| 5 | + Label, |
| 6 | + Link, |
| 7 | + spacing, |
| 8 | + Subtitle, |
| 9 | +} from '@mongodb-js/compass-components'; |
| 10 | +import React, { useMemo } from 'react'; |
| 11 | +import type { ShardKey } from '../store/reducer'; |
| 12 | +import toNS from 'mongodb-ns'; |
| 13 | + |
| 14 | +const codeBlockContainerStyles = css({ |
| 15 | + display: 'flex', |
| 16 | + flexDirection: 'column', |
| 17 | + gap: spacing[100], |
| 18 | +}); |
| 19 | + |
| 20 | +export interface ExampleCommandsMarkupProps { |
| 21 | + shardKey: ShardKey; |
| 22 | + namespace: string; |
| 23 | + showMetaData?: boolean; |
| 24 | + type?: 'requested' | 'existing'; |
| 25 | +} |
| 26 | + |
| 27 | +const paragraphStyles = css({ |
| 28 | + display: 'flex', |
| 29 | + flexDirection: 'column', |
| 30 | + gap: spacing[100], |
| 31 | +}); |
| 32 | + |
| 33 | +export function ExampleCommandsMarkup({ |
| 34 | + namespace, |
| 35 | + shardKey, |
| 36 | +}: ExampleCommandsMarkupProps) { |
| 37 | + const customShardKeyField = useMemo(() => { |
| 38 | + return shardKey.fields[1].name; |
| 39 | + }, [shardKey]); |
| 40 | + |
| 41 | + const sampleCodes = useMemo(() => { |
| 42 | + const { collection, database } = toNS(namespace); |
| 43 | + return { |
| 44 | + findingDocuments: `use ${database}\ndb[${JSON.stringify( |
| 45 | + collection |
| 46 | + )}].find({"location": "US-NY", "${customShardKeyField}": "<id_value>"})`, |
| 47 | + insertingDocuments: `use ${database}\ndb[${JSON.stringify( |
| 48 | + collection |
| 49 | + )}].insertOne({"location": "US-NY", "${customShardKeyField}": "<id_value>",...<other fields>})`, |
| 50 | + }; |
| 51 | + }, [namespace, customShardKeyField]); |
| 52 | + |
| 53 | + return ( |
| 54 | + <> |
| 55 | + <Subtitle>Example commands</Subtitle> |
| 56 | + <div className={paragraphStyles}> |
| 57 | + <Body> |
| 58 | + Start querying your database with some of the most{' '} |
| 59 | + <Link |
| 60 | + href="https://www.mongodb.com/docs/atlas/global-clusters" |
| 61 | + hideExternalIcon |
| 62 | + > |
| 63 | + common commands |
| 64 | + </Link>{' '} |
| 65 | + for Global Writes. |
| 66 | + </Body> |
| 67 | + <Body> |
| 68 | + Replace the text to perform operations on different documents. US-NY |
| 69 | + is an ISO 3166 location code referring to New York, United States. You |
| 70 | + can look up other ISO 3166 location codes below. |
| 71 | + </Body> |
| 72 | + </div> |
| 73 | + |
| 74 | + <div className={codeBlockContainerStyles}> |
| 75 | + <Label htmlFor="finding-documents">Finding documents</Label> |
| 76 | + <Code |
| 77 | + language="js" |
| 78 | + data-testid="sample-finding-documents" |
| 79 | + id="finding-documents" |
| 80 | + > |
| 81 | + {sampleCodes.findingDocuments} |
| 82 | + </Code> |
| 83 | + </div> |
| 84 | + |
| 85 | + <div className={codeBlockContainerStyles}> |
| 86 | + <Label htmlFor="inserting-documents">Inserting documents</Label> |
| 87 | + <Code |
| 88 | + language="js" |
| 89 | + data-testid="sample-inserting-documents" |
| 90 | + id="inserting-documents" |
| 91 | + > |
| 92 | + {sampleCodes.insertingDocuments} |
| 93 | + </Code> |
| 94 | + </div> |
| 95 | + </> |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +export default ExampleCommandsMarkup; |
0 commit comments