Merge pull request #148 from emrysal/feature/disable-integrations-when-invalid-or-missing-configuration
Take server config into account whilst listing integrations, improved…
This commit is contained in:
		
						commit
						e185b57db1
					
				
					 2 changed files with 98 additions and 77 deletions
				
			
		|  | @ -1,5 +1,20 @@ | ||||||
| const withTM = require('next-transpile-modules')(['react-timezone-select']); | const withTM = require('next-transpile-modules')(['react-timezone-select']); | ||||||
| 
 | 
 | ||||||
|  | const validJson = (jsonString) => { | ||||||
|  |     try { | ||||||
|  |         const o = JSON.parse(jsonString); | ||||||
|  |         if (o && typeof o === "object") { | ||||||
|  |             return o; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     catch (e) {} | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | if (process.env.GOOGLE_API_CREDENTIALS && ! validJson(process.env.GOOGLE_API_CREDENTIALS)) { | ||||||
|  |     console.warn('\x1b[33mwarn', '\x1b[0m', "- Disabled 'Google Calendar' integration. Reason: Invalid value for GOOGLE_API_CREDENTIALS environment variable. When set, this value needs to contain valid JSON like {\"web\":{\"client_id\":\"<clid>\",\"client_secret\":\"<secret>\",\"redirect_uris\":[\"<yourhost>/api/integrations/googlecalendar/callback>\"]}. You can download this JSON from your OAuth Client @ https://console.cloud.google.com/apis/credentials."); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| module.exports = withTM({ | module.exports = withTM({ | ||||||
|   future: { |   future: { | ||||||
|     webpack5: true, |     webpack5: true, | ||||||
|  |  | ||||||
|  | @ -7,7 +7,7 @@ import { useSession, getSession } from 'next-auth/client'; | ||||||
| import { CheckCircleIcon, XCircleIcon, ChevronRightIcon, PlusIcon } from '@heroicons/react/solid'; | import { CheckCircleIcon, XCircleIcon, ChevronRightIcon, PlusIcon } from '@heroicons/react/solid'; | ||||||
| import { InformationCircleIcon } from '@heroicons/react/outline'; | import { InformationCircleIcon } from '@heroicons/react/outline'; | ||||||
| 
 | 
 | ||||||
| export default function Home(props) { | export default function Home({ integrations }) { | ||||||
|     const [session, loading] = useSession(); |     const [session, loading] = useSession(); | ||||||
|     const [showAddModal, setShowAddModal] = useState(false); |     const [showAddModal, setShowAddModal] = useState(false); | ||||||
| 
 | 
 | ||||||
|  | @ -24,7 +24,7 @@ export default function Home(props) { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     function integrationHandler(type) { |     function integrationHandler(type) { | ||||||
|         fetch('/api/integrations/' + type + '/add') |         fetch('/api/integrations/' + type.replace('_', '') + '/add') | ||||||
|             .then((response) => response.json()) |             .then((response) => response.json()) | ||||||
|             .then((data) => window.location.href = data.url); |             .then((data) => window.location.href = data.url); | ||||||
|     } |     } | ||||||
|  | @ -38,73 +38,63 @@ export default function Home(props) { | ||||||
| 
 | 
 | ||||||
|             <Shell heading="Integrations"> |             <Shell heading="Integrations"> | ||||||
|                 <div className="bg-white shadow overflow-hidden sm:rounded-lg"> |                 <div className="bg-white shadow overflow-hidden sm:rounded-lg"> | ||||||
|                     <ul className="divide-y divide-gray-200"> |                     {integrations.filter( (ig) => ig.credential ).length !== 0 ? <ul className="divide-y divide-gray-200"> | ||||||
|                         {props.credentials.map((integration) => |                         {integrations.filter(ig => ig.credential).map( (ig) => (<li> | ||||||
|                             <li> |                             <Link href={"/integrations/" + ig.credential.id}> | ||||||
|                                 <Link href={"/integrations/" + integration.id}> |                                 <a className="block hover:bg-gray-50"> | ||||||
|                                     <a className="block hover:bg-gray-50"> |                                     <div className="flex items-center px-4 py-4 sm:px-6"> | ||||||
|                                         <div className="flex items-center px-4 py-4 sm:px-6"> |                                         <div className="min-w-0 flex-1 flex items-center"> | ||||||
|                                             <div className="min-w-0 flex-1 flex items-center"> |                                             <div className="flex-shrink-0"> | ||||||
|                                                 <div className="flex-shrink-0"> |                                                 <img className="h-10 w-10 mr-2" src={ig.imageSrc} alt={ig.title} /> | ||||||
|                                                     {integration.type == 'google_calendar' && <img className="h-10 w-10 mr-2" src="integrations/google-calendar.png" alt="Google Calendar" />} |                                             </div> | ||||||
|                                                     {integration.type == 'office365_calendar' && <img className="h-10 w-10 mr-2" src="integrations/office-365.png" alt="Office 365 / Outlook.com Calendar" />} |                                             <div className="min-w-0 flex-1 px-4 md:grid md:grid-cols-2 md:gap-4"> | ||||||
|  |                                                 <div> | ||||||
|  |                                                     <p className="text-sm font-medium text-blue-600 truncate">{ig.title}</p> | ||||||
|  |                                                     <p className="flex items-center text-sm text-gray-500"> | ||||||
|  |                                                         {ig.type.endsWith('_calendar') && <span className="truncate">Calendar Integration</span>} | ||||||
|  |                                                     </p> | ||||||
|                                                 </div> |                                                 </div> | ||||||
|                                                 <div className="min-w-0 flex-1 px-4 md:grid md:grid-cols-2 md:gap-4"> |                                                 <div className="hidden md:block"> | ||||||
|                                                     <div> |                                                     {ig.credential.key && <p className="mt-2 flex items-center text text-gray-500"> | ||||||
|                                                         {integration.type == 'google_calendar' && <p className="text-sm font-medium text-blue-600 truncate">Google Calendar</p>} |                                                         <CheckCircleIcon className="flex-shrink-0 mr-1.5 h-5 w-5 text-green-400" /> | ||||||
|                                                         {integration.type == 'office365_calendar' && <p className="text-sm font-medium text-blue-600 truncate">Office365 / Outlook.com Calendar</p>} |                                                         Connected | ||||||
|                                                         <p className="flex items-center text-sm text-gray-500"> |                                                     </p>} | ||||||
|                                                             {integration.type.endsWith('_calendar') && <span className="truncate">Calendar Integration</span>} |                                                     {!ig.credential.key && <p className="mt-3 flex items-center text text-gray-500"> | ||||||
|                                                         </p> |                                                         <XCircleIcon className="flex-shrink-0 mr-1.5 h-5 w-5 text-yellow-400" /> | ||||||
|                                                     </div> |                                                         Not connected | ||||||
|                                                     <div className="hidden md:block"> |                                                     </p>} | ||||||
|                                                         <div> |  | ||||||
|                                                             {integration.key && |  | ||||||
|                                                                 <p className="mt-2 flex items-center text text-gray-500"> |  | ||||||
|                                                                     <CheckCircleIcon className="flex-shrink-0 mr-1.5 h-5 w-5 text-green-400" /> |  | ||||||
|                                                                     Connected |  | ||||||
|                                                                 </p> |  | ||||||
|                                                             } |  | ||||||
|                                                             {!integration.key && |  | ||||||
|                                                                 <p className="mt-3 flex items-center text text-gray-500"> |  | ||||||
|                                                                     <XCircleIcon className="flex-shrink-0 mr-1.5 h-5 w-5 text-yellow-400" /> |  | ||||||
|                                                                 Not connected |  | ||||||
|                                                             </p> |  | ||||||
|                                                             } |  | ||||||
|                                                         </div> |  | ||||||
|                                                     </div> |  | ||||||
|                                                 </div> |                                                 </div> | ||||||
|                                             </div> |                                             </div> | ||||||
|                                             <div> |                                             <div> | ||||||
|                                                 <ChevronRightIcon className="h-5 w-5 text-gray-400" /> |                                                 <ChevronRightIcon className="h-5 w-5 text-gray-400" /> | ||||||
|                                             </div> |                                             </div> | ||||||
|                                         </div> |                                         </div> | ||||||
|                                     </a> |                                     </div> | ||||||
|                                 </Link> |                                 </a> | ||||||
|                             </li> |                             </Link> | ||||||
|                         )} |                         </li>))} | ||||||
|                     </ul> |                     </ul> | ||||||
|                     {props.credentials.length == 0 && |                     : | ||||||
|                         <div className="bg-white shadow sm:rounded-lg"> |                     <div className="bg-white shadow sm:rounded-lg"> | ||||||
|                             <div className="flex"> |                         <div className="flex"> | ||||||
|                                 <div className="py-9 pl-8"> |                             <div className="py-9 pl-8"> | ||||||
|                                     <InformationCircleIcon className="text-blue-600 w-16" /> |                                 <InformationCircleIcon className="text-blue-600 w-16" /> | ||||||
|  |                             </div> | ||||||
|  |                             <div className="py-5 sm:p-6"> | ||||||
|  |                                 <h3 className="text-lg leading-6 font-medium text-gray-900"> | ||||||
|  |                                      You don't have any integrations added. | ||||||
|  |                                 </h3> | ||||||
|  |                                 <div className="mt-2 text-sm text-gray-500"> | ||||||
|  |                                     <p> | ||||||
|  |                                         You currently do not have any integrations set up. Add your first integration to get started. | ||||||
|  |                                     </p> | ||||||
|                                 </div> |                                 </div> | ||||||
|                                 <div className="py-5 sm:p-6"> |                                 <div className="mt-3 text-sm"> | ||||||
|                                     <h3 className="text-lg leading-6 font-medium text-gray-900"> |                                     <button onClick={toggleAddModal} className="font-medium text-blue-600 hover:text-blue-500"> Add your first integration <span aria-hidden="true">→</span></button> | ||||||
|                                         You don't have any integrations added. |  | ||||||
|                                     </h3> |  | ||||||
|                                     <div className="mt-2 text-sm text-gray-500"> |  | ||||||
|                                         <p> |  | ||||||
|                                             You currently do not have any integrations set up. Add your first integration to get started. |  | ||||||
|                                         </p> |  | ||||||
|                                     </div> |  | ||||||
|                                     <div className="mt-3 text-sm"> |  | ||||||
|                                         <button onClick={toggleAddModal} className="font-medium text-blue-600 hover:text-blue-500"> Add your first integration <span aria-hidden="true">→</span></button> |  | ||||||
|                                     </div> |  | ||||||
|                                 </div> |                                 </div> | ||||||
|                             </div> |                             </div> | ||||||
|                         </div> |                         </div> | ||||||
|  |                     </div> | ||||||
|                     } |                     } | ||||||
|                 </div> |                 </div> | ||||||
|                 {showAddModal && |                 {showAddModal && | ||||||
|  | @ -150,30 +140,18 @@ export default function Home(props) { | ||||||
|                             </div> |                             </div> | ||||||
|                             <div className="my-4"> |                             <div className="my-4"> | ||||||
|                                 <ul className="divide-y divide-gray-200"> |                                 <ul className="divide-y divide-gray-200"> | ||||||
|                                     <li className="flex py-4"> |                                     {integrations.filter( (integration) => integration.installed ).map( (integration) => (<li className="flex py-4"> | ||||||
|                                         <div className="w-1/12 mr-4 pt-2"> |                                         <div className="w-1/12 mr-4 pt-2"> | ||||||
|                                             <img className="h-8 w-8 mr-2" src="integrations/office-365.png" alt="Office 365 / Outlook.com Calendar" /> |                                             <img className="h-8 w-8 mr-2" src={integration.imageSrc} alt={integration.title} /> | ||||||
|                                         </div> |                                         </div> | ||||||
|                                         <div className="w-10/12"> |                                         <div className="w-10/12"> | ||||||
|                                             <h2 className="text-gray-800 font-medium">Office 365 / Outlook.com Calendar</h2> |                                             <h2 className="text-gray-800 font-medium">{ integration.title }</h2> | ||||||
|                                             <p className="text-gray-400 text-sm">For personal and business accounts</p> |                                             <p className="text-gray-400 text-sm">{ integration.description }</p> | ||||||
|                                         </div> |                                         </div> | ||||||
|                                         <div className="w-2/12 text-right pt-2"> |                                         <div className="w-2/12 text-right pt-2"> | ||||||
|                                             <button onClick={() => integrationHandler('office365calendar')} className="font-medium text-blue-600 hover:text-blue-500">Add</button> |                                             <button onClick={() => integrationHandler(integration.type)} className="font-medium text-blue-600 hover:text-blue-500">Add</button> | ||||||
|                                         </div> |                                         </div> | ||||||
|                                     </li> |                                     </li>))} | ||||||
|                                     <li className="flex py-4"> |  | ||||||
|                                         <div className="w-1/12 mr-4 pt-2"> |  | ||||||
|                                             <img className="h-8 w-8 mr-2" src="integrations/google-calendar.png" alt="Google Calendar" /> |  | ||||||
|                                         </div> |  | ||||||
|                                         <div className="w-10/12"> |  | ||||||
|                                             <h2 className="text-gray-800 font-medium">Google Calendar</h2> |  | ||||||
|                                             <p className="text-gray-400 text-sm">For personal and business accounts</p> |  | ||||||
|                                         </div> |  | ||||||
|                                         <div className="w-2/12 text-right pt-2"> |  | ||||||
|                                             <button onClick={() => integrationHandler('googlecalendar')} className="font-medium text-blue-600 hover:text-blue-500">Add</button> |  | ||||||
|                                         </div> |  | ||||||
|                                     </li> |  | ||||||
|                                 </ul> |                                 </ul> | ||||||
|                             </div> |                             </div> | ||||||
|                             <div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse"> |                             <div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse"> | ||||||
|  | @ -190,6 +168,17 @@ export default function Home(props) { | ||||||
|     ); |     ); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | const validJson = (jsonString: string) => { | ||||||
|  |     try { | ||||||
|  |         const o = JSON.parse(jsonString); | ||||||
|  |         if (o && typeof o === "object") { | ||||||
|  |             return o; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     catch (e) {} | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export async function getServerSideProps(context) { | export async function getServerSideProps(context) { | ||||||
|     const session = await getSession(context); |     const session = await getSession(context); | ||||||
| 
 | 
 | ||||||
|  | @ -212,7 +201,24 @@ export async function getServerSideProps(context) { | ||||||
|             key: true |             key: true | ||||||
|         } |         } | ||||||
|     }); |     }); | ||||||
|  | 
 | ||||||
|  |     const integrations = [ { | ||||||
|  |         installed: !!(process.env.GOOGLE_API_CREDENTIALS && validJson(process.env.GOOGLE_API_CREDENTIALS)), | ||||||
|  |         credential: credentials.find( (integration) => integration.type === "google_calendar" ) || null, | ||||||
|  |         type: "google_calendar", | ||||||
|  |         title: "Google Calendar", | ||||||
|  |         imageSrc: "integrations/google-calendar.png", | ||||||
|  |         description: "For personal and business accounts", | ||||||
|  |     }, { | ||||||
|  |         installed: !!(process.env.MS_GRAPH_CLIENT_ID && process.env.MS_GRAPH_CLIENT_SECRET), | ||||||
|  |         type: "office365_calendar", | ||||||
|  |         credential: credentials.find( (integration) => integration.type === "office365_calendar" ) || null, | ||||||
|  |         title: "Office 365 / Outlook.com Calendar", | ||||||
|  |         imageSrc: "integrations/office-365.png", | ||||||
|  |         description: "For personal and business accounts", | ||||||
|  |     } ]; | ||||||
|  | 
 | ||||||
|     return { |     return { | ||||||
|       props: {credentials}, // will be passed to the page component as props
 |       props: {integrations}, | ||||||
|     } |     } | ||||||
| } | } | ||||||
		Loading…
	
		Reference in a new issue
	
	 Bailey Pumfleet
						Bailey Pumfleet