|
1 | | -import React, { useMemo } from "react"; |
| 1 | +import React, { useEffect, useMemo, useState } from "react"; |
2 | 2 | import { HiExclamationCircle, HiXCircle } from "react-icons/hi"; |
3 | 3 | import clsx from "clsx"; |
4 | 4 | import { sentenceCase } from "sentence-case"; |
@@ -45,6 +45,17 @@ const Explain: React.FC<Props> = ({ |
45 | 45 | } |
46 | 46 | }, [spec, simpleTokens]); |
47 | 47 |
|
| 48 | + const [ctaContribToAutocompleteVisible, setCtaContribToAutocompleteVisible] = |
| 49 | + useState( |
| 50 | + () => localStorage.getItem("cta/contributeToAutocomplete") !== "off" |
| 51 | + ); |
| 52 | + useEffect(() => { |
| 53 | + localStorage.setItem( |
| 54 | + "cta/contributeToAutocomplete", |
| 55 | + ctaContribToAutocompleteVisible ? "on" : "off" |
| 56 | + ); |
| 57 | + }, [ctaContribToAutocompleteVisible]); |
| 58 | + |
48 | 59 | if (!cmd || !spec || !parsed) |
49 | 60 | return ( |
50 | 61 | <div className="rounded-md bg-yellow-50 p-4"> |
@@ -101,49 +112,61 @@ const Explain: React.FC<Props> = ({ |
101 | 112 | ); |
102 | 113 |
|
103 | 114 | return ( |
104 | | - <div className="flex flex-col items-stretch space-y-4 select-none"> |
| 115 | + <> |
105 | 116 | {/* Results */} |
106 | | - {parsed.map((token) => ( |
107 | | - <div |
108 | | - key={token.indices[0]} |
109 | | - className={clsx( |
110 | | - "bg-white text-gray-900 dark:bg-black dark:text-white shadow-md rounded p-4 border-2 border-transparent transition", |
111 | | - selection[0] !== null && |
112 | | - selection[0] >= token.indices[0] && |
113 | | - selection[0] <= token.indices[1] && |
114 | | - "border-teal-400 opacity-100" |
115 | | - )} |
116 | | - onMouseEnter={() => |
117 | | - onSelectionChange([token.indices[0], token.indices[1]]) |
118 | | - } |
119 | | - onClick={() => |
120 | | - onSelectionChange([token.indices[0], token.indices[1]]) |
121 | | - } |
122 | | - > |
123 | | - <div className="font-mono font-bold"> |
124 | | - <span> |
125 | | - {token.value} |
126 | | - {token.displayName ? ` - ${token.displayName}` : ""} |
127 | | - </span> |
| 117 | + <div className="flex flex-col items-stretch space-y-4 select-none"> |
| 118 | + {parsed.map((token) => ( |
| 119 | + <div |
| 120 | + key={token.indices[0]} |
| 121 | + className={clsx( |
| 122 | + "bg-white text-gray-900 dark:bg-black dark:text-white shadow-md rounded p-4 border-2 border-transparent transition", |
| 123 | + selection[0] !== null && |
| 124 | + selection[0] >= token.indices[0] && |
| 125 | + selection[0] <= token.indices[1] && |
| 126 | + "border-teal-400 opacity-100" |
| 127 | + )} |
| 128 | + onMouseEnter={() => |
| 129 | + onSelectionChange([token.indices[0], token.indices[1]]) |
| 130 | + } |
| 131 | + onClick={() => |
| 132 | + onSelectionChange([token.indices[0], token.indices[1]]) |
| 133 | + } |
| 134 | + > |
| 135 | + <div className="font-mono font-bold"> |
| 136 | + <span> |
| 137 | + {token.value} |
| 138 | + {token.displayName ? ` - ${token.displayName}` : ""} |
| 139 | + </span> |
| 140 | + </div> |
| 141 | + <div>{token.description || sentenceCase(token.type)}</div> |
128 | 142 | </div> |
129 | | - <div>{token.description || sentenceCase(token.type)}</div> |
130 | | - </div> |
131 | | - ))} |
132 | | - {/* CTA to contribute */} |
133 | | - <div className="bg-white text-gray-500 dark:bg-black dark:text-gray-400 shadow-md rounded p-4 border-2 border-transparent transition"> |
134 | | - Want to make the descriptions better? They{"'"}re open-source! |
135 | | - Contribute at{" "} |
136 | | - <a |
137 | | - href="https://github.com/withfig/autocomplete" |
138 | | - target="_blank" |
139 | | - rel="noopener noreferrer" |
140 | | - className="hover:text-gray-900 dark:hover:text-gray-100" |
141 | | - > |
142 | | - withfig/autocomplete on Github |
143 | | - </a> |
144 | | - . |
| 143 | + ))} |
145 | 144 | </div> |
146 | | - </div> |
| 145 | + {/* CTA to contribute */} |
| 146 | + {ctaContribToAutocompleteVisible && ( |
| 147 | + <div className="mt-6 text-xs bg-white text-gray-500 dark:bg-black dark:text-gray-400 rounded-full transition flex flex-col items-center"> |
| 148 | + <div className="text-center leading-relaxed"> |
| 149 | + Could the descriptions be better? <br className="sm:hidden" /> |
| 150 | + Contribute at{" "} |
| 151 | + <a |
| 152 | + href="https://github.com/withfig/autocomplete" |
| 153 | + target="_blank" |
| 154 | + rel="noopener noreferrer" |
| 155 | + className="hover:text-gray-900 dark:hover:text-gray-100" |
| 156 | + > |
| 157 | + withfig/autocomplete on Github |
| 158 | + </a> |
| 159 | + . |
| 160 | + </div> |
| 161 | + <button |
| 162 | + className="mt-1 sm:mt-0.5 font-medium text-gray-400 hover:text-gray-600" |
| 163 | + onClick={() => setCtaContribToAutocompleteVisible(false)} |
| 164 | + > |
| 165 | + Dismiss |
| 166 | + </button> |
| 167 | + </div> |
| 168 | + )} |
| 169 | + </> |
147 | 170 | ); |
148 | 171 | }; |
149 | 172 |
|
|
0 commit comments