Skip to content

Commit

Permalink
Merge pull request #39 from bob2402/rocket-problem-url-parse
Browse files Browse the repository at this point in the history
Problem: merit requests that are only a github are annyoing
  • Loading branch information
gsovereignty authored Jul 24, 2024
2 parents 8f3b069 + 1274b61 commit 0a0cc18
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
14 changes: 8 additions & 6 deletions src/components/MeritRequestDashboard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { base } from '$app/paths';
import * as Breadcrumb from '$lib/components/ui/breadcrumb/index.js';
import type { MeritRequest } from '@/event_helpers/merits';
import { getRocketURL } from '@/helpers';
import { getRocketURL, parseProblem } from '@/helpers';
import type { NDKEvent } from '@nostr-dev-kit/ndk';
import MeritSummaryCard from './MeritSummaryCard.svelte';
Expand All @@ -27,11 +27,13 @@
</Breadcrumb.Item>
<Breadcrumb.Separator />
<Breadcrumb.Item>
<Breadcrumb.Page
>{merit
.Problem()
.substring(0, 16)}{#if merit.Problem().length > 16}...{/if}</Breadcrumb.Page
>
<Breadcrumb.Page>
{#await parseProblem(merit.Problem())}
{merit.Problem().substring(0, 16)}{#if merit.Problem().length > 16}...{/if}
{:then parsed}
{parsed.substring(0, 16)}{#if parsed.length > 16}...{/if}
{/await}
</Breadcrumb.Page>
</Breadcrumb.Item>
</Breadcrumb.List>
</Breadcrumb.Root>
Expand Down
10 changes: 8 additions & 2 deletions src/components/MeritRequests.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as Table from '@/components/ui/table';
import { MapOfMeritResult, MeritRequest } from '@/event_helpers/merits';
import { Rocket, RocketATagFilter } from '@/event_helpers/rockets';
import { unixToRelativeTime } from '@/helpers';
import { parseProblem, unixToRelativeTime } from '@/helpers';
import { ndk } from '@/ndk';
import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk';
import { Avatar, Name } from '@nostr-dev-kit/ndk-svelte-components';
Expand Down Expand Up @@ -114,7 +114,13 @@
/>
</div>
</Table.Cell>
<Table.Cell class="hidden text-left md:table-cell">{merit.Problem()}</Table.Cell>
<Table.Cell class="hidden text-left md:table-cell">
{#await parseProblem(merit.Problem())}
{merit.Problem()}
{:then parsed}
{parsed}
{/await}
</Table.Cell>
<Table.Cell class="table-cell">{merit.Sats}</Table.Cell>
<Table.Cell class="table-cell">{merit.Merits}</Table.Cell>
<Table.Cell class="hidden text-right md:table-cell"
Expand Down
19 changes: 14 additions & 5 deletions src/components/MeritSummaryCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
import { Separator } from '$lib/components/ui/separator/index.js';
import * as Table from '@/components/ui/table';
import { Rocket, RocketATagFilter } from '@/event_helpers/rockets';
import { formatReferenceTime, getCuckPrice, getRocketURL, unixToRelativeTime } from '@/helpers';
import {
formatReferenceTime,
getCuckPrice,
getRocketURL,
parseProblem,
unixToRelativeTime
} from '@/helpers';
import { derived } from 'svelte/store';
import { goto } from '$app/navigation';
Expand Down Expand Up @@ -116,7 +122,13 @@
<Card.Root class="sm:col-span-2 {border}">
<Card.Header class="pb-3">
<div class="flex flex-nowrap justify-between">
<Card.Title>Problem: {merit.Problem().substring(0, 20)}</Card.Title>{#if merit.Solution()}<a
<Card.Title>
{#await parseProblem(merit.Problem())}
Problem: {merit.Problem().substring(0, 20)}
{:then parsed}
{parsed}
{/await}
</Card.Title>{#if merit.Solution()}<a
class="flex flex-nowrap text-orange-500 underline decoration-orange-500"
href={merit.Solution()}>View Solution <ExternalLink size={18} class="m-1" /></a
>{/if}
Expand All @@ -129,9 +141,6 @@
/>
<Name ndk={$ndk} pubkey={merit.Pubkey} class="inline-block max-w-32 truncate p-2" />
</div>
<Card.Description class="max-w-lg text-balance leading-relaxed">
{#if merit.Problem().length > 20}{merit.Problem()}{/if}
</Card.Description>
<Card.Content class="p-6 text-sm">
<div class="grid gap-3">
<div class="font-semibold">Merit Request Details</div>
Expand Down
53 changes: 53 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,56 @@ export async function getCuckPrice(): Promise<number | Error> {
return e as Error;
}
}

export async function parseProblem(problem: string) {
try {
if (isGitHubIssueUrl(problem)) {
const apiURL = convertToGitHubApiUrl(problem);
if (!apiURL) {
return problem;
}
const response = await fetch(apiURL);
const json = await response.json();
return json.title;
} else {
return problem;
}
} catch (error) {
console.error('Get title error:', error);
return problem;
}
}

function isGitHubIssueUrl(url: string): boolean {
try {
const parsedUrl: URL = new URL(url);
if (parsedUrl.hostname !== 'github.com') {
return false;
}
const pathParts: string[] = parsedUrl.pathname.split('/').filter((part) => part !== '');
if (pathParts.length !== 4 || pathParts[2] !== 'issues' || !/^[1-9]\d*$/.test(pathParts[3])) {
return false;
}
return true;
} catch (error) {
return false;
}
}

function convertToGitHubApiUrl(issueUrl: URL | string): URL | null {
try {
const url = new URL(issueUrl);
if (url.hostname !== 'github.com') {
throw new Error('Not a valid GitHub URL');
}
const pathParts = url.pathname.split('/').filter((part) => part !== '');
if (pathParts.length !== 4 || pathParts[2] !== 'issues') {
throw new Error('Not a valid GitHub issue URL');
}
const [owner, repo, , issueNumber] = pathParts;
return new URL(`https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}`);
} catch (error) {
console.error('URL conversion error:', error);
return null;
}
}

0 comments on commit 0a0cc18

Please sign in to comment.