Skip to content

Commit

Permalink
feat: add CreatedAt coloumn to PR view and Issue view (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
liyujun-dev committed Jan 24, 2025
1 parent 93a06fc commit 1d83f05
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 4 deletions.
9 changes: 8 additions & 1 deletion config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type ColumnConfig struct {

type PrsLayoutConfig struct {
UpdatedAt ColumnConfig `yaml:"updatedAt,omitempty"`
CreatedAt ColumnConfig `yaml:"createdAt,omitempty"`
Repo ColumnConfig `yaml:"repo,omitempty"`
Author ColumnConfig `yaml:"author,omitempty"`
Assignees ColumnConfig `yaml:"assignees,omitempty"`
Expand All @@ -81,6 +82,7 @@ type PrsLayoutConfig struct {

type IssuesLayoutConfig struct {
UpdatedAt ColumnConfig `yaml:"updatedAt,omitempty"`
CreatedAt ColumnConfig `yaml:"createdAt,omitempty"`
State ColumnConfig `yaml:"state,omitempty"`
Repo ColumnConfig `yaml:"repo,omitempty"`
Title ColumnConfig `yaml:"title,omitempty"`
Expand Down Expand Up @@ -222,6 +224,9 @@ func (parser ConfigParser) getDefaultConfig() Config {
UpdatedAt: ColumnConfig{
Width: utils.IntPtr(lipgloss.Width("2mo ")),
},
CreatedAt: ColumnConfig{
Width: utils.IntPtr(lipgloss.Width("2mo ")),
},
Repo: ColumnConfig{
Width: utils.IntPtr(20),
},
Expand All @@ -244,6 +249,9 @@ func (parser ConfigParser) getDefaultConfig() Config {
UpdatedAt: ColumnConfig{
Width: utils.IntPtr(lipgloss.Width("2mo ")),
},
CreatedAt: ColumnConfig{
Width: utils.IntPtr(lipgloss.Width("2mo ")),
},
Repo: ColumnConfig{
Width: utils.IntPtr(15),
},
Expand Down Expand Up @@ -337,7 +345,6 @@ func (parser ConfigParser) writeDefaultConfigContents(
newConfigFile *os.File,
) error {
_, err := newConfigFile.WriteString(parser.getDefaultConfigYamlContents())

if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions data/issueapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type IssueData struct {
Login string
}
UpdatedAt time.Time
CreatedAt time.Time
Url string
Repository Repository
Assignees Assignees `graphql:"assignees(first: 3)"`
Expand Down Expand Up @@ -72,6 +73,10 @@ func (data IssueData) GetUpdatedAt() time.Time {
return data.UpdatedAt
}

func (data IssueData) GetCreatedAt() time.Time {
return data.CreatedAt
}

func makeIssuesQuery(query string) string {
return fmt.Sprintf("is:issue %s sort:updated", query)
}
Expand Down
5 changes: 5 additions & 0 deletions data/prapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type PullRequestData struct {
Login string
}
UpdatedAt time.Time
CreatedAt time.Time
Url string
State string
Mergeable string
Expand Down Expand Up @@ -185,6 +186,10 @@ func (data PullRequestData) GetUpdatedAt() time.Time {
return data.UpdatedAt
}

func (data PullRequestData) GetCreatedAt() time.Time {
return data.CreatedAt
}

func makePullRequestsQuery(query string) string {
return fmt.Sprintf("is:pr %s sort:updated", query)
}
Expand Down
2 changes: 2 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Repo struct {
type Branch struct {
Name string
LastUpdatedAt *time.Time
CreatedAt *time.Time
LastCommitMsg *string
CommitsAhead int
CommitsBehind int
Expand Down Expand Up @@ -103,6 +104,7 @@ func GetRepo(dir string) (*Repo, error) {
branches[i] = Branch{
Name: b,
LastUpdatedAt: updatedAt,
CreatedAt: updatedAt,
IsCheckedOut: isHead,
Remotes: remotes,
LastCommitMsg: lastCommitMsg,
Expand Down
14 changes: 14 additions & 0 deletions ui/components/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (issue *Issue) ToTableRow() table.Row {
issue.renderNumComments(),
issue.renderNumReactions(),
issue.renderUpdateAt(),
issue.renderCreatedAt(),
}
}

Expand All @@ -48,6 +49,19 @@ func (issue *Issue) renderUpdateAt() string {
return issue.getTextStyle().Render(updatedAtOutput)
}

func (issue *Issue) renderCreatedAt() string {
timeFormat := issue.Ctx.Config.Defaults.DateFormat

createdAtOutput := ""
if timeFormat == "" || timeFormat == "relative" {
createdAtOutput = utils.TimeElapsed(issue.Data.CreatedAt)
} else {
createdAtOutput = issue.Data.CreatedAt.Format(timeFormat)
}

return issue.getTextStyle().Render(createdAtOutput)
}

func (issue *Issue) renderRepoName() string {
repoName := issue.Data.Repository.Name
return issue.getTextStyle().Render(repoName)
Expand Down
14 changes: 13 additions & 1 deletion ui/components/issuessection/issuessection.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewModel(
ctx *context.ProgramContext,
cfg config.IssuesSectionConfig,
lastUpdated time.Time,
createdAt time.Time,
) Model {
m := Model{}
m.BaseModel = section.NewModel(
Expand All @@ -40,6 +41,7 @@ func NewModel(
Singular: m.GetItemSingularForm(),
Plural: m.GetItemPluralForm(),
LastUpdated: lastUpdated,
CreatedAt: createdAt,
},
)
m.Issues = []data.IssueData{}
Expand Down Expand Up @@ -166,6 +168,10 @@ func GetSectionColumns(
dLayout.UpdatedAt,
sLayout.UpdatedAt,
)
createdAtLayout := config.MergeColumnConfigs(
dLayout.CreatedAt,
sLayout.CreatedAt,
)
stateLayout := config.MergeColumnConfigs(dLayout.State, sLayout.State)
repoLayout := config.MergeColumnConfigs(dLayout.Repo, sLayout.Repo)
titleLayout := config.MergeColumnConfigs(dLayout.Title, sLayout.Title)
Expand Down Expand Up @@ -220,10 +226,15 @@ func GetSectionColumns(
Hidden: reactionsLayout.Hidden,
},
{
Title: "",
Title: "󱦻",
Width: updatedAtLayout.Width,
Hidden: updatedAtLayout.Hidden,
},
{
Title: "󱡢",
Width: createdAtLayout.Width,
Hidden: createdAtLayout.Hidden,
},
}
}

Expand Down Expand Up @@ -336,6 +347,7 @@ func FetchAllSections(
ctx,
sectionConfig,
time.Now(),
time.Now(),
) // 0 is the search section
sections = append(sections, &sectionModel)
fetchIssuesCmds = append(
Expand Down
3 changes: 3 additions & 0 deletions ui/components/listviewport/listviewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ type Model struct {
NumCurrentItems int
NumTotalItems int
LastUpdated time.Time
CreatedAt time.Time
ItemTypeLabel string
}

func NewModel(
ctx context.ProgramContext,
dimensions constants.Dimensions,
lastUpdated time.Time,
createdAt time.Time,
itemTypeLabel string,
numItems, listItemHeight int,
) Model {
Expand All @@ -43,6 +45,7 @@ func NewModel(
topBoundId: 0,
ItemTypeLabel: itemTypeLabel,
LastUpdated: lastUpdated,
CreatedAt: createdAt,
}
model.bottomBoundId = utils.Min(
model.NumCurrentItems-1,
Expand Down
24 changes: 24 additions & 0 deletions ui/components/pr/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,28 @@ func (pr *PullRequest) renderUpdateAt() string {
return pr.getTextStyle().Foreground(pr.Ctx.Theme.FaintText).Render(updatedAtOutput)
}

func (pr *PullRequest) renderCreatedAt() string {
timeFormat := pr.Ctx.Config.Defaults.DateFormat

createdAtOutput := ""
t := pr.Branch.CreatedAt
if pr.Data != nil {
t = &pr.Data.CreatedAt
}

if t == nil {
return ""
}

if timeFormat == "" || timeFormat == "relative" {
createdAtOutput = utils.TimeElapsed(*t)
} else {
createdAtOutput = t.Format(timeFormat)
}

return pr.getTextStyle().Foreground(pr.Ctx.Theme.FaintText).Render(createdAtOutput)
}

func (pr *PullRequest) renderBaseName() string {
if pr.Data == nil {
return ""
Expand Down Expand Up @@ -307,6 +329,7 @@ func (pr *PullRequest) ToTableRow(isSelected bool) table.Row {
pr.renderCiStatus(),
pr.renderLines(isSelected),
pr.renderUpdateAt(),
pr.renderCreatedAt(),
}
}

Expand All @@ -321,6 +344,7 @@ func (pr *PullRequest) ToTableRow(isSelected bool) table.Row {
pr.renderCiStatus(),
pr.renderLines(isSelected),
pr.renderUpdateAt(),
pr.renderCreatedAt(),
}
}

Expand Down
21 changes: 19 additions & 2 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewModel(
ctx *context.ProgramContext,
cfg config.PrsSectionConfig,
lastUpdated time.Time,
createdAt time.Time,
) Model {
m := Model{}
m.BaseModel = section.NewModel(
Expand All @@ -43,6 +44,7 @@ func NewModel(
Singular: m.GetItemSingularForm(),
Plural: m.GetItemPluralForm(),
LastUpdated: lastUpdated,
CreatedAt: createdAt,
},
)
m.Prs = []data.PullRequestData{}
Expand Down Expand Up @@ -203,6 +205,10 @@ func GetSectionColumns(
dLayout.UpdatedAt,
sLayout.UpdatedAt,
)
createdAtLayout := config.MergeColumnConfigs(
dLayout.CreatedAt,
sLayout.CreatedAt,
)
repoLayout := config.MergeColumnConfigs(dLayout.Repo, sLayout.Repo)
titleLayout := config.MergeColumnConfigs(dLayout.Title, sLayout.Title)
authorLayout := config.MergeColumnConfigs(dLayout.Author, sLayout.Author)
Expand Down Expand Up @@ -258,10 +264,15 @@ func GetSectionColumns(
Hidden: linesLayout.Hidden,
},
{
Title: "",
Title: "󱦻",
Width: updatedAtLayout.Width,
Hidden: updatedAtLayout.Hidden,
},
{
Title: "󱡢",
Width: createdAtLayout.Width,
Hidden: createdAtLayout.Hidden,
},
}
}

Expand Down Expand Up @@ -313,10 +324,15 @@ func GetSectionColumns(
Hidden: linesLayout.Hidden,
},
{
Title: "",
Title: "󱦻",
Width: updatedAtLayout.Width,
Hidden: updatedAtLayout.Hidden,
},
{
Title: "󱡢",
Width: createdAtLayout.Width,
Hidden: createdAtLayout.Hidden,
},
}
}

Expand Down Expand Up @@ -444,6 +460,7 @@ func FetchAllSections(
ctx,
sectionConfig,
time.Now(),
time.Now(),
)
if len(prs) > 0 && len(prs) >= i+1 && prs[i+1] != nil {
oldSection := prs[i+1].(*Model)
Expand Down
6 changes: 6 additions & 0 deletions ui/components/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type NewSectionOptions struct {
Singular string
Plural string
LastUpdated time.Time
CreatedAt time.Time
}

func NewModel(
Expand Down Expand Up @@ -78,6 +79,7 @@ func NewModel(
*ctx,
m.GetDimensions(),
options.LastUpdated,
options.CreatedAt,
m.Columns,
nil,
m.SingularForm,
Expand Down Expand Up @@ -323,6 +325,10 @@ func (m *BaseModel) LastUpdated() time.Time {
return m.Table.LastUpdated()
}

func (m *BaseModel) CreatedAt() time.Time {
return m.Table.CreatedAt()
}

func (m *BaseModel) UpdateTotalItemsCount(count int) {
m.Table.UpdateTotalItemsCount(count)
}
Expand Down
6 changes: 6 additions & 0 deletions ui/components/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewModel(
ctx context.ProgramContext,
dimensions constants.Dimensions,
lastUpdated time.Time,
createdAt time.Time,
columns []Column,
rows []Row,
itemTypeLabel string,
Expand Down Expand Up @@ -72,6 +73,7 @@ func NewModel(
ctx,
dimensions,
lastUpdated,
createdAt,
itemTypeLabel,
len(rows),
itemHeight,
Expand Down Expand Up @@ -322,6 +324,10 @@ func (m *Model) LastUpdated() time.Time {
return m.rowsViewport.LastUpdated
}

func (m *Model) CreatedAt() time.Time {
return m.rowsViewport.CreatedAt
}

func (m *Model) UpdateLastUpdated(t time.Time) {
m.rowsViewport.LastUpdated = t
}
Expand Down
2 changes: 2 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ func (m *Model) setCurrentViewSections(newSections []section.Section) {
Filters: "archived:false",
},
time.Now(),
time.Now(),
)
m.prs = append([]section.Section{&search}, newSections...)
} else {
Expand All @@ -809,6 +810,7 @@ func (m *Model) setCurrentViewSections(newSections []section.Section) {
Filters: "",
},
time.Now(),
time.Now(),
)
m.issues = append([]section.Section{&search}, newSections...)
}
Expand Down

0 comments on commit 1d83f05

Please sign in to comment.