|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { |
| 3 | + Banner, |
| 4 | + BannerVariant, |
| 5 | + Body, |
| 6 | + css, |
| 7 | + Link, |
| 8 | + spacing, |
| 9 | + Code, |
| 10 | + Subtitle, |
| 11 | + Label, |
| 12 | + SpinLoader, |
| 13 | + Button, |
| 14 | +} from '@mongodb-js/compass-components'; |
| 15 | +import { connect } from 'react-redux'; |
| 16 | +import { |
| 17 | + ShardingStatuses, |
| 18 | + unmanageNamespace, |
| 19 | + type RootState, |
| 20 | + type ShardKey, |
| 21 | + type ShardZoneData, |
| 22 | +} from '../../store/reducer'; |
| 23 | +import toNS from 'mongodb-ns'; |
| 24 | +import { ShardZonesTable } from '../shard-zones-table'; |
| 25 | + |
| 26 | +const nbsp = '\u00a0'; |
| 27 | + |
| 28 | +const containerStyles = css({ |
| 29 | + display: 'flex', |
| 30 | + flexDirection: 'column', |
| 31 | + gap: spacing[400], |
| 32 | +}); |
| 33 | + |
| 34 | +const codeBlockContainerStyles = css({ |
| 35 | + display: 'flex', |
| 36 | + flexDirection: 'column', |
| 37 | + gap: spacing[100], |
| 38 | + maxWidth: '700px', |
| 39 | +}); |
| 40 | + |
| 41 | +const paragraphStyles = css({ |
| 42 | + display: 'flex', |
| 43 | + flexDirection: 'column', |
| 44 | + gap: spacing[100], |
| 45 | +}); |
| 46 | + |
| 47 | +type ShardKeyCorrectProps = { |
| 48 | + namespace: string; |
| 49 | + shardKey: ShardKey; |
| 50 | + shardZones: ShardZoneData[]; |
| 51 | + isUnmanagingNamespace: boolean; |
| 52 | + onUnmanageNamespace: () => void; |
| 53 | +}; |
| 54 | + |
| 55 | +export function ShardKeyCorrect({ |
| 56 | + namespace, |
| 57 | + shardKey, |
| 58 | + shardZones, |
| 59 | + isUnmanagingNamespace, |
| 60 | + onUnmanageNamespace, |
| 61 | +}: ShardKeyCorrectProps) { |
| 62 | + const customShardKeyField = useMemo(() => { |
| 63 | + return shardKey.fields[1].name; |
| 64 | + }, [shardKey]); |
| 65 | + |
| 66 | + const sampleCodes = useMemo(() => { |
| 67 | + const { collection } = toNS(namespace); |
| 68 | + return { |
| 69 | + findingDocuments: `use ${collection}\ndb.${collection}.find({"location": "US-NY", "${customShardKeyField}": "<id_value>"})`, |
| 70 | + insertingDocuments: `use ${collection}\ndb.${collection}.insertOne({"location": "US-NY", "${customShardKeyField}": "<id_value>",...<other fields>})`, |
| 71 | + }; |
| 72 | + }, [namespace, customShardKeyField]); |
| 73 | + |
| 74 | + return ( |
| 75 | + <div className={containerStyles}> |
| 76 | + <Banner variant={BannerVariant.Info}> |
| 77 | + <strong> |
| 78 | + All documents in your collection should contain both the ‘location’ |
| 79 | + field (with a ISO country or subdivision code) and your{' '} |
| 80 | + {customShardKeyField} field at insert time. |
| 81 | + </strong> |
| 82 | + {nbsp}We have included a table for reference below. |
| 83 | + </Banner> |
| 84 | + |
| 85 | + <div className={codeBlockContainerStyles}> |
| 86 | + <Body> |
| 87 | + <strong>{namespace}</strong> is configured with the following shard |
| 88 | + key: |
| 89 | + </Body> |
| 90 | + <Code language="js">{namespace}</Code> |
| 91 | + </div> |
| 92 | + |
| 93 | + <Subtitle>Example commands</Subtitle> |
| 94 | + <div className={paragraphStyles}> |
| 95 | + <Body> |
| 96 | + Start querying your database with some of the most{' '} |
| 97 | + <Link |
| 98 | + href="https://www.mongodb.com/docs/atlas/global-clusters" |
| 99 | + hideExternalIcon |
| 100 | + > |
| 101 | + common commands |
| 102 | + </Link>{' '} |
| 103 | + for Global Writes. |
| 104 | + </Body> |
| 105 | + <Body> |
| 106 | + Replace the text to perform operations on different documents. US-NY |
| 107 | + is an ISO 3166 location code referring to New York, United States. You |
| 108 | + can look up other ISO 3166 location codes below. |
| 109 | + </Body> |
| 110 | + </div> |
| 111 | + |
| 112 | + <div className={codeBlockContainerStyles}> |
| 113 | + <Label htmlFor="finding-documents">Finding documents</Label> |
| 114 | + <Code language="js">{sampleCodes.findingDocuments}</Code> |
| 115 | + </div> |
| 116 | + |
| 117 | + <div className={codeBlockContainerStyles}> |
| 118 | + <Label htmlFor="inserting-documents">Inserting documents</Label> |
| 119 | + <Code language="js">{sampleCodes.insertingDocuments}</Code> |
| 120 | + </div> |
| 121 | + |
| 122 | + <Subtitle>Location Codes</Subtitle> |
| 123 | + <div className={paragraphStyles}> |
| 124 | + <Body> |
| 125 | + Each document’s first field should include an ISO 3166-1 Alpha-2 code |
| 126 | + for the location it belongs to. |
| 127 | + </Body> |
| 128 | + <Body> |
| 129 | + We also support ISO 3166-2 subdivision codes for countries containing |
| 130 | + a cloud provider data center (both ISO 3166-1 and ISO 3166-2 codes may |
| 131 | + be used for these countries). All valid country codes and the zones to |
| 132 | + which they map are listed in the table below. Additionally, you can |
| 133 | + view a list of all location codes{' '} |
| 134 | + <Link |
| 135 | + href="https://cloud-dev.mongodb.com/static/atlas/country_iso_codes.txt" |
| 136 | + hideExternalIcon |
| 137 | + > |
| 138 | + here |
| 139 | + </Link> |
| 140 | + . |
| 141 | + </Body> |
| 142 | + <Body> |
| 143 | + Locations’ zone mapping can be changed by navigating to this clusters{' '} |
| 144 | + <Link |
| 145 | + hideExternalIcon |
| 146 | + href="https://cloud-dev.mongodb.com/v2/66bb81dafe547055785904a3#/clusters/edit/Cluster0" |
| 147 | + > |
| 148 | + Edit Configuration |
| 149 | + </Link>{' '} |
| 150 | + page and clicking the Configure Location Mappings’ link above the map. |
| 151 | + </Body> |
| 152 | + </div> |
| 153 | + |
| 154 | + <ShardZonesTable shardZones={shardZones} /> |
| 155 | + |
| 156 | + <Subtitle>Unmanage this collection</Subtitle> |
| 157 | + <Body> |
| 158 | + Documents belonging to this collection will no longer be distributed |
| 159 | + across the shards of your global clusters. |
| 160 | + </Body> |
| 161 | + <div> |
| 162 | + <Button |
| 163 | + data-testid="shard-collection-button" |
| 164 | + onClick={onUnmanageNamespace} |
| 165 | + disabled={isUnmanagingNamespace} |
| 166 | + variant="primary" |
| 167 | + leftGlyph={ |
| 168 | + isUnmanagingNamespace ? ( |
| 169 | + <SpinLoader title="Unmanaging collection" /> |
| 170 | + ) : undefined |
| 171 | + } |
| 172 | + > |
| 173 | + Unmanage collection |
| 174 | + </Button> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + ); |
| 178 | +} |
| 179 | + |
| 180 | +export default connect( |
| 181 | + (state: RootState) => ({ |
| 182 | + namespace: state.namespace, |
| 183 | + // For this view, sharKey is always defined |
| 184 | + shardKey: state.shardKey as ShardKey, |
| 185 | + shardZones: state.shardZones, |
| 186 | + isUnmanagingNamespace: |
| 187 | + state.status === ShardingStatuses.UNMANAGING_NAMESPACE, |
| 188 | + }), |
| 189 | + { |
| 190 | + onUnmanageNamespace: unmanageNamespace, |
| 191 | + } |
| 192 | +)(ShardKeyCorrect); |
0 commit comments