Skip to content

Commit

Permalink
Some web code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebroberts committed May 6, 2024
1 parent e036840 commit e36f848
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 272 deletions.
5 changes: 5 additions & 0 deletions src/app/web/domainComponents/genericComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { a } from '../hiccough/hiccoughElements'

export function githubAnchor(target: string) {
return a(target, `<i class='bi bi-github' style='color: #6e5494'></i>`)
}
60 changes: 60 additions & 0 deletions src/app/web/domainComponents/pushComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Clock, displayDateTime } from '../../util/dateAndTime'
import { GithubPush } from '../../domain/types/GithubPush'
import { td, tr } from '../hiccough/hiccoughElements'
import { latestCommitInPush } from '../../domain/github/githubPush'
import { commitCell, githubRepoUrl, repoCell } from './repoElementComponents'
import { userCell } from './userComponents'
import { GithubRepositoryElement } from '../../domain/types/GithubRepositoryElement'
import { inlineChildren, withOptions } from '../hiccough/hiccoughElement'
import { githubAnchor } from './genericComponents'

export type PushRowOptions = {
showDescriptionCell?: boolean
showRepoCell?: boolean
}

export function pushRow(clock: Clock, push: GithubPush, options?: PushRowOptions) {
const { showDescriptionCell, showRepoCell } = {
showDescriptionCell: false,
showRepoCell: false,
...options
}

const { dateTime }: { dateTime: string } = push
return tr(
{ class: 'info' },
showDescriptionCell ? pushDescriptionCell : undefined,
showRepoCell ? repoCellForPush(push) : undefined,
branchCell(push),
td(displayDateTime(clock, dateTime)),
userCell(push.actor),
commitCellForPush(push)
)
}

const pushDescriptionCell = td('Push')

export function repoCellForPush(push: GithubPush) {
return repoCell({ ...push, repoHtmlUrl: githubRepoUrl(push) })
}

function branchCell(push: GithubRepositoryElement & Pick<GithubPush, 'ref'>) {
return withOptions(
inlineChildren,
td(
push.ref.split('/')[2],
`&nbsp;&nbsp;`,
githubAnchor(`${githubRepoUrl(push)}/tree/${push.ref.split('/')[2]}`)
)
)
}

function commitCellForPush(push: GithubPush) {
// Use the message of the **last** commit
const commit = latestCommitInPush(push)
return commitCell({
...push,
message: commit.message,
sha: commit.sha
})
}
43 changes: 43 additions & 0 deletions src/app/web/domainComponents/repoElementComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { GithubRepositoryElement } from '../../domain/types/GithubRepositoryElement'
import { inlineChildren, withOptions } from '../hiccough/hiccoughElement'
import { a, td } from '../hiccough/hiccoughElements'
import { githubAnchor } from './genericComponents'

export function repoCell({
ownerId,
repoId,
repoName,
repoHtmlUrl
}: GithubRepositoryElement & {
repoHtmlUrl: string
}) {
return withOptions(
inlineChildren,
td(a(`/app/account/${ownerId}/repo/${repoId}`, repoName), '&nbsp;&nbsp;', githubAnchor(repoHtmlUrl))
)
}

export function githubRepoUrl({
ownerName,
repoName
}: Pick<GithubRepositoryElement, 'ownerName' | 'repoName'>) {
return `https://github.com/${ownerName}/${repoName}`
}

export function commitCell(
event: GithubRepositoryElement & {
message: string
repoHtmlUrl?: string
sha: string
}
) {
const { repoHtmlUrl, message, sha } = event
return withOptions(
inlineChildren,
td(
message.length < 40 ? message : `${message.substring(0, 40)}...`,
'&nbsp;&nbsp;',
githubAnchor(`${repoHtmlUrl ?? githubRepoUrl(event)}/commit/${sha}`)
)
)
}
12 changes: 12 additions & 0 deletions src/app/web/domainComponents/userComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { td } from '../hiccough/hiccoughElements'
import { inlineChildren, withOptions } from '../hiccough/hiccoughElement'
import { githubAnchor } from './genericComponents'

export function userCell(actor?: { login: string }) {
return actor === undefined
? td()
: withOptions(
inlineChildren,
td(actor.login, `&nbsp;&nbsp;`, githubAnchor(`https://github.com/${actor.login}`))
)
}
92 changes: 92 additions & 0 deletions src/app/web/domainComponents/workflowComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { GithubWorkflowRunEvent } from '../../domain/types/GithubWorkflowRunEvent'
import { GithubRepositoryElement } from '../../domain/types/GithubRepositoryElement'
import { inlineChildren, withOptions } from '../hiccough/hiccoughElement'
import { a, td, tr } from '../hiccough/hiccoughElements'
import { Clock, displayDateTime } from '../../util/dateAndTime'
import { githubAnchor } from './genericComponents'
import { commitCell, githubRepoUrl, repoCell } from './repoElementComponents'
import { runWasSuccessful } from '../../domain/github/githubWorkflowRunEvent'
import { userCell } from './userComponents'

export type WorkflowRowOptions = {
showDescriptionCell?: boolean
showRepoCell?: boolean
showWorkflowCell?: boolean
}

export function workflowRow(
clock: Clock,
workflowRunEvent: GithubWorkflowRunEvent,
options?: WorkflowRowOptions
) {
const { showDescriptionCell, showRepoCell, showWorkflowCell } = {
showDescriptionCell: false,
showRepoCell: false,
showWorkflowCell: false,
...options
}

return tr(
{ class: workflowRunClass(workflowRunEvent) },
showDescriptionCell ? descriptionCell(workflowRunEvent) : undefined,
showRepoCell ? repoCell(workflowRunEvent) : undefined,
showWorkflowCell ? workflowCell(workflowRunEvent) : undefined,
showDescriptionCell ? undefined : workflowResultCell(workflowRunEvent),
workflowRunCell(clock, workflowRunEvent),
userCell(workflowRunEvent.actor),
commitCellForWorkflowRunEvent(workflowRunEvent)
)
}

export function workflowRunClass(event: GithubWorkflowRunEvent) {
// TOEventually - handle in progress
return runWasSuccessful(event) ? 'success' : 'danger'
}

export function descriptionCell(event: GithubWorkflowRunEvent) {
return runWasSuccessful(event) ? successfulRunDescriptionCell : failedRunDescriptionCell
}

const successfulRunDescriptionCell = td('Successful Run')
const failedRunDescriptionCell = td('Failed Run')

export function workflowCell(
event: GithubRepositoryElement &
Pick<GithubWorkflowRunEvent, 'workflowHtmlUrl' | 'workflowId' | 'workflowName' | 'path'>
) {
const workflowPath = `${event.path.substring(event.path.indexOf('/') + 1)}`
return withOptions(
inlineChildren,
td(
a(
`/app/account/${event.ownerId}/repo/${event.repoId}/workflow/${event.workflowId}`,
event.workflowName ?? workflowPath
),
'&nbsp;&nbsp;',
githubAnchor(event.workflowHtmlUrl ?? `${githubRepoUrl(event)}/actions/${workflowPath}`)
)
)
}

export function workflowResultCell(event: GithubWorkflowRunEvent) {
// TOEventually - handle in progress
return runWasSuccessful(event) ? successfulRunResultCell : failedRunResultCell
}

const successfulRunResultCell = td('Success')
const failedRunResultCell = td('Failed')

export function workflowRunCell(clock: Clock, event: GithubWorkflowRunEvent) {
return withOptions(
inlineChildren,
td(displayDateTime(clock, event.updatedAt), '&nbsp;&nbsp;', githubAnchor(event.htmlUrl))
)
}

export function commitCellForWorkflowRunEvent(event: GithubWorkflowRunEvent) {
return commitCell({
...event,
sha: event.headSha,
message: event.displayTitle
})
}
155 changes: 0 additions & 155 deletions src/app/web/views/pageElements.ts

This file was deleted.

Loading

0 comments on commit e36f848

Please sign in to comment.