Skip to content

Commit

Permalink
fix: handle icon rendering and defaults for settings / rules
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Feb 18, 2023
1 parent 7595324 commit c9633d7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
8 changes: 5 additions & 3 deletions src/lib/notion/NotionAPIWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,17 @@ class NotionAPIWrapper {
return '';
}
let title = getObjectTitle(page) ?? `Untitled: ${new Date()}`;
let icon = renderIcon(getBlockIcon(page as WithIcon, settings.pageEmoji));
let icon = await renderIcon(
getBlockIcon(page as WithIcon, settings.pageEmoji)
);
return this.getBlockTitle(icon, title, settings);
}

getDatabaseTitle(
async getDatabaseTitle(
database: GetDatabaseResponse,
settings: Settings
): Promise<string> {
let icon = renderIcon(
let icon = await renderIcon(
getBlockIcon(database as WithIcon, settings.pageEmoji)
);
let title = isFullDatabase(database)
Expand Down
24 changes: 16 additions & 8 deletions src/lib/notion/helpers/renderIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { renderToStaticMarkup } from 'react-dom/server';
import axios from 'axios';

export default function renderIcon(icon?: string) {
export default async function renderIcon(icon?: string) {
if (!icon) {
return null;
}
return renderToStaticMarkup(
icon.startsWith('http') ? (
<img src={icon} width="32" />
) : (
<span className="icon">{icon}</span>
)
);
if (icon.startsWith('http')) {
let validIcon = true;
await axios.get(icon).catch(function (error) {
if (error.response && error.response.status === 404) {
validIcon = false;
}
});
if (!validIcon) {
return null;
}
return renderToStaticMarkup(<img src={icon} width="32" />);
}

return renderToStaticMarkup(<span className="icon">{icon}</span>);
}
5 changes: 3 additions & 2 deletions src/lib/notion/helpers/renderLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { GetBlockResponse } from '@notionhq/client/build/src/api-endpoints';
import ReactDOMServer from 'react-dom/server';
import renderIcon from './renderIcon';

export default function renderLink(
export default async function renderLink(
title: string,
block: GetBlockResponse,
icon?: string
) {
const r = await renderIcon(icon);
return ReactDOMServer.renderToStaticMarkup(
<a id={block.id} href={`https://notion.so/${block.id.replace(/\-/g, '')}`}>
{renderIcon(icon)}
{r}
{title}
</a>
);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/rules/findRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async function findRule(req: Request, res: Response) {
.returning('*')
.first()
.then((result) => {
res.json(result);
res.status(200).json(result);
})
.catch((err) => {
sendError(err);
Expand Down
10 changes: 1 addition & 9 deletions src/routes/settings/findSetting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Request, Response } from 'express';

import DB from '../../lib/storage/db';
import { sendError } from '../../lib/error/sendError';

export default async function findSetting(req: Request, res: Response) {
console.debug(`find settings ${req.params.id}`);
Expand All @@ -14,13 +13,6 @@ export default async function findSetting(req: Request, res: Response) {
const storedSettings = await DB('settings')
.where({ object_id: id })
.returning(['payload'])
.first()
.then((result) => {
res.json(result);
})
.catch((err) => {
sendError(err);
res.status(400).send();
});
.first();
return res.json({ payload: storedSettings });
}

0 comments on commit c9633d7

Please sign in to comment.