-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(code connect): add function to get list of figma component ids
- Loading branch information
1 parent
0f3b31a
commit 6644d36
Showing
3 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const figmaComonentsUrl = | ||
'https://api.figma.com/v1/files/<your-file-key-here>/components'; | ||
const figmaToken = 'your-figma-token-here'; | ||
|
||
/** | ||
* Fetches the figma component ids from the figma api | ||
* @returns {Map<string, {name: string, nodeId: string}>} - A map of component names to their corresponding node ids | ||
* @throws {Error} - If the fetch request fails | ||
* @example | ||
* const componentIds = await fetchFigmaComponentIds(); | ||
* console.log(componentIds.get('button')); | ||
* // Output: { name: 'button', nodeId: '0:1' } | ||
**/ | ||
|
||
async function fetchFigmaComponentIds() { | ||
try { | ||
const response = await fetch(figmaComonentsUrl, { | ||
method: 'GET', | ||
headers: { | ||
'X-FIGMA-TOKEN': figmaToken, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
const componentListArray = data.meta.components | ||
// Filter out components that do not have a `containingStateGroup` property | ||
// as it contains the correct nodeId for the component | ||
.filter((component) => | ||
component.containing_frame.hasOwnProperty('containingStateGroup') | ||
) | ||
// create a new array with a normalized name of the component | ||
// and the encoded node id of the component | ||
.map((component) => { | ||
return { | ||
name: component.containing_frame.containingStateGroup.name | ||
.split('/') | ||
.pop() | ||
.toLowerCase() | ||
.replace(/[\s\-]/g, ''), | ||
nodeId: encodeURIComponent( | ||
component.containing_frame.containingStateGroup.nodeId | ||
), | ||
}; | ||
}); | ||
// Use a Map arranged by name for lookups TODO: needs to be deduped | ||
return new Map( | ||
componentListArray.map((component) => [component.name, component]) | ||
); | ||
} catch (error) { | ||
console.error(`Error fetching data: ${error}`); | ||
} | ||
} | ||
|
||
module.exports = { fetchFigmaComponentIds }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// @ts-nocheck | ||
import { Link } from '@commercetools-frontend/ui-kit'; | ||
import figma from '@figma/code-connect'; | ||
|
||
// REQUIRED: supply node id for the figma component | ||
figma.connect( | ||
Link, | ||
'https://www.figma.com/design/UoqmtHSHwxvEZRcbsM4A3Z/CT-product-design-system?node-id=118%3A17594', | ||
{ | ||
props: { | ||
// *This file was generated from a script* | ||
// TODO: manually map props here, see https://www.figma.com/code-connect-docs/react/#figmaconnect | ||
children: figma.children('*'), | ||
}, | ||
example: (props) => <Link>{props.children}</Link>, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters