Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
queden committed Feb 7, 2022
1 parent d3b99d2 commit 2eca8b3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 71 deletions.
50 changes: 21 additions & 29 deletions browser/src/components/ContributionCard.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<<<<<<< HEAD
import {
Contribution,
PatternToDisplay,
Prompt,
} from "src/types/common/server-api";
=======
import { ClientContribution, PatternToDisplay } from "src/types/common/server-api";
>>>>>>> 3f698bd... WIP markdown support
import { ClientContribution, PatternToDisplay, Prompt } from "src/types/common/server-api";
import dayjs from "dayjs";
import { BlobSingle } from "src/components/BlobSingle";
import "./ContributionCard.css";
Expand Down Expand Up @@ -56,15 +48,33 @@ export function getFullContributionResponse({
);
}

const renderHtml = (resp: string): string | JSX.Element | JSX.Element[] => {
// Remove first p tag to prevent first text going to next line, sanitize html string
// and then convert to JSX element
return parse(
sanitizeHtml(
resp.replace(
/<p[^>]*>|<\/p[^>]*>/,
""
)
)
)
};

export function getContributionCardResponse({
response,
responseHtml,
prompt,
pattern,
}: Contribution) {
}: ClientContribution) {
if (!response) {
return response;
}

const input = renderHtml(
responseHtml || response
);

switch (prompt) {
case Prompt.LooksLike:
case Prompt.WeNeed:
Expand All @@ -74,7 +84,7 @@ export function getContributionCardResponse({
{replaceJSX(PromptDescriptions[prompt], {
[Placeholder]: <b>{getPatternPlaceholder(pattern, prompt)}</b>,
})}{" "}
{response}
{input}
</>
);
// TODO: this doesn't replace with the right case from before.
Expand Down Expand Up @@ -124,24 +134,6 @@ export function ContributionCard({
const { openContributionModal, openContributionId } =
useContext(ModalContext);


const renderHtml = (resp: string): string | JSX.Element | JSX.Element[] => {
// Remove first p tag to prevent first text going to next line, sanitize html string
// and then convert to JSX element
return parse(
sanitizeHtml(
resp.replace(
/<p[^>]*>|<\/p[^>]*>/,
""
)
)
)
};

const input = renderHtml(
responseHtml || response
);

return (
<div
className={
Expand Down
24 changes: 0 additions & 24 deletions browser/src/components/core/Editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@
border-color: red;
}

.modal {
background-color: hsla(0, 0%, 15%, 1);
width:100%;
max-width:576px;
padding:3rem;
position:relative;
border-radius:0.375rem;
z-index: 9999;
}

.linkIcon {
padding-left: 2px;
}
Expand Down Expand Up @@ -112,20 +102,6 @@
padding-right: 6px;
}

.overlay {
background-color: hsla(0,0%,100%,.1);
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;

display:flex;
align-items: center;
justify-content:center;
z-index: 8000;
}

.iconShimmer {
text-align: center;

Expand Down
12 changes: 3 additions & 9 deletions browser/src/components/core/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import sanitizeHtml from "sanitize-html";
import isURL from "validator/lib/isURL";

import { ButtonClass } from "../../types/styles";
import { selectFullWordCommand, unsetWhitespaceMarksCommand } from "./commands";
import { ResponseCharacterLimit } from "../ContributionSection";
import "./Editor.css";

Expand Down Expand Up @@ -92,18 +91,15 @@ export function Editor({

var url = null;
// If link is not null, check if it's valid and display error message otherwise.
if (!(linkInput === "" || linkInput === null || linkInput === undefined)) {
if (!linkInput) {
if (isURL(linkInput)) {
url = linkInput;
} else {
setIsInvalidInput(true);
return;
}
} else {
editor.chain().focus().command(({ tr, commands }) => {
return selectFullWordCommand({ tr, commands })
}).unsetLink().run();

editor.chain().focus().unsetLink().run();
closeModal();
return;
}
Expand All @@ -117,9 +113,7 @@ export function Editor({
}

// Set link.
editor.chain().focus().command(({ tr, commands }) => {
return selectFullWordCommand({ tr, commands })
}).setLink({ href: url }).run();
editor.chain().focus().setLink({ href: url }).run();

closeModal();
};
Expand Down
2 changes: 0 additions & 2 deletions browser/src/components/core/commands.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,13 @@ test("two words and surrounding spaces selected", () => {
});

test("punctuation stuff", () => {
console.log("________________________")
testCommand({
selectedFrom: 47,
selectedTo: 50,
expectedFrom: 49,
expectedTo: 51,
expectedContent: "22",
})
console.log("________________________")
});

test("in the middle of multiple spaces", () => {
Expand Down
18 changes: 12 additions & 6 deletions browser/src/helpers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ export async function getContribution({
});

const mdToHtmlConverter = new Converter();
response.responseHtml = mdToHtmlConverter.makeHtml(
const responseHtml = mdToHtmlConverter.makeHtml(
response.response
);

return response as ClientContribution;
return {
...response,
responseHtml,
} as ClientContribution;
}

export async function getContributions({
Expand All @@ -106,14 +109,17 @@ export async function getContributions({
);

const mdToHtmlConverter = new Converter();
const responseWithHtml = response.map((res) => {
res.responseHtml = mdToHtmlConverter.makeHtml(
const responseWithHtml = response.map((res: ClientContribution) => {
const responseHtml = mdToHtmlConverter.makeHtml(
res.response
);
return res;
return {
...res,
responseHtml,
} as ClientContribution
});

return response as ClientContribution[];
return responseWithHtml as ClientContribution[];
}

export async function getUser({
Expand Down
1 change: 0 additions & 1 deletion browser/src/types/common/server-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export interface AddContributionRequest {
walletId: string;
// This should be the full text response, formatted as markdown.
response: string;
// Full text response formatted as markdown as HTML.
prompt: Prompt;
pattern: Pattern;
}
Expand Down

0 comments on commit 2eca8b3

Please sign in to comment.