|
| 1 | +import * as React from "react"; |
| 2 | +import { Query } from "react-apollo" |
| 3 | +import gql from "graphql-tag" |
| 4 | + |
| 5 | +export const FigmaContext = React.createContext({ |
| 6 | + fileId: null, |
| 7 | + pageName: null |
| 8 | +}) |
| 9 | + |
| 10 | +export const rectFragment = gql` |
| 11 | + fragment Rect on Node { |
| 12 | + position { |
| 13 | + x |
| 14 | + y |
| 15 | + } |
| 16 | + size { |
| 17 | + width |
| 18 | + height |
| 19 | + } |
| 20 | + } |
| 21 | +` |
| 22 | + |
| 23 | +export const childrenFragment = gql` |
| 24 | + fragment ChildrenOfName on Frame { |
| 25 | + children(name: $nodeName) { |
| 26 | + ... on Frame { |
| 27 | + id |
| 28 | + image(params: { format: "svg" }) |
| 29 | + ...Rect |
| 30 | + } |
| 31 | + ... on Text { |
| 32 | + id |
| 33 | + name |
| 34 | + visible |
| 35 | + style { |
| 36 | + fontSize |
| 37 | + fontFamily |
| 38 | + fontWeight |
| 39 | + letterSpacing |
| 40 | + lineHeightPx |
| 41 | + } |
| 42 | + fill { |
| 43 | + r |
| 44 | + g |
| 45 | + b |
| 46 | + a |
| 47 | + } |
| 48 | + ...Rect |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + ${rectFragment} |
| 54 | +` |
| 55 | + |
| 56 | +const pageFragment = gql` |
| 57 | + fragment Page on File { |
| 58 | + lastModified |
| 59 | + pages(name: $pageName) { |
| 60 | + name |
| 61 | + frames { |
| 62 | + name |
| 63 | + ...Rect |
| 64 | + ...ChildrenOfName |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +` |
| 69 | +export const FIGMA_FILE_QUERY = gql` |
| 70 | + query FigmaFileQuery($fileId: ID!, $pageName: String!, $nodeName: String!) { |
| 71 | + file(id: $fileId) { |
| 72 | + ...Page |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + ${pageFragment} |
| 77 | + ${childrenFragment} |
| 78 | + ${rectFragment} |
| 79 | +` |
| 80 | + |
| 81 | +const FIGMA_FILE_SUBSCRIPTION = gql` |
| 82 | + subscription onFigmaFileUpdated( |
| 83 | + $fileId: ID! |
| 84 | + $pageName: String! |
| 85 | + $nodeName: String! |
| 86 | + ) { |
| 87 | + file(id: $fileId) { |
| 88 | + ...Page |
| 89 | + } |
| 90 | + } |
| 91 | +
|
| 92 | + ${pageFragment} |
| 93 | + ${childrenFragment} |
| 94 | + ${rectFragment} |
| 95 | +` |
| 96 | + |
| 97 | +interface IFile { |
| 98 | + fileId: string, |
| 99 | + pageName: string, |
| 100 | + children?: any |
| 101 | +} |
| 102 | + |
| 103 | +export default function File({ fileId, pageName, children }: IFile) { |
| 104 | + return ( |
| 105 | + <Query |
| 106 | + query={FIGMA_FILE_QUERY} |
| 107 | + variables={{ fileId, pageName, nodeName: "" }} |
| 108 | + > |
| 109 | + {({ loading, data, error, subscribeToMore }) => { |
| 110 | + if (error) { |
| 111 | + console.error(error) |
| 112 | + return "Oh no!" |
| 113 | + } else if (loading) { |
| 114 | + return "Loading..." |
| 115 | + } else if (!data) { |
| 116 | + return null |
| 117 | + } |
| 118 | + const subscribeToFileUpdates = () => |
| 119 | + subscribeToMore({ |
| 120 | + document: FIGMA_FILE_SUBSCRIPTION, |
| 121 | + variables: { fileId, pageName, nodeName: "" }, |
| 122 | + updateQuery: (prev, { subscriptionData }) => { |
| 123 | + console.log(subscriptionData) |
| 124 | + if (!subscriptionData.data) return prev |
| 125 | + const newFile = subscriptionData.data |
| 126 | + console.log("Figma file updated!") |
| 127 | + return newFile |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + subscribeToFileUpdates() |
| 132 | + |
| 133 | + return ( |
| 134 | + <FigmaContext.Provider |
| 135 | + value={{ |
| 136 | + fileId, |
| 137 | + pageName |
| 138 | + }} |
| 139 | + > |
| 140 | + {children({ data })} |
| 141 | + </FigmaContext.Provider> |
| 142 | + ) |
| 143 | + }} |
| 144 | + </Query> |
| 145 | + ) |
| 146 | +} |
0 commit comments