summaryrefslogtreecommitdiff
path: root/Pages/TagDefinitions.razor
blob: 58e5ac7428315a904ab3dc5e6db0ed1c1505ed07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
@page "/TagDefinitions"
@*
 * @inject IDbContextFactory<HBContext> dbFactory
 * @inject ITagService tagService
 * @inject IUserService userService
 * @implements IDisposable
 *@
@inject HBSession hb

<PageTitle>Tag Definitions</PageTitle>

<div style="padding:var(--size-default-gap);">
    <ButtonContainer>
        <Button
            BackgroundColor=Color.ButtonPrimary
            ShortcutKey=@('c')
            OnClick=@(async () => PromptTagCreate())>

				<u>C</u>reate
            </Button>
    </ButtonContainer>

    <TabContainer @ref=tabContainer>
        @foreach(var ns in tagNamespaces) {
            <TabPane Title="@(ns ?? "Default")">
                <table id="tag-definitions" class="data-table">
                    <tr>
                        <th>Alias</th>
                        <th>Name</th>
                        <th>Implicit Tags</th>
                        <th></th>
                    </tr>
                    @foreach(var tagDef in tagDefinitions.Where(td => td.Namespace == ns)) {
                        <tr data-guid="@tagDef.TagDefinitionId">
                            <td>@tagDef.Alias</td>
                            <td>
                                <a href="/Gallery?t=@tagDef.TagDefinitionId" class="nondecorated">
                                    @tagDef.Name
                                </a>
                            </td>
                            <td>
                                <i>
                                    @{
                                        var implicitTags = tagDefinitions
                                            .IntersectBy(tagDef.ImplicitTags, td => td.TagDefinitionId)
                                            .OrderBy(td => td.Name);
                                            // .Where(td => td.Source == TagSource.UserTag);
                                        foreach(var tag in implicitTags) {
                                            <a href="/Gallery?t=@tag.TagDefinitionId" class="nondecorated">
                                                @tag.Name
                                            </a>
                                            if(tag != implicitTags.Last())
                                                @(", ")
                                        }
                                    }
                            </i>
                            </td>
                            <td class="actions">
                                <a href="javascript:;" @onclick=@(() => tagEditDialog.Show(tagDef))>Edit</a>
                                <a href="javascript:;" @onclick=@(() => PromptToDelete(tagDef))>
                                    Delete
                                </a>
                                <a href="javascript:;" @onclick=@(() => PromptImplicitTags(tagDef))>
                                    Implicit Tags
                                </a>
@*
                                @if(tagDef.ImplicitTags.Select(td => td.Guid).Contains(HBContext.NsfwTag)) {
                                    <a href="javascript:;" @onclick=@(() => SetNsfw(tagDef, false))>Make SFW</a>
                                } else {
                                    <a href="javascript:;" @onclick=@(() => SetNsfw(tagDef, true))>Make NSFW</a>
                                }
*@
                            </td>
                        </tr>
                    }
                </table>
            </TabPane>
        }
    </TabContainer>
</div>

<Dialog Title="Are you sure you want to delete this tag definition?" @ref=deleteTagDialog>
    <ButtonContainer>
        <Button OnClick=@(async () => deleteTagDialog.Hide())>Cancel</Button>
        <Button
            BackgroundColor=Color.ButtonWarning
            OnClick=@(async () => DeleteTagDefinition())>

            Confirm
        </Button>
    </ButtonContainer>
</Dialog>

<TagEditDialog OnTagUpdate=TagUpdated @ref=tagEditDialog/>

<TagSelectDialog
    OnSubmit=SetImplicitTags
    @ref=implicitTagDialog />
 
@code {
    private TabContainer    tabContainer;
    private Dialog          deleteTagDialog;
    private TagEditDialog   tagEditDialog;
    private TagSelectDialog implicitTagDialog;

    private TagDefinition? toDelete;
    private TagDefinition? toEditImplicit;

    private TagDefinition[] tagDefinitions;

    private string[] tagNamespaces = Array.Empty<string>();

    // protected override void OnInitialized() =>
    //     userService.UserSessionState.OnStateChange += ShowNsfwChanged;

    protected override void OnParametersSet() =>
        LoadTagsAsync();

    private async void LoadTagsAsync() {
        bool showNsfw = true;

        Guid[] nsfwTags = Array.Empty<Guid>();
        // if(!userService.UserSessionState.ShowNsfw)
        //     nsfwTags = tagService.TagsThatImply(HBContext.NsfwTag)
        //         .Select(td => td.ObjectId)
        //         .ToArray();

        tagDefinitions = (await hb.Tag.GetTagDefinitionAsync())
                // TODO: Limit shown tags to user-tags
                //.Where(td => td.Source == TagSource.UserTag)
                .OrderBy(td => td.Namespace)
                .ThenBy(td => td.Name)
                .Where(td => showNsfw || !td.ImplicitTags.Intersect(nsfwTags).Any())
                .ToArray();

        tagNamespaces = tagDefinitions
            .Select(td => td.Namespace)
            .Order()
            .Distinct()
            .ToArray();

        await InvokeAsync(() => StateHasChanged());
    }

    private void PromptToDelete(TagDefinition toDelete) {
        this.toDelete = toDelete;
        deleteTagDialog.Show();
    }

    private void DeleteTagDefinition() {
        // if(toDelete is null)
        //     return;

        // tagService.DeleteTagDefinition(toDelete);
        // deleteTagDialog.Hide();
        // LoadTagsAsync();
        // StateHasChanged();
    }

    private void PromptTagCreate() {
        string? ns = tabContainer.ActivePane?.Title;
        if(ns == "Default")
            ns = null;
        tagEditDialog.Show(ns);
    }

    private void TagUpdated(object? sender, EventArgs e) {
        LoadTagsAsync();
        StateHasChanged();
    }

    private void PromptImplicitTags(TagDefinition toEditImplicit) {
        // this.toEditImplicit = toEditImplicit;
        // implicitTagDialog.SelectedTags =
        //     toEditImplicit.ImplicitTags.ToArray();
        // implicitTagDialog.Show();
    }

    private void SetImplicitTags(TagDefinition[] tagDefs) {
        if(toEditImplicit is null)
            return;

        // tagService.SetImplicitTags(toEditImplicit, tagDefs);
        LoadTagsAsync();
        StateHasChanged();
    }

    private void SetNsfw(TagDefinition tagDef, bool nsfw) {
        // if(nsfw)
        //     tagService.AddImplicitTag(tagDef.Guid, HBContext.NsfwTag);
        // else
        //     tagService.RemoveImplicitTag(tagDef.Guid, HBContext.NsfwTag);
        // LoadTagsAsync();
        // StateHasChanged();
    }

    // private async void ShowNsfwChanged(UserSessionState userSessionState) =>
    //     await InvokeAsync(() => {
    //         LoadTagsAsync();
    //         StateHasChanged();
    //     });

    // public void Dispose() =>
    //     userService.UserSessionState.OnStateChange -= ShowNsfwChanged;
}